【问题标题】:How can I get a function to repeat itself 5 times within another function? (Rock Paper Scissors)如何让一个函数在另一个函数中重复 5 次? (剪刀石头布)
【发布时间】:2020-05-27 16:38:34
【问题描述】:

我正在构建一个剪刀石头布游戏的脚本,我让计算机在这里随机返回“石头、纸或剪刀”

function computerPlay() {
      let computerOption = ['Rock', 'Paper', 'Scissors']
      return computerOption[Math.floor(Math.random() * computerOption.length)];
} 

我让电脑在这里和我对战(如果我经常选择“摇滚”)

function playRound(playerSelection, computerSelection) {
      if (computerSelection == 'Paper') {
         return 'You lose! Paper beats Rock!';
      } else if (computerSelection == 'Rock') {
         return 'You tied! Nobody wins.';
      } else {  
          return 'You win! Rock beats Scissors!';
      } 
}
const playerSelection = 'Rock'
const computerSelection = computerPlay()
console.log(playRound(playerSelection, computerSelection))

我遇到问题的部分是创建一个函数,让计算机在新函数 game() 中连续与我对战 5 次。我还没有学习循环,我正在通过 TOP 学习,他们建议只使用我的 playRound() 函数连续 5 次,并在每场比赛后控制台记录结果。

function game() {
      playRound(playerSelection, computerSelection)
      console.log(playRound(playerSelection, computerSelection));
      playRound(playerSelection, computerSelection)
      console.log(playRound(playerSelection, computerSelection));
      playRound(playerSelection, computerSelection)
      console.log(playRound(playerSelection, computerSelection));
      playRound(playerSelection, computerSelection)
      console.log(playRound(playerSelection, computerSelection));
      playRound(playerSelection, computerSelection)
      console.log(playRound(playerSelection, computerSelection));
}

当我调用函数 game() 时,我不断让 console.log 返回相同的值 5 次,所以我觉得它只运行了一次游戏并且只显示了 5 次结果。对于如何让游戏实际运行 5 次,我们将不胜感激。

【问题讨论】:

    标签: javascript


    【解决方案1】:

    对您的game() 稍作更改。或者,您可以使用game2 循环调用它们。

    function computerPlay() {
      let computerOption = ["Rock", "Paper", "Scissors"];
      return computerOption[Math.floor(Math.random() * computerOption.length)];
    }
    
    function playRound(playerSelection, computerSelection) {
      if (computerSelection == "Paper") {
        return "You lose! Paper beats Rock!";
      } else if (computerSelection == "Rock") {
        return "You tied! Nobody wins.";
      } else {
        return "You win! Rock beats Scissors!";
      }
    }
    const playerSelection = "Rock";
    
    function game() {
      console.log(playRound(playerSelection, computerPlay()));
      console.log(playRound(playerSelection, computerPlay()));
      console.log(playRound(playerSelection, computerPlay()));
      console.log(playRound(playerSelection, computerPlay()));
      console.log(playRound(playerSelection, computerPlay()));
    }
    
    const game2 = () =>
      Array.from(new Array(5), () =>
        console.log(playRound(playerSelection, computerPlay()))
      );
    
    game();
    game2();

    【讨论】:

      【解决方案2】:

      生成computerSelection在playRound函数内,这样每个游戏都会生成一个新的随机计算机选择:

      function computerPlay() {
        let computerOption = ['Rock', 'Paper', 'Scissors']
        return computerOption[Math.floor(Math.random() * computerOption.length)];
      }
      
      function playRound() {
        const playerSelection = 'Rock'
        const computerSelection = computerPlay()
        if (computerSelection == 'Paper') {
          return 'You lose! Paper beats Rock!';
        } else if (computerSelection == 'Rock') {
          return 'You tied! Nobody wins.';
        } else {
          return 'You win! Rock beats Scissors!';
        }
      }
      
      function game() {
        for (let i = 0; i < 5; i++) {
          console.log(playRound());
        }
      }
      
      game();

      或者,对于prompt 的廉价交互式版本:

      const choices = ['Rock', 'Paper', 'Scissors'];
      function computerPlay() {
        return choices[Math.floor(Math.random() * choices.length)];
      }
      
      function playRound() {
        let playerSelection;
        while (!choices.includes(playerSelection)) {
          playerSelection = prompt('Rock, Paper, or Scissors?');
          if (playerSelection === null) {
            throw new Error('exiting');
          }
        }
        const computerSelection = computerPlay()
        if (computerSelection == 'Paper') {
          return 'You lose! Paper beats Rock!';
        } else if (computerSelection == 'Rock') {
          return 'You tied! Nobody wins.';
        } else {
          return 'You win! Rock beats Scissors!';
        }
      }
      
      const delay = ms => new Promise(res => setTimeout(res, ms));
      async function game() {
        for (let i = 0; i < 5; i++) {
          console.log(playRound());
          await delay(50); // give Stack Snippet console time to render
        }
      }
      
      game();

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-29
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多