<html>
  <head>
    <title>Rock Paper Scissors - Play Online</title>
  </head>
  <body>
    <h1>Let's play Rock Paper Scissors!</h1>
    <p>Select your weapon:</p>
    <form>
      <input type="radio" name="choice" value="rock">Rock<br>
      <input type="radio" name="choice" value="paper">Paper<br>
      <input type="radio" name="choice" value="scissors">Scissors<br>
      <input type="submit" value="Play!">
    </form>
    <script>
      const form = document.querySelector("form");
      form.addEventListener("submit", function (event) {
        event.preventDefault();
        const choice = form.querySelector("input[name=choice]:checked").value;
<pre><code>    const computerChoice = Math.random();
    if (computerChoice &lt; 0.34) {
      computerChoice = 'rock';
    } else if (computerChoice &lt;= 0.67) {
      computerChoice = 'paper';
    } else {
      computerChoice = 'scissors';
    }

    let result;
    if (computerChoice === choice) {
      result = 'It's a tie!';
    } else if (computerChoice === 'rock') {
      if (choice === 'paper') {
        result = 'You win!';
      } else {
        result = 'Computer wins';
      }
    } else if (computerChoice === 'paper') {
      if (choice === 'scissors') {
        result = 'You win!';
      } else {
        result = 'Computer wins';
      }
    } else if (computerChoice === 'scissors') {
      if (choice === 'rock') {
        result = 'You win!';
      } else {
        result = 'Computer wins';
      }
    }

    alert(`You chose ${choice} and the computer chose ${computerChoice}. ${result}`);
  });
&lt;/script&gt;
</code></pre>
  </body>
</html>
Play Rock Paper Scissors Online - Simple Game

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

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