<html>
<head>
    <title>Rock Paper Scissors</title>
</head>
<body>
    <h1>Rock Paper Scissors</h1>
    <div>
        <button id='rock'>Rock</button>
        <button id='paper'>Paper</button>
        <button id='scissors'>Scissors</button>
    </div>
    <div id='result'></div>
    <script>
        let choices = ['Rock', 'Paper', 'Scissors'];
        let userChoice;
        let computerChoice;
        let result;
<pre><code>    document.getElementById('rock').onclick = userPick;
    document.getElementById('paper').onclick = userPick;
    document.getElementById('scissors').onclick = userPick;

    function userPick() {
        userChoice = this.innerHTML;
        computerChoice = choices[Math.floor(Math.random() * choices.length)];
        compare();
    }

    function compare() {
        if (userChoice === computerChoice) {
            result = 'It's a tie!';
        } else if (userChoice === 'Rock' &amp;&amp; computerChoice === 'Paper') {
            result = 'Computer wins!';
        } else if (userChoice === 'Rock' &amp;&amp; computerChoice === 'Scissors') {
            result = 'You win!';
        } else if (userChoice === 'Paper' &amp;&amp; computerChoice === 'Rock') {
            result = 'You win!';
        } else if (userChoice === 'Paper' &amp;&amp; computerChoice === 'Scissors') {
            result = 'Computer wins!';
        } else if (userChoice === 'Scissors' &amp;&amp; computerChoice === 'Rock') {
            result = 'Computer wins!';
        } else if (userChoice === 'Scissors' &amp;&amp; computerChoice === 'Paper') {
            result = 'You win!';
        }
        document.getElementById('result').innerHTML = 'You chose ' + userChoice + ' and the computer chose ' + computerChoice + '&lt;br&gt;' + result;
    }
&lt;/script&gt;
</code></pre>
</body>
</html>
Play Rock Paper Scissors Online - Free Game

原文地址: https://www.cveoy.top/t/topic/lmn8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录