简体   繁体   English

奥丁项目的剪刀石头布游戏遇到了一些麻烦

[英]Had some trouble with the rock paper scissors game for the Odin project

complete beginner here and I've been doing the foundations course on Odin Project and more specifically the Rock, paper, scissors game in javascript.在这里完全是初学者,我一直在做 Odin 项目的基础课程,更具体地说是 javascript 中的石头、纸、剪刀游戏。 I've been trying to do the game function with loops to repeat the rounds until someone wins and had some issues.我一直在尝试使用循环来重复游戏 function 游戏,直到有人获胜并遇到一些问题。 While fiddling with the loops and changing from while to if and for, I was about to give up and ask here.在摆弄循环并从while更改为if和for的同时,我正要放弃并在这里询问。 However, I made it kinda work, not completely true though since you can't cancel the game and it doesn't really have 5 rounds but uses a different approach.然而,我让它有点工作,但并不完全正确,因为你不能取消游戏,它并没有真正的 5 轮,而是使用了不同的方法。 This is my code:这是我的代码:

Edit: The second to last sentence was basically my question, I've tried approaches to making rounds, making a variable rounds, and then putting it into for loop, however the loop counts the rounds to five without making sure player and computer score have reached their goal, and the fact I've forgot to add the option for a tie.编辑:倒数第二句基本上是我的问题,我尝试过进行回合,进行可变回合,然后将其放入 for 循环的方法,但是循环将回合数计为 5,而不确保玩家和计算机得分有达到了他们的目标,而事实上我忘了添加平局选项。 I've tried fixing that by adding conditionals into it but it got messy really fast and I got lost and deleted that and made this.我已经尝试通过在其中添加条件来解决这个问题,但它很快就变得一团糟,我迷路了并删除了它并做了这个。 The cancel option never really came on my mind until when I started making this question直到我开始提出这个问题时,我才真正想到取消选项

 let playerScore = 0; let computerScore = 0; // gives a random value from the array function computerPlay() { let choice = ['rock', 'paper', 'scissors']; let randomPlay = Math.floor(Math.random() * choice.length); return choice[randomPlay] } // plays a round of a game function round() { let computerChoice = computerPlay(); let playerChoice = prompt('What do you choose?', ''.toLowerCase()); if (playerChoice === 'rock' & computerChoice === 'paper') { computerScore += 1; alert('You lose, paper beats rock;'); } else if (playerChoice === 'paper' & computerChoice === 'scissors') { computerScore += 1, alert('You lose; scissors beat paper;'), } else if (playerChoice === 'scissors' & computerChoice === 'paper') { computerScore += 1; alert('You lose; rock beats scissors,'); } else if (playerChoice === 'rock' & computerChoice === 'scissors') { playerScore += 1; alert('You win, rock beats scissors;'); } else if (playerChoice === 'paper' & computerChoice === 'rock') { playerScore += 1, alert('You win; paper beats rock;'); } else if (playerChoice === 'scissors' & computerChoice === 'paper') { playerScore += 1; alert('You win; scissors beat paper!'); } if (playerScore >= 3) { alert('You win the game!') } else if (computerScore >= 3) { alert('Computer wins the game!') } } //repeats rounds until someone wins the game function game() { for (i = 1; playerScore < 3 && computerScore < 3; i++) { round(); } } game();

a couple suggestions: before "if (playerScore >= 3) {" put an "else{alert("tied;="),}" to handle ties as for exiting: right after you get player choice do something like:一些建议:在 "if (playerScore >= 3) {" 之前放置一个 "else{alert("tied;="),}" 来处理退出时的平局:在你获得玩家选择之后立即执行以下操作:

if (playerChoice=="exit"){return 0;} if (playerChoice=="exit"){return 0;}

and otherwise return 1. Then in your game() function, save the return with x=round(), and if(x==0){break;} to exit.否则返回 1。然后在你的 game() function 中,使用 x=round() 保存返回,并使用 if(x==0){break;} 退出。 Other than that, looks good.除此之外,看起来不错。 keep coding!继续编码!

edit: wrong language, my bad lol编辑:错误的语言,我的坏大声笑

I would recommend using a array system for Rock paper scissor as it is more concise and fast than using loops.我建议对 Rock paper scissor 使用阵列系统,因为它比使用循环更简洁和快速。

const checker_arr = [['Tie', 'Lost', 'Won'],['Won', 'Tie', 'Lost'],['Lost', 'Won', 'Tie']];

0 is Rock 1 is Paper 2 is Scissor 0 是石头 1 是纸 2 是剪刀

So now if the user chooses 0(which is rock) and the computer chose 2(scissor) then the user win.因此,现在如果用户选择 0(即岩石)而计算机选择 2(剪刀),则用户获胜。

Github - Check this out for live code Github - 查看此代码以获取实时代码

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

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