随机的if / else行为-石头,纸,剪刀游戏

时间:2019-03-26 00:16:22

标签: javascript

所以我已经研究了一段时间了,无法弄清楚为什么我的if / else语句没有控制台。根据计算机和播放器的选择,记录正确的响应。 无论播放器或计算机的输入如何,它似乎都会返回一个随机语句。另外,我不明白为什么它会提示我输入两次。

我知道还有其他方法可以在控制台中构建一个简单的RPS游戏,但是我不明白为什么这将无法正常工作。我更担心在构建一个简单的游戏中缺少哪些步骤,这对于以后的其他项目至关重要。我显然是新来的,正在通过Odin Project网站进行研究,以供参考。提前非常感谢您的帮助!无论如何,这是代码:

  <script>
    function computerChoice() {
      let choices = ['rock', 'paper', 'scissors'];
      let result = choices[Math.floor(Math.random() * choices.length)];
      return result
    };

    let playerSelection = function() {
      let playerChoice = prompt('What do you choose, young Padawan?')
      return playerChoice.toLowerCase()
    };

    let computerSelection = computerChoice();

    console.log(computerChoice());
    console.log(playerSelection());

    function play(playerSelection, computerSelection) {
      if (playerSelection === 'rock' && computerSelection === 'paper') {
        console.log('Paper beats rock! Computron wins!');
      } else if (playerSelection === 'rock' && computerSelection === 'scissors') {
        console.log('Rock smash scissors! You win!');
      } else if (playerSelection === 'paper' && computerSelection === 'rock') {
        console.log('Paper covers rock! You win Starlord!');
      } else if (playerSelection === 'paper' && computerSelection === 'scissors') {
        console.log('Scissors cuts paper! Thanos is king!'); 
      } else if (playerSelection === 'scissors' && computerSelection === 'paper') {
        console.log('Scissors cuts paper! The Avengers avenge!'); 
      } else if (playerSelection === 'scissors' && computerSelection === 'rock') {
        console.log('Rock smash! Avengers suck!');
      } else {
        console.log('Tie! You must select a different weapon!');
        }   
    };

      play();

  </script>

2 个答案:

答案 0 :(得分:1)

在这里,您可以使用多种不同的方式来定义和调用函数。您可以定义播放器选择功能,但不要像在选择计算机时那样在单个实例中调用它。为了使播放器选择与结果一致,请将您的播放器选择定义为函数,然后在记录结果之前将结果分配给变量。

function computerChoice() {
  let choices = ['rock', 'paper', 'scissors'];
  let result = choices[Math.floor(Math.random() * choices.length)];
  return result
};

function playerChoice() {
  let playerChoice = prompt('What do you choose, young Padawan?');
  return playerChoice.toLowerCase();
}

let playerSelection = playerChoice();
let computerSelection = computerChoice();

console.log("Player selection", playerSelection);
console.log("Computer selection", computerSelection);

function play(playerSelection, computerSelection) {
  if (playerSelection === 'rock' && computerSelection === 'paper') {
    console.log('Paper beats rock! Computron wins!');
  } else if (playerSelection === 'rock' && computerSelection === 'scissors') {
    console.log('Rock smash scissors! You win!');
  } else if (playerSelection === 'paper' && computerSelection === 'rock') {
    console.log('Paper covers rock! You win Starlord!');
  } else if (playerSelection === 'paper' && computerSelection === 'scissors') {
    console.log('Scissors cuts paper! Thanos is king!'); 
  } else if (playerSelection === 'scissors' && computerSelection === 'paper') {
    console.log('Scissors cuts paper! The Avengers avenge!'); 
  } else if (playerSelection === 'scissors' && computerSelection === 'rock') {
    console.log('Rock smash! Avengers suck!');
  } else {
    console.log('Tie! You must select a different weapon!');
  }   
};

play(playerSelection, computerSelection);

编辑

还要为播放功能定义两个参数,但不要将其传递给该函数,请确保同时传递playerSelection和computerSelection

答案 1 :(得分:0)

您在用console.log调用函数时,我添加了一个保证,以确保在选择用户输入之前什么都不会运行。然后显示如果要平手就重新启动。

console.log(computerChoice()); // re-runs function
console.log(playerSelection()); // re-runs function

在开始向用户发出警告以进行礼貌之前,我还要确保页面已加载。

document.addEventListener("DOMContentLoaded", function(event) { })

对于computerChoice函数,让我们删除冗余变量,因为我们返回的是变量值。我们改为这样做。

return (choices[Math.floor(Math.random() * choices.length)]);

我们在用户输入中使用了promise,promise基本上是一个承诺值或拒绝某些东西的函数。调用函数时,使用.then((value))来获取值。

let playerSelection = function() {
      return new Promise((resolve) => {
        let playerChoice = prompt('What do you choose, young Padawan?')
        console.log(playerChoice); // we log the player choice here instead
        resolve(playerChoice.toLowerCase());
      })
};

我们得到这样的承诺值

playerSelection().then((value) => {  })

function computerChoice() {
        let choices = ['rock', 'paper', 'scissors'];
        return (choices[Math.floor(Math.random() * choices.length)]);

      }

    let playerSelection = function() {
      return new Promise((resolve) => {
        let playerChoice = prompt('What do you choose, young Padawan?')
        console.log(playerChoice); // we log the player choice here instead
        resolve(playerChoice.toLowerCase());
      })
    };

    // Remove console.log(playerChoice); calling the function, causes two prompts

    function play() {
      playerSelection().then((playerChoice) => {
        let computerSelection = computerChoice(); // We want new one each time.
        if (playerChoice === 'rock' && computerSelection === 'paper') {
          console.log('Paper beats rock! Computron wins!');
        } else if (playerChoice === 'rock' && computerSelection === 'scissors') {
          console.log('Rock smash scissors! You win!');
        } else if (playerChoice === 'paper' && computerSelection === 'rock') {
          console.log('Paper covers rock! You win Starlord!');
        } else if (playerChoice === 'paper' && computerSelection === 'scissors') {
          console.log('Scissors cuts paper! Thanos is king!');
        } else if (playerChoice === 'scissors' && computerSelection === 'paper') {
          console.log('Scissors cuts paper! The Avengers avenge!');
        } else if (playerChoice === 'scissors' && computerSelection === 'rock') {
          console.log('Rock smash! Avengers suck!');
        } else {
          alert('Tie! You must select a different weapon!');
          play() // Tie-have user re-enter choice
        }
      })
    };
    // Make sure page loaded
    document.addEventListener("DOMContentLoaded", function(event) {
      //do work
      play();
    });