<html>
  <head>
    <title>Rock Paper Scissors Online Game</title>
    <script>
      // Create a function to get the user input
      function getUserChoice() {
        var userChoice = prompt('Do you choose rock, paper or scissors?');
        // Make sure user input is valid
        if (userChoice != 'rock' && userChoice != 'paper' && userChoice != 'scissors') {
          alert('Invalid choice, please try again');
          return getUserChoice();
        }
        return userChoice;
      }
<pre><code>  // Create a function to get the computer input
  function getComputerChoice() {
    var computerChoice = Math.random();
    if (computerChoice &lt;= 0.33) {
      return 'rock';
    } else if (computerChoice &lt;= 0.66) {
      return 'paper';
    } else {
      return 'scissors';
    }
  }

  // Create a function to determine winner
  function determineWinner(userChoice, computerChoice) {
    if (userChoice === computerChoice) {
      return 'It's a tie!';
    } else if (userChoice === 'rock') {
      if (computerChoice === 'paper') {
        return 'Computer wins!';
      } else {
        return 'You win!';
      }
    } else if (userChoice === 'paper') {
      if (computerChoice === 'scissors') {
        return 'Computer wins!';
      } else {
        return 'You win!';
      }
    } else if (userChoice === 'scissors') {
      if (computerChoice === 'rock') {
        return 'Computer wins!';
      } else {
        return 'You win!';
      }
    }
  }

  // Create a function to play the game
  function playGame() {
    var userChoice = getUserChoice();
    var computerChoice = getComputerChoice();
    alert('You chose ' + userChoice + ' and the computer chose ' + computerChoice + '.');
    alert(determineWinner(userChoice, computerChoice));
  }

&lt;/script&gt;
</code></pre>
  </head>
  <body>
    <h1>Rock Paper Scissors</h1>
    <button onclick='playGame()'>Play Game</button>
  </body>
</html>
Play Rock Paper Scissors Online - Simple HTML Game

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

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