Rock Paper Scissors Game: Simple HTML Implementation

This is a basic implementation of the classic Rock Paper Scissors game using HTML.

HTML Code

<!DOCTYPE html>
<html>
<head>
	<title>Rock Paper Scissors Game</title>
	<script>
		function playGame(userChoice) {
			var computerChoice = Math.random();
			if (computerChoice < 0.34) {
				computerChoice = 'rock';
			} else if (computerChoice < 0.67) {
				computerChoice = 'paper';
			} else {
				computerChoice = 'scissors';
			}

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

			document.getElementById('result').innerHTML = result;
		}
	</script>
</head>
<body>
	<h1>Rock Paper Scissors Game</h1>
	<p>Choose your weapon:</p>
	<button onclick="playGame('rock')">Rock</button>
	<button onclick="playGame('paper')">Paper</button>
	<button onclick="playGame('scissors')">Scissors</button>
	<p id='result'></p>
</body>
</html>

How to Play

  1. Save the code as an HTML file (e.g., 'rockpaperscissors.html').
  2. Open the HTML file in a web browser.
  3. Click on the 'Rock', 'Paper', or 'Scissors' button to choose your weapon.
  4. The computer will randomly choose its weapon.
  5. The game will determine the winner and display the result.

Enjoy playing Rock Paper Scissors!

Rock Paper Scissors Game: Simple HTML Implementation

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

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