<html>
    <head>
        <title> Rock, Paper, Scissors </title> 
    </head>
    <body>
        <h1> Rock, Paper, Scissors </h1>
        <p> Choose your weapon: </p>
        <form>
            <input type = "radio" name="weapon" value="rock">Rock
            <input type = "radio" name="weapon" value="paper">Paper
            <input type = "radio" name="weapon" value="scissors">Scissors
            <input type = "submit" value="Commit Choice">
        </form>
        <script>
            let userChoice;
            let computerChoice;
            let possibleChoices = ['rock', 'paper', 'scissors'];
<pre><code>        document.querySelector(&quot;form&quot;).addEventListener(&quot;submit&quot;, (e) =&gt; {
            e.preventDefault();

            userChoice = document.querySelector('input[name=&quot;weapon&quot;]:checked').value;
            computerChoice = possibleChoices[Math.floor(Math.random() * 3)];

            if (userChoice === computerChoice) {
                alert(&quot;It's a tie!&quot;);
            }
            else if (userChoice === &quot;rock&quot; &amp;&amp; computerChoice === &quot;paper&quot;) {
                alert(&quot;Computer wins!&quot;);
            }
            else if (userChoice === &quot;rock&quot; &amp;&amp; computerChoice === &quot;scissors&quot;) {
                alert(&quot;You win!&quot;);
            }
            else if (userChoice === &quot;paper&quot; &amp;&amp; computerChoice === &quot;rock&quot;) {
                alert(&quot;You win!&quot;);
            }
            else if (userChoice === &quot;paper&quot; &amp;&amp; computerChoice === &quot;scissors&quot;) {
                alert(&quot;Computer wins!&quot;);
            }
            else if (userChoice === &quot;scissors&quot; &amp;&amp; computerChoice === &quot;rock&quot;) {
                alert(&quot;Computer wins!&quot;);
            }
            else if (userChoice === &quot;scissors&quot; &amp;&amp; computerChoice === &quot;paper&quot;) {
                alert(&quot;You win!&quot;);
            }
        });
    &lt;/script&gt;
&lt;/body&gt;
</code></pre>
</html>
Play Rock Paper Scissors Online - Simple HTML Game

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

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