为什么我的分数变量没有显示正确的值?

Why isn't my score variable showing the right value?

我正在制作一款基本的剪刀石头布游戏。到目前为止,这个程序似乎运行良好,它检测到玩家和计算机之间的选择,但分数并没有正常上升。如果玩家连续多次获胜,我不能得到高于 1 的分数,如果出现平局,则分数变为 0。

computerSelection由随机数决定,playerSelection在下面调用函数时进入

如果玩家第一次选择 rock 而电脑选择 paper,控制台会显示“"Player wins and the score is 1" .
如果在第二轮玩家选择 scissors 而计算机选择 paper,控制台会显示 "Player wins and the score is 1" 而我希望它说“ "Player wins and the score is 2"

function computerPlay() {
    const random = Math.ceil(Math.random() * 3);
    if (random === 1) {
        return "rock";
    }
    else if (random === 2) {
        return "paper";
    }
    else {
        return "scissors";
    }
}

let score = 0;

function playRound(playerSelection, computerSelection) {

    // Computer wins
    if ((playerSelection === 'paper' && computerSelection === 'scissors') ||
        (playerSelection === 'scissors' && computerSelection === 'rock') ||
        (playerSelection === 'rock' && computerSelection === 'paper')) {
        score--;

        //Stop negative scores
        if (score < 0) {
            score = 0;
        }
        return "Computer wins and the score is " +score;
    }

    // Player wins
    else if ((playerSelection === 'paper' && computerSelection === 'rock')|| 
        (playerSelection === 'rock' && computerSelection === 'scissors') ||
        playerSelection === 'scissors' && computerSelection === 'paper') {
        score++;
        return "Player wins and the score is " +score;
    }

    // Same selection
    else {
        return "Tie and the score is " +score;
    }

}
console.log(playRound('paper', computerPlay()));

好的,所以你的问题是你没有任何逻辑来让游戏 "session" 继续下去。意思是,(基于我现在在你的 post 中看到的内容) 每轮游戏都是独占的,并且每轮的结果不会复合。要解决这个问题,您需要实现一个游戏循环来控制游戏流程并允许每个会话进行多轮。

话虽如此,这是我想出的并经过一些测试可以帮助你的:

function computerPlay () {
  const options = [ 'rock', 'paper', 'scissors' ];
  return options[Math.floor(Math.random()*options.length)];
}


function computerWins ( playerSelection, computerSelection ) {
  return ( playerSelection === 'paper' && computerSelection === 'scissors' ) ||
      ( playerSelection === 'scissors' && computerSelection === 'rock' ) ||
      ( playerSelection === 'rock' && computerSelection === 'paper' );
}

function playerWins ( playerSelection, computerSelection ) {
  return ( playerSelection === 'paper' && computerSelection === 'rock' ) ||
      ( playerSelection === 'rock' && computerSelection === 'scissors' ) ||
      playerSelection === 'scissors' && computerSelection === 'paper';
}

function playRound ( score, playerSelection, computerSelection ) {
  let result = {};

  // Computer wins
  if ( computerWins( playerSelection, computerSelection ) ) {
    score--;

    //Stop negative scores
    if ( score < 0 ) {
      score = 0;
    }
    result = { message : 'Computer wins, and the score is: ', score };
  }

  // Player wins
  else if ( playerWins( playerSelection, computerSelection ) ) {
    score++;
    result = { message : 'Player wins and the score is: ', score };
  }

  // Same selection
  else {
    result = { message : 'Tie game and the score is: ', score };
  }
  return result;

}

function annouceWinner ( score, message ) {
  console.log( `${message} ${score}` );
}

function main () {
  let score = 0;

  while ( score < 5 ) {
    let roundResult = playRound( score, 'paper', computerPlay() );
    score           = roundResult.score;
    annouceWinner( score, roundResult.message );
  }
}

main();

您会注意到我创建了几个实用程序方法,并对其进行了整体清理。

  • 现在有一个 computerWins 方法来保存确定计算机何时获胜的逻辑。这很好,因为如果由于某种原因这个逻辑需要重构,它只需要在这个方法中完成!
  • 现在有一个 playerWins 方法来保存确定玩家何时获胜的逻辑。
  • 现在有一个 announceWinner 方法。这不一定是必需的,但这使您可以轻松地将消息传递部分从主循环函数中分离出来。
  • 现在有一个 main 方法。这是核心,它控制程序的流程并允许每个会话有多个 "rounds"。

示例输出:

Computer wins, and the score is:  1
Tie game and the score is:  1
Player wins and the score is:  2
Player wins and the score is:  3
Computer wins, and the score is:  2
Tie game and the score is:  2
Player wins and the score is:  3
Player wins and the score is:  4
Player wins and the score is:  5