<html>
<head>
    <title>My HTML Game</title>
    <script>
        function startGame() {
            var userChoice = prompt('Do you choose rock, paper or scissors?');
            if (! userChoice) {
                document.write('<p>You didn't choose anything! Refresh the page to try again.</p>');
                return;
            }
            userChoice = userChoice.toLowerCase();
            var computerChoice = Math.random();
            if (computerChoice < 0.34) {
                computerChoice = 'rock';
            } else if(computerChoice <= 0.67) {
                computerChoice = 'paper';
            } else {
                computerChoice = 'scissors';
            }
<pre><code>        document.write('&lt;p&gt;You chose &lt;strong&gt;' + userChoice + '&lt;/strong&gt; and I chose &lt;strong&gt;' + computerChoice + '&lt;/strong&gt;.&lt;/p&gt;');

        var compare = function(choice1, choice2) {
            if (choice1 === choice2) {
                return 'It's a tie!';
            }
            if (choice1 === 'rock') {
                if (choice2 === 'scissors') {
                    return 'You win!';
                } else {
                    return 'I win!';
                }
            }
            if (choice1 === 'paper') {
                if (choice2 === 'rock') {
                    return 'You win!';
                } else {
                    return 'I win!';
                }
            }
            if (choice1 === 'scissors') {
                if (choice2 === 'rock') {
                    return 'I win!';
                } else {
                    return 'You win!';
                }
            }
        };
        var results = compare(userChoice, computerChoice);
        document.write('&lt;p&gt;&lt;strong&gt;' + results + '&lt;/strong&gt;&lt;/p&gt;');
    }
&lt;/script&gt;
</code></pre>
</head>
<body>
    <h2>Let's play Rock, Paper, Scissors!</h2>
    <button type="button" onclick="startGame()">Start Game</button>
</body>
</html>
Play Rock Paper Scissors - HTML Game

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

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