简体   繁体   English

我需要添加什么,所以我的JavaScript会像我想要的那样工作?

[英]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.

相关问题 我需要添加什么代码,以便此javascript自动在文本末尾添加3个点? - What code do I need add so that this javascript adds 3 dots at the end of the text automatically? 需要帮助让我的页脚做我想做的事情 - Need help getting my footer to do what I want it to do 为了使我的character_count工作,我需要对HTML和JavaScript代码进行哪些更改? - What changes to my HTML and JavaScript code do I need to make, in order for my character_count to work? 我需要向我的 javascript 添加什么才能使用本地存储,以便在刷新页面时,用户选择的字体大小仍然相同? - What do I need to add to my javascript to be able use local storage so that when the page is refreshed, the user-selected font-size is still the same? 我已将此数据读入控制台。 我想在我的网页中显示“ exampleHeader”和“ exampleBody”。 我该怎么办? - I have this data reading into my Console. I want to display the “exampleHeader” and “exampleBody” in my webpage. How would I do so? 我是否需要为此表更改或添加新数组才能在 Javascript 中工作? - Do I need change or add new array for this table to work in Javascript? 为什么我需要在我的javascript方法中添加单词this - Why Do I need to add the word this to my javascript method 如果要使用Telerik的Kendo UI,我需要知道哪些语言? - What languages do I need to know if I want to work with Kendo UI from Telerik? 我需要更改什么才能在 JS 中调用我的函数? - What do I need to change so I can call my function in JS? 我需要向我的 Javascript 添加什么才能显示来自我的 DynamoDB 表和 API 网关的命中计数器? - What do I need to add to my Javascript to be able to show the hit counter from my DynamoDB table and API Gateway?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM