代码之家  ›  专栏  ›  技术社区  ›  00Saad

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

  •  0
  • 00Saad  · 技术社区  · 6 年前

    我正在做一个基本的石头剪刀游戏。到目前为止,这个程序似乎进展顺利,它检测到玩家和计算机之间的选择,但分数没有正确上升。如果球员连续多次获胜,我的得分不会高于1,如果打成平局,得分将变为0。

    计算机选择由一个随机数决定,当下面调用该函数时,将输入playerSelection。

    如果玩家选择 岩石 电脑会选择 纸张 ,控制台第一次显示“玩家获胜,得分为1”。
    如果第二轮选手选择 剪刀 电脑会选择 纸张 ,控制台显示“玩家获胜,分数为 1. “当我预料到它会说”“玩家赢了,比分是” 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()));
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Evan Bechtol    6 年前

    好吧,那么你的问题是你没有任何逻辑来保持游戏“会话”继续。意思 (根据我现在在你的帖子中看到的情况) 每一轮比赛都是排他性的,并且结果不会使每一轮的结果复杂化。要解决这个问题,您需要实现一个游戏循环来控制游戏流程,并允许每个会话进行多轮。

    话虽如此,以下是我的想法和测试结果,以帮助您:

    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 方法这是核心,它控制程序的流程,并允许您在每个会话中有多个“回合”。

    示例输出:

    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
    
    推荐文章