【发布时间】:2016-08-03 23:11:31
【问题描述】:
我正在可汗学院编写一个剪刀石头布游戏,以便获得视觉效果,但 var compare = function(choice1, choice2) 无法正常工作。 html 它工作正常的想法。
插入了我的其余代码(请记住,视觉效果和按钮不是
background(0, 0, 0);
var userChoice = text("Do you choose rock, paper or scissors? Refresh to play again!", 25, 50); //starting text
var choice1 = userChoice;
var choice2 = computerChoice;
var winner = 25;
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} // Abover: computers choice, randomly chooses rock, paper or scissors
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
text("It is a draw!!! Try again!", winner, 50); // If both choices are the same it is a draw
}
};
if (choice1 === "rock") {
if (choice2 === "scissors") {
text("rock Wins!!!", winner, 50);
} else {
text("paper Wins!!!", winner, 50); // If the choices are rock and scissors, rock wins, if not then paper wins
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
text("paper Wins!!!", winner, 50);
} else {
if (choice2 === "scissors") {
text("scissors Wins!!!", winner, 50); // If the choices are rock and paper, paper wins, if not then scissors
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
text("rock Wins!!!", winner, 50);
} else {
if (choice2 === "paper") {
text("paper Wins!!!", winner, 50); // If the choices are scissors and rock, rock wins, if not then paper
}
}
}
}
}
// Above: compares the two choices to determine the winner, winner is rock, paper or scissors, not computer or user
text("User Choice: ", 20, 80 + userChoice);
text("Computer Choice: ", 20, 70 + computerChoice);
compare(userChoice, computerChoice); // Above: the message that tells the user who won
//Below: everything to do with the buttons
var squareW = 50;
var squareH = 50;
draw();
rect(75, 200, squareW, squareH); //left square
rect(175, 200, squareW, squareH); //middle square
rect(275, 200, squareW, squareH); //right square
【问题讨论】:
-
你的比较函数只测试它们是否相等 - 正确格式化你的代码,很明显发生了什么
-
我编辑了你的代码,我所做的只是正确缩进代码,所以你可以看到为什么比较函数不是你认为的那样
-
感谢我已修复它。 :)
标签: javascript var