Play Rock Paper Scissors Online - Simple HTML Game
<!DOCTYPE html>
<html>
<head>
<title>Play Rock Paper Scissors Online - Simple HTML Game</title>
<script>
//Get user choice from the 3 buttons
function getUserChoice(userInput) {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
console.log('Error! Please choose rock, paper, or scissors!');
}
}
//Get computer choice
function getComputerChoice() {
var randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
//Determine the winner
function determineWinner(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return 'It's a tie!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'Computer wins!';
} else {
return 'You win!';
}
}
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'Computer wins!';
} else {
return 'You win!';
}
}
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'Computer wins!';
} else {
return 'You win!';
}
}
}
//Start game
function playGame() {
var userChoice = getUserChoice('rock');
var computerChoice = getComputerChoice();
console.log('You chose ' + userChoice);
console.log('The computer chose ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
<pre><code></script>
</code></pre>
</head>
<body>
<h1>Play Rock Paper Scissors Online</h1>
<div>
<button onclick="playGame('rock')">Rock</button>
<button onclick="playGame('paper')">Paper</button>
<button onclick="playGame('scissors')">Scissors</button>
</div>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmq5 著作权归作者所有。请勿转载和采集!