Advertisement
TheiPhoneFan

Rock Paper Scissors Game

Jan 4th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.76 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Rock, Paper, Scissors Game</title>
  7.     <style>
  8.         body {
  9.             font-family: Arial, sans-serif;
  10.             text-align: center;
  11.         }
  12.         #result {
  13.             font-size: 1.2em;
  14.             margin-top: 10px;
  15.         }
  16.     </style>
  17. </head>
  18. <body>
  19.     <h1>Rock, Paper, Scissors Game</h1>
  20.     <p>Choose your move:</p>
  21.     <button onclick="playGame('rock')">Rock🪨</button>
  22.     <button onclick="playGame('paper')">Paper📄</button>
  23.     <button onclick="playGame('scissors')">Scissors✂️</button>
  24.     <p id="result"></p>
  25.  
  26.     <script>
  27.         function playGame(userChoice) {
  28.             // Array of possible moves
  29.             const moves = ['rock', 'paper', 'scissors'];
  30.  
  31.             // Get a random move for the computer
  32.             const computerChoice = moves[Math.floor(Math.random() * moves.length)];
  33.  
  34.             // Determine the winner
  35.             const result = determineWinner(userChoice, computerChoice);
  36.  
  37.             // Display the result
  38.             document.getElementById("result").innerHTML = `You chose ${userChoice}. Computer chose ${computerChoice}. ${result}`;
  39.         }
  40.  
  41.         function determineWinner(user, computer) {
  42.             if (user === computer) {
  43.                 return "It's a tie!";
  44.             } else if ((user === 'rock' && computer === 'scissors') ||
  45.                       (user === 'paper' && computer === 'rock') ||
  46.                       (user === 'scissors' && computer === 'paper')) {
  47.                return "You win!";
  48.             } else {
  49.                 return "Computer wins!";
  50.             }
  51.         }
  52.     </script>
  53. </body>
  54. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement