<html>
  <head>
    <title>Rock Paper Scissors Online - Free Game</title>
  </head>
  <body>
    <h1>Rock Paper Scissors</h1>
    <h2>Choose one:</h2>
    <div>
      <button onClick="play('rock')">Rock</button>
      <button onClick="play('paper')">Paper</button>
      <button onClick="play('scissors')">Scissors</button>
    </div>
    <div id="result"></div>
    <script>
      function play(userChoice) {
        const computerChoice = Math.random();
        if (computerChoice < 0.34) {
          computerChoice = 'rock';
        } else if (computerChoice <= 0.67) {
          computerChoice = 'paper';
        } else {
          computerChoice = 'scissors';
        }
        console.log('Computer: ' + computerChoice);
<pre><code>    const compare = (userChoice, computerChoice) =&gt; {
      if (userChoice === computerChoice) {
        return 'The result is a tie!';
      } else if (userChoice === 'rock') {
        if (computerChoice === 'scissors') {
          return 'You win!';
        } else {
          return 'The computer wins!';
        }
      } else if (userChoice === 'paper') {
        if (computerChoice === 'rock') {
          return 'You win!';
        } else {
          return 'The computer wins!';
        }
      } else if (userChoice === 'scissors') {
        if (computerChoice === 'paper') {
          return 'You win!';
        } else {
          return 'The computer wins!';
        }
      }
    };

    document.getElementById('result').innerHTML = compare(
      userChoice,
      computerChoice
    );
  }
&lt;/script&gt;
</code></pre>
  </body>
</html>

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

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