[英]What do i need to add, so my JavaScript would work as i want it to?
I started learning JavaScript , and this is my first game i built: Rock, Paper, Scissors. 我开始学习JavaScript ,这是我构建的第一款游戏:Rock,Paper,Scissors。 (Player vs Computer) Now, everything is working, but i couldn't figure out how to make this :
(玩家与电脑)现在,一切正常,但我无法弄清楚如何做到这一点 :
When result is tie, the game starts again asking if i would like to play again, without having to refresh the browser. 当结果为平局时,游戏再次开始询问我是否想再次播放,而无需刷新浏览器。
Please don't answer with jQuery , I'm trying to learn JavaScript! 请不要用jQuery回答 ,我正在尝试学习JavaScript! :)
:)
Here is my snippet: 这是我的片段:
// declare variables var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); // prevent the user from choosing a different answer if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") { userChoice = prompt("Your answer is not acceptable! Please choose again!"); } // pick a random answer from computer if (computerChoice < 0.34) { computerChoice = "rock"; } else if (computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } alert("Computer: " + computerChoice); // check who wins var compare = function(choice1, choice2) { if (choice1 === choice2) { alert("The result is a tie!"); } else if (choice1 === "rock") { if (choice2 === "scissors") { alert("rock wins"); } else { alert("paper wins"); } } else if (choice1 === "paper") { if (choice2 === "rock") { alert("paper wins"); } else { alert("scissors win"); } } } // call the function compare(userChoice, computerChoice);
What you're basically looking to do is wrap the game in a larger contextual loop, which itself just checks for a terminating condition to the game. 你基本上要做的是将游戏包装在一个更大的上下文循环中,它本身只是检查游戏的终止条件。
Structurally, let's assume the entirety of your game is enclosed in a function: 在结构上,让我们假设您的整个游戏都包含在一个函数中:
function playGame() {
// all the code you have now
}
Then the loop would have a structure like this: 然后循环将有这样的结构:
while (true) {
playGame();
if (!confirm("Would you like to play again?")) {
break;
}
}
You can of course get fancier with the confirmation, or perhaps use a value to control the loop rather than an infinite loop with a break
. 当然,你可以通过确认来获得更好的功能,或者使用一个值来控制循环而不是一个带有
break
的无限循环。 But the concept is the same. 但这个概念是一样的。 The overall loop will continue to repeat and "play again" until some condition exists which would cause it to stop.
整个循环将继续重复并“再次播放”,直到存在某些条件导致其停止。
So in your case, you want to check if it's a tie, yes? 所以在你的情况下,你想检查它是否是平局,是吗? Structurally something like this:
结构上是这样的:
while (true) {
playGame();
if (!gameIsTie()) {
break;
}
}
or: 要么:
var winner = 0;
while (winner == 0) {
playGame();
winner = determineWinner();
}
Wherein you would implement the functions needed to determine if the game is a tie, or if there is a winner, etc. 其中你将实现确定游戏是否平局所需的功能,或者是否有赢家等。
You can put all in function and call if is tie. 如果是平局,你可以把所有功能和呼叫都放在一起。
(function game(){ // declare variables var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); // prevent the user from choosing a different answer if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") { userChoice = prompt("Your answer is not acceptable! Please choose again!"); } // pick a random answer from computer if (computerChoice < 0.34) { computerChoice = "rock"; } else if (computerChoice <= 0.67) { computerChoice = "paper"; } else { computerChoice = "scissors"; } alert("Computer: " + computerChoice); // check who wins var compare = function(choice1, choice2) { if (choice1 === choice2) { alert("The result is a tie!"); var newGame = prompt("Play again?"); if(newGame === 'yes'){ game(); } } else if (choice1 === "rock") { if (choice2 === "scissors") { alert("rock wins"); } else { alert("paper wins"); } } else if (choice1 === "paper") { if (choice2 === "rock") { alert("paper wins"); } else { alert("scissors win"); } } } // call the function compare(userChoice, computerChoice); })()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.