当我想“让我们做一个简单的游戏时,我正在玩一些javascript!”
所以我开始了,现在我被困住了,我可以使用你的帮助:p
所以这就是我制定的“计划”:
var name1 = prompt("what is your name?");
var name2 = prompt("what is your name?");
var user1 = prompt("rock, paper, scissors");
var user2 = prompt("rock, paper, scissors");
显示他们的选择:
if (user1 === "rock") {
document.write("<b>rock</b>");
} else if (user1 === "scissors") {
document.write("<b>scissors</b>");
} else if (user1 === "paper") {
document.write("<b>paper</b>");
} else {
alert("DUDE THAT AINT NO OPTION YO");
}
document.write(" vs ");
if (user2 === "rock") {
document.write("<b>rock</b>");
} else if (user2 === "scissors") {
document.write("<b>scissors</b>");
} else if (user2 === "paper") {
document.write("<b>paper</b>");
} else {
alert("DUDE THAT AINT NO OPTION YO");
}
document.write(" = ");
计算结果:
if (user1 === user2) {
document.write("tie, try again!");
}
if (user1 === "rock") {
if (user2 === "scissors") {
document.write("yay " + name1 + "<b> wins!</b>");
} else if (user2 === "rock") {
document.write("tie...");
} else {
document.write("yay " + name2 + "<b> wins!</b>");
}
} else if (user1 === "paper") {
if (user2 === "rock") {
document.write("yay " + name1 + "<b> wins!</b>");
} else if (user2 === "rock") {
document.write("yay " + name2 + "<b> wins!</b>");
} else {
document.write("tie...");
}
} else if (user1 === "scissors") {
if (user2 === "scissors") {
document.write("tie...");
} else if (user2 === "paper") {
document.write("yay " + name1 + "<b> wins!</b>");
} else {
document.write("yay " + name2 + "<b> wins!</b>");
}
} else {
document.write("wut");
}
以下是我遇到的问题:我想创建一个按钮,从步骤3重新启动。步骤1和2不得重复!我该怎么做?
提前致谢!
(代码可能在那里凌乱^所以这里是jsfiddle链接:http://jsfiddle.net/robinvandernoord/c0n2wuuf/)
PS:我应该使用document.write还是其他东西? //更新我使用alert()代替,而且在我看来它更好。
答案 0 :(得分:1)
首先,我会将主要游戏部分(除了要求名字之外的所有内容)放入一个函数中。这样你就可以调用该函数来玩另一个游戏。
要询问他们是否想玩,您可以使用confirm
框询问他们。这将返回true或false。
只需将它放在函数的末尾,如果它们单击OK,再次调用该函数:
function playGame()
{
var user1 = prompt("rock, paper, scissors");
var user2 = prompt("rock, paper, scissors");
if (user1 === "rock") {
document.write("<b>rock</b>");
} else if (user1 === "scissors") {
document.write("<b>scissors</b>");
} else if (user1 === "paper") {
document.write("<b>paper</b>");
} else {
alert("DUDE THAT AINT NO OPTION YO");
}
document.write(" vs ");
if (user2 === "rock") {
document.write("<b>rock</b>");
} else if (user2 === "scissors") {
document.write("<b>scissors</b>");
} else if (user2 === "paper") {
document.write("<b>paper</b>");
} else {
alert("DUDE THAT AINT NO OPTION YO");
}
document.write(" = ");
if (user1 === user2) {
document.write("tie, try again!");
}
if (user1 === "rock") {
if (user2 === "scissors") {
document.write("yay " + name1 + "<b> wins!</b>");
} else if (user2 === "rock") {
document.write("tie...");
} else {
document.write("yay " + name2 + "<b> wins!</b>");
}
} else if (user1 === "paper") {
if (user2 === "rock") {
document.write("yay " + name1 + "<b> wins!</b>");
} else if (user2 === "rock") {
document.write("yay " + name2 + "<b> wins!</b>");
} else {
document.write("tie...");
}
} else if (user1 === "scissors") {
if (user2 === "scissors") {
document.write("tie...");
} else if (user2 === "paper") {
document.write("yay " + name1 + "<b> wins!</b>");
} else {
document.write("yay " + name2 + "<b> wins!</b>");
}
} else {
document.write("wut");
}
var playAgain = confirm("Play Again?");
if(playAgain) {
// just run the game again
playGame();
}
}
// ask for their names
var name1 = prompt("what is your name?");
var name2 = prompt("what is your name?");
// run the first game
playGame();
下一步是将提示移动到屏幕上的<input>
元素,因为弹出提示可怕。
答案 1 :(得分:0)
你只需将步骤3-6放在一个循环中,然后在这个循环中添加一个新步骤,然后打印出来#34;要继续按任意键&#34;并等待用户输入。