【问题标题】:Blackjack if statement二十一点 if 语句
【发布时间】:2018-06-25 12:03:04
【问题描述】:

我正在制作一个简单的二十一点游戏,但我的 if 有问题 陈述。当我单击触发 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>");
<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>

【问题讨论】:

  • line9: ` var total = randomnumber;` 之后我想你点击:deal 什么增加了total 的值:total += Math.floor(Math.random() * 10 + 1); in keepcards() 你有这个:` if (total > randomnumber)`什么是真的。

标签: javascript html if-statement blackjack


【解决方案1】:

如果您调试您的代码,您会看到您正在对照 randomNumber 检查总值,但要真正检查玩家是否赢了,有必要对照 ComputerHand 进行检查,对吗?
所以我在下面的 sn-p 中做了。
现在,您将计算机手的代码修复为 18,在不久的将来,我假设您将更改它,使计算机像播放器一样接收随机卡。

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>

【讨论】:

  • 不客气。如果这对您有帮助,请点赞并标记为正确:D
猜你喜欢
  • 2013-04-29
  • 2013-10-01
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-31
  • 2021-11-14
相关资源
最近更新 更多