需要帮助解决循环问题

时间:2016-12-07 07:29:46

标签: javascript

我最近开始学习Javascript,享受它很多。所以,在学习了一些关于循环的信息之后,我决定改进一个简单的Rock,Scissors,Paper游戏。改进是保持玩家获胜和计算机获胜值作为变量,然后循环该函数,直到playerScore变量达到10的值。我还不太擅长语法,尽管试图获得一般逻辑和我犯了错误的地方。

为了实现我的目标,我宣布了两个变量 - playerScore和computerScore,他们的初始值为0.在每个玩家获胜或计算机获胜后,我决定将+ 1添加到变量。

比开始游戏,我已经宣布了一个函数playGame()并使用While循环它。循环似乎是无限的,不仅如此,当前结果不会记录到控制台。非常感谢任何帮助,将帮助我理解逻辑比我通过的任何课程更多。

以下是代码:

var playerScore = 0;
var computerScore = 0;

function getUserChoice() {
  var userInput = prompt('Choose stone, scissors or paper');
  userInput = userInput.toLowerCase();
  if(userInput === 'stone' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') {
    return userInput;
  }
  else {

    alert('Error! Choose stone, scissors or paper!');

  }
}

function getComputerChoice() {
 var randomNumber = Math.floor(Math.random() *3);
  if(randomNumber === 1) {
    return 'stone';
  }

  else if(randomNumber === 2) {
    return 'paper';
  }

  else {
    return 'scissors';

  }
}


function determineWinner (userChoice, computerChoice) {
  if(userChoice === computerChoice) {
    return 'That's a tie!';
  }

  if(userChoice === 'stone') {
    if(computerChoice === 'scissors') {

      playerScore = playerScore + 1;
      сonsole.log(playerScore);
      console.log(computerScore);
      return 'Player won!';


    }
    else {
      if(computerChoice === 'paper') {

      computerScore = computerScore + 1;
      сonsole.log(playerScore);
      console.log(computerScore);

        return 'Computer won!'

      }
    }
  }

  if(userChoice === 'paper') {
    if(computerChoice === 'scissors') {
      computerScore = computerScore + 1;
      сonsole.log(playerScore);
      console.log(computerScore);
      return 'Computer won!';
    }
    else {
      if(computerChoice === 'stone') {
      playerScore = playerScore + 1;
      сonsole.log(playerScore);
      console.log(computerScore);
      return 'Player wonи!';
    }
    }
      }
  if(userChoice === 'scissors') {
    if(computerChoice === 'stone') {
      computerScore = computerScore + 1;
      сonsole.log(playerScore);
      console.log(computerScore);    
      return 'Computer won!';
    }
    else {
      if(computerChoice === 'paper') {
      playerScore = playerScore + 1;
      сonsole.log(playerScore);
      console.log(computerScore); 
      return 'Player won!';
      }
    }
      }

      if(userChoice === 'bomb') {
        if(computerChoice === 'stone' || computerChoice === 'scissors' || computerChoice === 'paper') {
     playerScore = playerScore + 1;
      сonsole.log(playerScore);
      console.log(computerScore); 
      return 'Player won!';
          return 'Player won!';

        }
      }

  }


while(playerScore < 10) {
function playGame() {
  var userChoice = getUserChoice();
  var computerChoice = getComputerChoice();
  alert('Player chose' + ' ' + userChoice + '!');
 alert('Computer chose' + ' ' + computerChoice + '!');

 alert(determineWinner(userChoice, computerChoice));
 playGame() = false;
}
};



playGame();

1 个答案:

答案 0 :(得分:0)

您需要将while循环移动到函数playGame()

function playGame() {
  while(playerScore < 10) {
    var userChoice = getUserChoice();
    var computerChoice = getComputerChoice();
    alert('Player chose' + ' ' + userChoice + '!');
    alert('Computer chose' + ' ' + computerChoice + '!');

    alert(determineWinner(userChoice, computerChoice));
  }
}