如何循环 playRound() 函数?

我正在制作我的第一个石头剪刀布游戏。我还没有正式完成循环,但我正在尝试将 for 循环应用于 playRound 函数,以便它播放五次。可悲的是我不确定在哪里应用它。我尝试了几种方法,但不断出现错误。任何人都可以看看并提供一个选项。代码如下。


function computerPlay() {

  const number = Math.floor(Math.random() * 1000);

  if (number % 3 === 0) {

    return "rock";

  }

  if (number % 3 === 1) {

    return "paper";

  } else {

    return "scissors";

  }

}

function playRound(playerSelection, computerSelection) {

  playerSelection = "rock";

  computerSelection = computerPlay();

  if (

    (playerSelection == "rock" && computerSelection == "scissors") ||

    (playerSelection == "scissors" && computerSelection == "paper") ||

    (playerSelection == "paper" && computerSelection == "rock")

  ) {

    return "Player Wins!";

  } else if (

    (playerSelection == "rock" && computerSelection == "paper") ||

    (playerSelection == "paper" && computerSelection == "scissors") ||

    (playerSelection == "scissors" && computerSelection == "rock")

  ) {

    return "Computer Wins!";

  } else {

    return "Tie";

  }

}

playerSelection = "rock";

computerSelection = computerPlay();

console.log(playRound(playerSelection, computerSelection));


慕后森
浏览 137回答 3
3回答

慕容森

希望这个例子能给我们一些关于如何进行良好编程的提示// rock = 0// paper = 1// scissor = 2const valueMap = {&nbsp; 0: 'rock',&nbsp; 1: 'paper',&nbsp; 2: 'scissor'}function pick() {&nbsp; return Math.floor(Math.random() * 3)}function decide(p1, p2) {&nbsp; const pool = [p1, p2]&nbsp; const sortedPool = pool.sort((a, b) => b.value - a.value)&nbsp; const diff = sortedPool[0].value - sortedPool[1].value&nbsp; if (diff === 2) {&nbsp; &nbsp; return sortedPool[1].name&nbsp; } else if (diff === 0) {&nbsp; &nbsp; return 'draw'&nbsp; } else {&nbsp; &nbsp; return pool.find(v => v.value === sortedPool[0].value).name&nbsp; }}function play(times, cb) {&nbsp; let n = 1&nbsp; while (n <= times) {&nbsp; &nbsp; cb(n)&nbsp; &nbsp; n++&nbsp; }}play(5, function(n) {&nbsp; const player1 = {&nbsp; &nbsp; name: 'Player',&nbsp; &nbsp; value: pick()&nbsp; }&nbsp; const player2 = {&nbsp; &nbsp; name: 'Computer',&nbsp; &nbsp; value: pick()&nbsp; }&nbsp; const result = decide(player1, player2)&nbsp; console.log(&nbsp; &nbsp; `Game ${n}`,&nbsp; &nbsp; `${player1.name} ${valueMap[player1.value]}`,&nbsp; &nbsp; ` vs `,&nbsp; &nbsp; `${player2.name} ${valueMap[player2.value]}`,&nbsp; &nbsp; `>>> Winner ${result}&nbsp; &nbsp; `&nbsp; )})

UYOU

如果我正确理解你想要什么,你可以用whilea 做一个循环counter,我还改进了你的代码,使严格相等(从==到===),并删除了冗余代码let counter = 1;function computerPlay() {&nbsp; const number = Math.floor(Math.random() * 1000);&nbsp; if (number % 3 === 0) {&nbsp; &nbsp; return "rock";&nbsp; } else if (number % 3 === 1) {&nbsp; &nbsp; return "paper";&nbsp; } else {&nbsp; &nbsp; return "scissors";&nbsp; }}function playRound(playerSelection, computerSelection) {&nbsp; counter++;&nbsp; if (&nbsp; &nbsp; (playerSelection === "rock" && computerSelection === "scissors") ||&nbsp; &nbsp; (playerSelection === "scissors" && computerSelection === "paper") ||&nbsp; &nbsp; (playerSelection === "paper" && computerSelection === "rock")&nbsp; ) {&nbsp; &nbsp; return "Player Wins!";&nbsp; } else if (&nbsp; &nbsp; (playerSelection === "rock" && computerSelection == "paper") ||&nbsp; &nbsp; (playerSelection === "paper" && computerSelection === "scissors") ||&nbsp; &nbsp; (playerSelection === "scissors" && computerSelection === "rock")&nbsp; ) {&nbsp; &nbsp; return "Computer Wins!";&nbsp; } else {&nbsp; &nbsp; return "Tie";&nbsp; }}playerSelection = "rock";while (counter < 6) {&nbsp; console.log(playRound(playerSelection, computerPlay()));}

12345678_0001

为最后 3 行添加循环&nbsp; &nbsp; for (i=0; i<5; i++){&nbsp; &nbsp; &nbsp; &nbsp;playerSelection = "rock";&nbsp; &nbsp; &nbsp; &nbsp;computerSelection = computerPlay();&nbsp; &nbsp; &nbsp; &nbsp;console.log(playRound(playerSelection, computerSelection));&nbsp; &nbsp; }编辑:请注意,由于计算机选择在第二个参数中传递,因此 playRound 函数内有多余的调用函数computerPlay()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript