Play Rock Paper Scissors Online - Free Game
<html>
<head>
<title>Rock Paper Scissors</title>
</head>
<body>
<h1>Rock Paper Scissors</h1>
<div>
<button id='rock'>Rock</button>
<button id='paper'>Paper</button>
<button id='scissors'>Scissors</button>
</div>
<div id='result'></div>
<script>
let choices = ['Rock', 'Paper', 'Scissors'];
let userChoice;
let computerChoice;
let result;
<pre><code> document.getElementById('rock').onclick = userPick;
document.getElementById('paper').onclick = userPick;
document.getElementById('scissors').onclick = userPick;
function userPick() {
userChoice = this.innerHTML;
computerChoice = choices[Math.floor(Math.random() * choices.length)];
compare();
}
function compare() {
if (userChoice === computerChoice) {
result = 'It's a tie!';
} else if (userChoice === 'Rock' && computerChoice === 'Paper') {
result = 'Computer wins!';
} else if (userChoice === 'Rock' && computerChoice === 'Scissors') {
result = 'You win!';
} else if (userChoice === 'Paper' && computerChoice === 'Rock') {
result = 'You win!';
} else if (userChoice === 'Paper' && computerChoice === 'Scissors') {
result = 'Computer wins!';
} else if (userChoice === 'Scissors' && computerChoice === 'Rock') {
result = 'Computer wins!';
} else if (userChoice === 'Scissors' && computerChoice === 'Paper') {
result = 'You win!';
}
document.getElementById('result').innerHTML = 'You chose ' + userChoice + ' and the computer chose ' + computerChoice + '<br>' + result;
}
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmn8 著作权归作者所有。请勿转载和采集!