gpt4 book ai didi

javascript - 为什么我玩石头、剪子、布、蜥蜴、史波克时没有说我赢了?

转载 作者:行者123 更新时间:2023-11-28 19:59:54 25 4
gpt4 key购买 nike

我的游戏运行良好,除了当涉及蜥蜴或史波克时我赢了,我不明白为什么,因为当我输了时,它显示我输了,这是正确的。我试图查找拼写错误,但没有找到。如果有人能看出问题所在,我将不胜感激。

这是一个 fiddle ,以便您可以看到问题:http://jsfiddle.net/5RGwv/

这是我的 HTML:

<div id="center">

<button id="button" onclick="play()">Play</button>
<p>You rolled:</p><span id="myroll"></span>
<p>Your opponent rolled:</p><span id="opproll"></span>

<p id="outcome"></p>
</div>

Javascript:

function play() {

var ROCK = 0;
var PAPER = 1;
var SCISSORS = 2;
var LIZARD = 3;
var SPOCK = 4;

var choices = ['rock', 'paper', 'scissors', 'lizard', 'spock'];

var myRoll = Math.floor(Math.random()*choices.length);
var opponentRoll = Math.floor(Math.random()*choices.length);

document.getElementById("myroll").innerHTML=choices[myRoll];
document.getElementById("opproll").innerHTML=choices[opponentRoll];

if (myRoll == opponentRoll) {
document.getElementById("outcome").innerHTML="It's a draw.";
return;
} //end of if

switch(myRoll) {

case ROCK:
document.getElementById("outcome").innerHTML=(opponentRoll == SCISSORS ? 'You win!' : 'You lose!');
return;

case ROCK:
document.getElementById("outcome").innerHTML=(opponentRoll == LIZARD ? 'You win!' : 'You lose!');
return;

case PAPER:
document.getElementById("outcome").innerHTML=(opponentRoll == ROCK ? 'You win!' : 'You lose!');
return;

case PAPER:
document.getElementById("outcome").innerHTML=(opponentRoll == SPOCK ? 'You win!' : 'You lose!');
return;

case SCISSORS:
document.getElementById("outcome").innerHTML=(opponentRoll == PAPER ? 'You win!' : 'You lose!');
return;

case SCISSORS:
document.getElementById("outcome").innerHTML=(opponentRoll == LIZARD ? 'You win!' : 'You lose!');
return;

case LIZARD:
document.getElementById("outcome").innerHTML=(opponentRoll == SPOCK ? 'You win!' : 'You lose!');
return;

case LIZARD:
document.getElementById("outcome").innerHTML=(opponentRoll == PAPER ? 'You win!' : 'You lose!');
return;

case SPOCK:
document.getElementById("outcome").innerHTML=(opponentRoll == SCISSORS ? 'You win!' : 'You lose!');
return;

case SPOCK:
document.getElementById("outcome").innerHTML=(opponentRoll == ROCK ? 'You win!' : 'You lose!');
return;

} //end of switch(myRoll)


} //end of play()

最佳答案

将您的 switch 语句替换为:

var result = false;
switch(myRoll) {
case ROCK:
result = opponentRoll == SCISSORS || opponentRoll == LIZARD;
break;
case PAPER:
result = opponentRoll == ROCK || opponentRoll == SPOCK;
break;
case SCISSORS:
result = opponentRoll == PAPER || opponentRoll == LIZARD;
break;
case LIZARD:
result = opponentRoll == SPOCK || opponentRoll == PAPER;
break;
case SPOCK:
result = opponentRoll == SCISSORS || opponentRoll == ROCK;
break;
}

document.getElementById("outcome").innerHTML = result ? 'You win!' : 'You lose!';

这段代码使用开关来检查您是否获胜。然后,最后设置该值。

既然如果你投“石头”而对手投“剪刀”或“蜥蜴”,你就赢了,所以我将这些陈述结合起来。

您可以通过仅检查 win 案例来进一步缩短代码,如下所示:

var result =  myRoll == ROCK     && (opponentRoll == SCISSORS || opponentRoll == LIZARD) ||
myRoll == PAPER && (opponentRoll == ROCK || opponentRoll == SPOCK) ||
myRoll == SCISSORS && (opponentRoll == PAPER || opponentRoll == LIZARD) ||
myRoll == LIZARD && (opponentRoll == PAPER || opponentRoll == SPOCK) ||
myRoll == SPOCK && (opponentRoll == ROCK || opponentRoll == SCISSORS);

document.getElementById("outcome").innerHTML = result ? 'You win!' : 'You lose!';

如果这 5 行中的任何一行为 true,您就赢了。

关于javascript - 为什么我玩石头、剪子、布、蜥蜴、史波克时没有说我赢了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21849581/

25 4 0
文章推荐: python - 将 args 转换为平面列表?
文章推荐: javascript - contenteditable
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com