2 votes

Comment accumuler une valeur en JavaScript

Je suis nouveau en programmation et je veux créer un jeu de Black Jack en utilisant JavaScript, mais j'ai un problème, lorsque le jeu commence et que la première main est servie si le joueur veut "frapper" plus d'une fois, la valeur de player_total2 et ne s'accumule pas, voici mon code :

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;
  }
}

1voto

bcr666 Points 1041
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();

Voici votre problème

            player_total2 = player_total2 + player_total + cardValue(card_dealt);

player_total2 a déjà le player_total, et vous l'ajoutez à nouveau.

        }
    }

    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 + ".");
}

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X