<!DOCTYPE html>
<html>
  <head>
    <title>Rock Paper Scissors Online Game</title>
  </head>
  <body>
    <h1>Play Rock Paper Scissors</h1>
    <div>
      <p>Choose your weapon:</p>
      <button type="button" onclick="game('rock')">Rock</button>
      <button type="button" onclick="game('paper')">Paper</button>
      <button type="button" onclick="game('scissors')">Scissors</button>
    </div>
    <script>
      function computerPlay() {
        let choices = ['rock', 'paper', 'scissors'];
        let index = Math.floor(Math.random() * choices.length);
        return choices[index];
      }
<pre><code>  function game(playerSelection) {
    let computerSelection = computerPlay();
    let winner;
    if (playerSelection == computerSelection) {
      winner = 'draw';
    } else if (
      (playerSelection == 'rock' &amp;&amp; computerSelection == 'scissors') ||
      (playerSelection == 'scissors' &amp;&amp; computerSelection == 'paper') ||
      (playerSelection == 'paper' &amp;&amp; computerSelection == 'rock')
    ) {
      winner = 'player';
    } else {
      winner = 'computer';
    }

    alert(
      'You chose ' + playerSelection + ' and the computer chose ' + computerSelection + '. You ' + (winner == 'player' ? 'win!' : winner == 'computer' ? 'lose!' : 'draw!')
    );
  }
&lt;/script&gt;
</code></pre>
  </body>
</html>
Play Rock Paper Scissors Online - Simple HTML Game

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

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