岩石,纸张,sciccors,蜥蜴,spock javascript/html游戏

rock, paper, sciccors, lizard, spock javascript/html game

本文关键字:html 游戏 javascript 蜥蜴 纸张 sciccors 岩石 spock      更新时间:2023-09-26

我想制作《生活大爆炸》中的摇滚、纸张、科幻、蜥蜴、斯波克游戏。我在html中使用了javascript。

    <button type='button' onclick='playGameame()'>Lets start!</button>
    <p id='userChoice'>You chose:</p>
    <p id='aiChoice'>Your friendly pc chose:</p>
    <p id='result'>The result is:</p>
        <script>
            function playGame(){
                var userChoice = prompt('What will be you choice of weapon?', 'rock, paper, scissors, lizard or spock'); 
                var aiChoice = Math.random();
                if (aiChoice < 0.2 ){
                    aiChoice = 'rock';
                } else if(aiChoice <= 0.4){
                    aiChoice = 'paper';
                } else if(aiChoice <= 0.6){
                    aiCoice = 'sciccors';
                } else if(aiChoice <= 0.8){
                    aiChoice = 'lizard';
                } else{
                    aiChoice = 'spock';
                }
                document.getElementById('userChoice').innerHTML = 'You chose: ' + userChoice;
                document.getElementById('aiChoice').innerHTML = 'Your friendly pc chose: ' + aiChoice;
                if (userChoice === aiChoice){
                    result = 'It''s a tie!';
                } 
                <!-- ROCK -->
                else if (userChoice === 'rock'){
                    if (aiChoice === 'paper' || 'spock'){
                        result = 'You lose!';
                    } else {
                        result = 'You win!';
                    }
                }
                <!-- PAPER -->
                else if (userChoice === 'paper'){
                    if (aiChoice === 'lizard' || 'sciccors'){
                        result = 'You lose!';
                    } else {
                        result = 'You win!';
                    }
                }
                <!-- SCICCORS -->
                else if (userChoice === 'sciccors'){
                    if (aiChoice === 'spock' || 'rock'){
                        result = 'You lose!';
                    } else {
                        result = 'You win!';
                    }
                }
                <!-- LIZARD -->
                else if (userChoice === 'lizard'){
                    if (aiChoice === 'rock' || 'sciccors'){
                        result = 'You lose!';
                    } else {
                        result = 'You win!';
                    }
                }
                <!-- SPOCK -->
                else if (userChoice === 'spock'){
                    if (aiChoice === 'paper' || 'lizard'){
                        result = 'You lose!';
                    } else {
                        result = 'You win!';
                    }
                }
                var result = 
                document.getElementById('result').innerHTML = 'The result is: ' + result;
            }
        </script>

问题是,如果我点击浏览器中的按钮调用该函数,我得到的唯一结果是"结果是:你输了!"或"结果是,打成平手!"。例如,当userChoise是岩石,aiChoise是蜥蜴时,结果应该是"你赢了!",但实际上它会返回"你输了!"。

有人能帮我吗?我将不胜感激。

祝你今天愉快。

您使用OR不正确。您应该这样重复您的变量:
if (aiChoice == 'paper' || aiChoice == 'spock')
出现此问题是因为以下形式的语句:

'paper' || 'spock'

被评估为true。