二十一点如果声明

时间:2018-01-16 17:43:46

标签: javascript html if-statement blackjack

我正在制作一个简单的黑杰克游戏,我遇到了问题 声明。当我点击触发if语句的立场按钮时,它总是显示提示"你赢了"不管得分是多少。

例如,如果用户得分为11且计算机得分为18(总是设置为),则提示"您将失去"应该显示。



     
 var randomnumber = Math.floor(Math.random() * 10 + 1);

 function random() {
     return randomnumber;
 }

 var total = randomnumber;

 function dealcard() {
     total += Math.floor(Math.random() * 10 + 1);
     document.getElementById('playerscards').value = total;

     if (total > 21) {
         alert("You have gone bust click new game to play again!");
     }
 }

 function keepcards() 
 {
     if (total > randomnumber) {
         alert("You win!");
     }    
     else if (total < randomnumber) {
         alert("You lose!");
     }    
     else if (total === randomnumber) {
         alert("It is a draw!");}     
     }
 now = new Date();
 localtime = now.toString();		
 hours = now.getHours();
 mins = now.getMinutes();
 secs = now.getSeconds();
 document.write("<h1>");
 document.write(hours + ":" + mins + ":" + secs);
 document.write("</h1>");
&#13;
<head>
    <h1><i>BLACKJACK</i></h1>
    <h4>
    Computers Cards: <input type="text" id="computerscards" value="18">
    <br>Player 1 cards: <input type="text" id="playerscards">
    </h4>
</head>

<input type="button" value="start" 
    onclick="document.getElementById('playerscards').value = random()">

<input type="button" value="deal" onclick="dealcard()">
<input type="button" value="stand" onclick="keepcards()">
<input type="button" value="new game" onclick="window.location.reload()">

<p>To start the game press start and draw a card<br> Press deal to add a new 
card <br> press stand if you do not wish to draw a new card<br> Press new game 
if you want to refresh the page to play a mew game</p>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

如果您调试代码,您将看到您正在检查randomNumber的总值,但是要真正检查玩家是否赢了,是否需要针对ComputerHand检查它?  所以我在下面的代码片段中做了 现在你的代码将coumputerHand修复为18,在不久的将来我会假设你会以一种计算机收到像播放器这样的ramdom卡的方式改变它。

var randomnumber = Math.floor(Math.random() * 10 + 1);

    function random() {
      return randomnumber;
    }

    var total = randomnumber;
    var CPUHand = parseInt(document.getElementById('computerscards').value);

    function dealcard() {
      total += Math.floor(Math.random() * 10 + 1);
      document.getElementById('playerscards').value = total;

      if (total > 21) {
        alert("You have gone bust click new game to play again!");
      }
    }

    function keepcards(){
      debugger;

      if (total > CPUHand) {
        alert("You win!");
      }    
      else if (total < CPUHand) {
        alert("You lose!");
      }    
      else if (total === CPUHand) {
        alert("It is a draw!");
      }     
    }
    
      now = new Date();
      localtime = now.toString();		
      hours = now.getHours();
      mins = now.getMinutes();
      secs = now.getSeconds();
      document.write("<h1>");
      document.write(hours + ":" + mins + ":" + secs);
      document.write("</h1>");
    
<head>
    <h1><i>BLACKJACK</i></h1>
    <h4>
    Computers Cards: <input type="text" id="computerscards" value="18">
    <br>Player 1 cards: <input type="text" id="playerscards">
    </h4>
    </head>

    <input type="button" value="start" 
    onclick="document.getElementById('playerscards').value = random()">

    <input type="button" value="deal" onclick="dealcard()">

    <input type="button" value="stand" onclick="keepcards()">

    <input type="button" value="new game" onclick="window.location.reload()">

    <p>To start the game press start and draw a card<br> Press deal to add a new 
    card <br> press stand if you do not wish to draw a new card<br> Prsee new 
    game 
    if you want to refresh the page to play a mew game</p>