Play Rock Paper Scissors Online - Simple HTML Game
<!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' && computerSelection == 'scissors') ||
(playerSelection == 'scissors' && computerSelection == 'paper') ||
(playerSelection == 'paper' && computerSelection == 'rock')
) {
winner = 'player';
} else {
winner = 'computer';
}
alert(
'You chose ' + playerSelection + ' and the computer chose ' + computerSelection + '. You ' + (winner == 'player' ? 'win!' : winner == 'computer' ? 'lose!' : 'draw!')
);
}
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmnI 著作权归作者所有。请勿转载和采集!