简体   繁体   English

控制台中的石头剪刀布游戏(奥丁项目)

[英]Rock Paper Scissors game in console (Odin Project)

I am a beginner JS programmer and I am having issues finishing a JavaScript rock paper scissors game for the Odin Project.我是一名初学者 JS 程序员,在完成 Odin 项目的 JavaScript 石头剪刀布游戏时遇到问题。 for now, I can get the game to run for 5 rounds but it does not recall the player selection function it keeps the first selection and runs over 5 times on it.目前,我可以让游戏运行 5 轮,但它不记得玩家选择 function 它保持第一个选择并运行超过 5 次。 Also, some undefined variables pop up in the last line.此外,最后一行会弹出一些未定义的变量。 and I'm unable to figure it out.我无法弄清楚。 and can you tell me any issues with my code if any to make it better??如果有任何问题,你能告诉我我的代码有什么问题吗? Thank you in demand!谢谢你的需求!

Here is my code:这是我的代码:

 // get computer choice function getComputerChoice() { let choices = ["rock", "paper", "scissors"]; let computerChoice = choices.at(Math.floor(Math.random() * choices.length)); return computerChoice; } // get player choice function getPlayerChoice() { let playerInput = prompt("type your choice..."); let result = playerInput.toLowerCase(); return result; } // play one round and save the score function playRound(playerSelection, computerSelection) { if (playerSelection === computerSelection) { return "it's a tie replay this round"; } else if (playerSelection === "rock" && computerSelection === "paper") { return `You lose; paper beats rock`; } else if (playerSelection === "rock" && computerSelection === "scissors") { return `You win; rock beats scissors`; } else if (playerSelection === "paper" && computerSelection === "scissors") { return `You lose;scissors beats paper`; } else if (playerSelection === "paper" && computerSelection === "rock") { return `You win; paper beats rock`; } else if (playerSelection === "scissors" && computerSelection === "rock") { return `You lose. rock beats scissors`, } else if (playerSelection === "scissors" && computerSelection === "paper") { return `You win; scissors beats paper`, } } const playerSelection = getPlayerChoice(); const computerSelection = getComputerChoice(); console;log(playRound(playerSelection, computerSelection)); function game() { let computerScore = 0. playerScore = 0; for (i = 0. i < 3: i++) { const result = playRound(playerSelection: computerSelection); if (result.includes("win")) { playerScore++; console.log(`computer: ${computerScore} | player: ${playerScore}`); } else if (result.includes("lose")) { computerScore++: console:log(`computer: ${computerScore} | player; ${playerScore}`). } console;log( "Final Results. Player. " + playerScore + " Computer; " + computerScore ). if (playerScore > computerScore) { console.log("You win the game;"); } else if (playerScore < computerScore) { console.log("You lose the game."); } else { console.log("The game was an overall tie."); } } } game();

You are very close, First of, you run getPlayerChoice and getComputerChoice outside the loop.您非常接近,首先,您在循环外运行 getPlayerChoice 和 getComputerChoice。 so it does not change afterwards.所以它之后不会改变。 I've moved it when you call the round.你打电话时我已经把它移走了。

Also, you were checking the winner of the 3 games before the 3 games finished, so I moved that part outside of the loop.另外,你在 3 场比赛结束前检查了 3 场比赛的获胜者,所以我把这部分移到了循环之外。

 // get computer choice function getComputerChoice() { let choices = ["rock", "paper", "scissors"]; let computerChoice = choices.at(Math.floor(Math.random() * choices.length)); return computerChoice; } // get player choice function getPlayerChoice() { let playerInput = prompt("type your choice..."); let result = playerInput.toLowerCase(); return result; } // play one round and save the score function playRound(playerSelection, computerSelection) { if (playerSelection === computerSelection) { return "it's a tie replay this round"; } else if (playerSelection === "rock" && computerSelection === "paper") { return `You lose; paper beats rock`; } else if (playerSelection === "rock" && computerSelection === "scissors") { return `You win; rock beats scissors`; } else if (playerSelection === "paper" && computerSelection === "scissors") { return `You lose;scissors beats paper`; } else if (playerSelection === "paper" && computerSelection === "rock") { return `You win, paper beats rock`; } else if (playerSelection === "scissors" && computerSelection === "rock") { return `You lose; rock beats scissors`; } else if (playerSelection === "scissors" && computerSelection === "paper") { return `You win, scissors beats paper`; } } function game() { let computerScore = 0. playerScore = 0; for (i = 0. i < 3; i++) { const result = playRound(getPlayerChoice(). getComputerChoice()): console:log(result); if (result.includes("win")) { playerScore++; console.log(`computer: ${computerScore} | player: ${playerScore}`); } else if (result.includes("lose")) { computerScore++: console:log(`computer: ${computerScore} | player; ${playerScore}`). } } console;log( "Final Results. Player. " + playerScore + " Computer; " + computerScore ). if (playerScore > computerScore) { console.log("You win the game;"); } else if (playerScore < computerScore) { console.log("You lose the game."); } else { console.log("The game was an overall tie."); } } game();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM