如何在JavaScript中累积值

问题描述 投票:2回答:1

我是编程方面的新手,我想使用JavaScript创建一个Black Jack游戏,但是我遇到了一个问题,当游戏开始并且如果玩家想多次“击中”一手发球时, player_total2加倍并且没有累积,以下是我的代码:

var player_total;
var player_total2;
var player_total3;
var player_card1;
var player_card2;
var firstPlay;
var secondPlay;
var welcome = confirm("Welcome to ♠ BalckJack ♥, the goal of the game is to get 21 points, you have the options to hit or stand.");
var message;
var card_dealt;

var card_deck = [
  ["Ace-H", 1],
  ["2-H", 2],
  ["3-H", 3],
  ["4-H", 4],
  ["5-H", 5],
  ["6-H", 6],
  ["7-H", 7],
  ["8-H", 8],
  ["9-H", 9],
  ["10-H", 10],
  ["Jack-H", 10],
  ["Queen-H", 10],
  ["King-H", 10],
  ["Ace-S", 1],
  ["2-S", 2],
  ["3-S", 3],
  ["4-S", 4],
  ["5-S", 5],
  ["6-S", 6],
  ["7-S", 7],
  ["8-S", 8],
  ["9-S", 9],
  ["10-S", 10],
  ["Jack-S", 10],
  ["Queen-S", 10],
  ["King-S", 10],
  ["Ace-D", 1],
  ["2-D", 2],
  ["3-D", 3],
  ["4-D", 4],
  ["5-D", 5],
  ["6-D", 6],
  ["7-D", 7],
  ["8-D", 8],
  ["9-D", 9],
  ["10-D", 10],
  ["Jack-D", 10],
  ["Queen-D", 10],
  ["King-D", 10],
  ["Ace-C", 1],
  ["2-C", 2],
  ["3-C", 3],
  ["4-C", 4],
  ["5-C", 5],
  ["6-C", 6],
  ["7-C", 7],
  ["8-C", 8],
  ["9-C", 9],
  ["10-C", 10],
  ["Jack-C", 10],
  ["Queen-C", 10],
  ["King-C", 10]
];

if (welcome === true) {
  shuffle(card_deck);

  player_card1 = card_deck.pop();
  player_card2 = card_deck.pop();
  player_total = cardValue(player_card1) + cardValue(player_card2);

  if (player_total == 21) {
    message = console.log("Your card total is " + player_total + ". Congratulations! You win!!!");
  } else {
    firstPlay = prompt("You hand is " + cardName(player_card1) + " and " + cardName(player_card2) + ". Your card total is " + player_total + ". Do you want to hit or stand?");
  }

  if (firstPlay === "hit") {
    card_dealt = card_deck.pop();
    player_total2 = player_total + cardValue(card_dealt);

    while (player_total2 < 21 && secondPlay !== "stand") {
      var secondPlay = prompt("You got now a " + cardName(card_dealt) + ". Your card total is " + player_total2 + ". Do you want to hit or stand?");
      if (secondPlay === "hit") {
        card_dealt = card_deck.pop();
        player_total2 = player_total2 + player_total + cardValue(card_dealt);
      }
    }

    if (secondPlay === "stand") {
      message = console.log("Your total card value is " + player_total2 + ".");
    } else if (player_total2 > 21) {
      message = console.log("You got a " + cardName(card_dealt) + ". Your total card value is " + player_total2 + ". You lost!");
    }
  } else {
    message = console.log("Your total card value is " + player_total + ".");
  }

}

function cardValue(card) {
  return card[1];
}

function cardName(card) {
  return card[0];
}

function shuffle(a) {
  var j, x, i;
  for (i = a.length - 1; i > 0; i--) {
    j = Math.floor(Math.random() * (i + 1));
    x = a[i];
    a[i] = a[j];
    a[j] = x;
  }
}
javascript
1个回答
1
投票
if (firstPlay === "hit") {
    card_dealt = card_deck.pop();
    player_total2 = player_total + cardValue(card_dealt);

    while (player_total2 < 21 && secondPlay !== "stand") {
        var secondPlay = prompt("You got now a " + cardName(card_dealt) + ". Your card total is " + player_total2 + ". Do you want to hit or stand?");
        if (secondPlay === "hit") {
            card_dealt = card_deck.pop();

这是您的问题

            player_total2 = player_total2 + player_total + cardValue(card_dealt);

player_total2已经有player_total,并且您要再次添加它。

        }
    }

    if (secondPlay === "stand") {
        message = console.log("Your total card value is " + player_total2 + ".");
    } else if (player_total2 > 21) {
        message = console.log("You got a " + cardName(card_dealt) + ". Your total card value is " + player_total2 + ". You lost!");
    }
} else {
    message = console.log("Your total card value is " + player_total + ".");
}
© www.soinside.com 2019 - 2024. All rights reserved.