Стоп оценка становится отрицательной и старше 52 - PullRequest
0 голосов
/ 22 мая 2019

У меня есть текстовая военная игра, в которой команды начинают со счетом 26 (userScore и computerScore).Когда игрок выигрывает, он идет вверх, а когда игрок проигрывает, он падает.

Проблема в том, что счет становится отрицательным и превышает 52 (в случае войны на 51 карте), что составляет все карты плюс некоторые (не может иметь более 52 карт).

Как я могу ограничить userScore и computerScore отрицательным результатом или превышающим 52?

var suits = ["Spades", "Hearts", "Clubs", "Diamonds"];
var cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"];

var attempts = 1;
var war = false;

var computerScore = 26;
var userScore = 26;


while (computerScore < 52 && userScore < 52)
{

  var computerIndex = Math.floor(Math.random() * cards.length);
  var userIndex = Math.floor(Math.random() * cards.length);

  var computerCard = cards[computerIndex];
  var userCard = cards[userIndex];
  var computerSuit = suits[Math.floor(Math.random()*suits.length)];
  var userSuit = suits[Math.floor(Math.random()*suits.length)];

  alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);

  if (computerIndex > userIndex && war == false)
  {
    computerScore++;
    userScore--;
    alert("I win. \n\nI have " + computerScore + " cards and you have " + userScore + " cards.");
  }

  else if (computerIndex < userIndex && war == false)
  {
    computerScore--;
    userScore++;
    alert("You win. \n\nI have " + computerScore + " cards and you have " + userScore + " cards.");
  }

  else if (computerIndex == userIndex && war == false)
  {
    alert("TIE! TIME FOR WAR! \n\nI have " + computerScore + " cards and you have " + userScore + " cards")
    war = true;

    var computerIndex = Math.floor(Math.random() * cards.length);
    var userIndex = Math.floor(Math.random() * cards.length);

    var computerCard = cards[computerIndex];
    var userCard = cards[userIndex];
    var computerSuit = suits[Math.floor(Math.random()*suits.length)];
    var userSuit = suits[Math.floor(Math.random()*suits.length)];

    alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);

      if (computerIndex > userIndex && war == true)
          { 
            computerScore = computerScore + (attempts * 3);
            userScore = userScore - (attempts * 3);
            alert("I win. \n\nI have " + computerScore + " cards and you have " + userScore + " cards.");
            war = false;
            attempts = 1;
          }

          else if (computerIndex < userIndex && war == true)
          {
            userScore = userScore + (attempts * 3);
            computerScore = computerScore - (attempts * 3);
            alert("You win. \n\nI have " + computerScore + " cards and you have " + userScore + " cards.");
            war = false;
            attempts = 1;
          }

          else 
          {
            alert("TIE! TIME FOR WAR (AGAIN)! \n\nI have " + computerScore + " cards and you have " + userScore + " cards")
            attempts++;

            var computerIndex = Math.floor(Math.random() * cards.length);
            var userIndex = Math.floor(Math.random() * cards.length);

            var computerCard = cards[computerIndex];
            var userCard = cards[userIndex];
            var computerSuit = suits[Math.floor(Math.random()*suits.length)];
            var userSuit = suits[Math.floor(Math.random()*suits.length)];

            alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);

            if (computerIndex == userIndex)
            {
              war = true;
            }

            else
            {
              war = false;
            }
          }
  }
}  

              if (computerScore >= 52)
                {
                  alert("I WIN!  GOOD GAME!");
                }
                  else
                {
                  alert("YOU WIN!  GOOD GAME!")
                }

1 Ответ

0 голосов
/ 22 мая 2019

Вы используете (cards.length) для вычисления computerIndex и userIndex, в то время как вы должны использовать (cards.length) -1, потому что массивы всегда начинаются с индекса 0, поэтому общее количество карт в итоге превысило 52. и то же самое для костюма. edit: чтобы избежать смещения скобок, просто создайте переменную и используйте ее, как показано ниже:

<script>

alert("Hi. Let's Play War!");

var computerScore = 48;
var userScore = 4;

var suits = ["Spades", "Hearts", "Clubs", "Diamonds"];
var cards = ["2", "3", "4"];

var attempts = 1;
var war = false;

function mathClampC(min,mid,max)
{
  var min = 0;
  var max = 52;
  var mid = computerScore;
  return Math.min(Math.max(min,mid),max);
}

function mathClampU(min,mid,max)
{

  var min = 0;
  var max = 52;
  var mid = userScore;
  return Math.min(Math.max(min,mid),max);
}

while (computerScore < 52 && userScore < 52)
{
  var computerIndex = Math.floor(Math.random() * cards.length);
  var userIndex = Math.floor(Math.random() * cards.length);

  var computerCard = cards[computerIndex];
  var userCard = cards[userIndex];
  var computerSuit = suits[Math.floor(Math.random()*suits.length)];
  var userSuit = suits[Math.floor(Math.random()*suits.length)];

  alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);

  if (computerIndex > userIndex && war == false)
  {
    computerScore++;
    userScore--;
    alert("I win. \n\nI have " + mathClampC() + " cards and you have " + mathClampU() + " cards.");
  }

  else if (computerIndex < userIndex && war == false)
  {
    computerScore--;
    userScore++;
    alert("You win. \n\nI have " + mathClampC() + " cards and you have " + mathClampU() + " cards.");
  }

  else if (computerIndex == userIndex && war == false)
  {
    alert("TIE! TIME FOR WAR! \n\nI have " + mathClampC() + " cards and you have " + mathClampU() + " cards")
    war = true;

    var computerIndex = Math.floor(Math.random() * cards.length);
    var userIndex = Math.floor(Math.random() * cards.length);

    var computerCard = cards[computerIndex];
    var userCard = cards[userIndex];
    var computerSuit = suits[Math.floor(Math.random()*suits.length)];
    var userSuit = suits[Math.floor(Math.random()*suits.length)];

    alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);

      if (computerIndex > userIndex && war == true)
          { 
            computerScore = computerScore + (attempts * 3);
            userScore = userScore - (attempts * 3);
            alert("I win. \n\nI have " + mathClampC() + " cards and you have " + mathClampU() + " cards.");
            war = false;
          }

          else if (computerIndex < userIndex && war == true)
          {
            userScore = userScore + (attempts * 3);
            computerScore = computerScore - (attempts * 3);
            alert("You win. \n\nI have " + mathClampC() + " cards and you have " + mathClampU() + " cards.");
            war = false;
          }

          else if (computerIndex == userIndex || war == true)
          {
            alert("TIE! TIME FOR WAR (AGAIN)! \n\nI have " + mathClampC() + " cards and you have " + mathClampU() + " cards")
            attempts++;

            if (computerIndex == userIndex) 
            {
              war = true;
            }

            else 
            {
              war = false;
            }
          }
  }
}  

              if (computerScore >= 52)
                {
                  alert("I WIN!  GOOD GAME!");
                }
                  else
                {
                  alert("YOU WIN!  GOOD GAME!")
                }

</script>
...