Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                    
                
        

我的游戏正常进行,除非我在涉及蜥蜴或斯波克的情况下获胜,但我不知道为什么,因为当我输掉比赛时,我就输了,这是正确的。我试图寻找错别字,但找不到任何错别字。如果有人看到错了,我将不胜感激。

这是一个小提琴,所以您可以看到问题: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!';


这段代码仅使用switch来检查您是否赢了。然后,最后设置该值。

由于如果您滚动“摇滚”而对手滚动“剪刀”或“蜥蜴”则您赢了,因此我结合了这些说法。

您可以通过仅检查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,您就赢了。

09-25 16:45