Play Rock, Paper, Scissors: Simple HTML Game - Online & Free
Play Rock, Paper, Scissors!
<div>
<h2>Instructions:</h2>
<p>Choose one of the three options: Rock, Paper, or Scissors. Click the button corresponding to your choice. The computer will select one of the three options as well. The winner will be determined by the following rules:</p>
<ul>
<li><b>Rock</b> beats <b>Scissors</b></li>
<li><b>Scissors</b> beats <b>Paper</b></li>
<li><b>Paper</b> beats <b>Rock</b></li>
</ul>
</div>
<div>
<input type="button" value="Rock" onclick="playGame('Rock')">
<input type="button" value="Paper" onclick="playGame('Paper')">
<input type="button" value="Scissors" onclick="playGame('Scissors')">
</div>
<div>
<p id="result"></p>
</div>
<script>
function playGame(userChoice) {
const choices = ['Rock', 'Paper', 'Scissors'];
let computerChoice = choices[Math.floor(Math.random() * 3)];
let result;
if (computerChoice === userChoice) {
result = 'It's a tie!';
} else if (computerChoice === 'Rock' && userChoice === 'Paper') {
result = 'You win!';
} else if (computerChoice === 'Paper' && userChoice === 'Scissors') {
result = 'You win!';
} else if (computerChoice === 'Scissors' && userChoice === 'Rock') {
result = 'You win!';
} else {
result = 'Computer wins!';
}
document.getElementById('result').innerHTML = `You chose ${userChoice}, computer chose ${computerChoice}. ${result}`;
}
</script>
原文地址: https://www.cveoy.top/t/topic/lmj9 著作权归作者所有。请勿转载和采集!