为了账号安全,请及时绑定邮箱和手机立即绑定

如何循环 playRound() 函数?

如何循环 playRound() 函数?

慕后森 2023-08-24 21:08:10
我正在制作我的第一个石头剪刀布游戏。我还没有正式完成循环,但我正在尝试将 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));
查看完整描述

3 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

希望这个例子能给我们一些关于如何进行良好编程的提示


// rock = 0

// paper = 1

// scissor = 2

const valueMap = {

  0: 'rock',

  1: 'paper',

  2: 'scissor'

}


function pick() {

  return Math.floor(Math.random() * 3)

}


function decide(p1, p2) {

  const pool = [p1, p2]

  const sortedPool = pool.sort((a, b) => b.value - a.value)

  const diff = sortedPool[0].value - sortedPool[1].value

  if (diff === 2) {

    return sortedPool[1].name

  } else if (diff === 0) {

    return 'draw'

  } else {

    return pool.find(v => v.value === sortedPool[0].value).name

  }

}


function play(times, cb) {

  let n = 1

  while (n <= times) {

    cb(n)

    n++

  }

}


play(5, function(n) {

  const player1 = {

    name: 'Player',

    value: pick()

  }

  const player2 = {

    name: 'Computer',

    value: pick()

  }

  const result = decide(player1, player2)

  console.log(

    `Game ${n}`,

    `${player1.name} ${valueMap[player1.value]}`,

    ` vs `,

    `${player2.name} ${valueMap[player2.value]}`,

    `>>> Winner ${result}

    `

  )

})



查看完整回答
反对 回复 2023-08-24
?
UYOU

TA贡献1878条经验 获得超4个赞

如果我正确理解你想要什么,你可以用whilea 做一个循环counter,我还改进了你的代码,使严格相等(从==到===),并删除了冗余代码


let counter = 1;


function computerPlay() {

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

  if (number % 3 === 0) {

    return "rock";

  } else if (number % 3 === 1) {

    return "paper";

  } else {

    return "scissors";

  }

}


function playRound(playerSelection, computerSelection) {

  counter++;

  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";


while (counter < 6) {

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

}


查看完整回答
反对 回复 2023-08-24
?
12345678_0001

TA贡献1802条经验 获得超5个赞

为最后 3 行添加循环


    for (i=0; i<5; i++){

       playerSelection = "rock";

       computerSelection = computerPlay();

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

    }

编辑:请注意,由于计算机选择在第二个参数中传递,因此 playRound 函数内有多余的调用函数computerPlay()


查看完整回答
反对 回复 2023-08-24
  • 3 回答
  • 0 关注
  • 136 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信