Play Rock Paper Scissors Online - Free Game
<!DOCTYPE html>
<html>
<head>
<title>Rock Paper Scissors Online Game</title>
<style>
button {
font-size: 18px;
padding: 5px;
border-radius: 5px;
}
</style>
</head>
<body>
<h1>Rock Paper Scissors</h1>
<button onclick="play('rock')">Rock</button>
<button onclick="play('paper')">Paper</button>
<button onclick="play('scissors')">Scissors</button>
<p id="result"></p>
<script>
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function play(userChoice) {
var choices = ['rock', 'paper', 'scissors'];
var computerChoice = choices[getRandomInt(0, 2)];
document.getElementById('result').innerHTML =
'Computer chose ' + computerChoice + '<br>';
<pre><code> if (userChoice === computerChoice) {
document.getElementById('result').innerHTML += 'Tie!';
} else if (userChoice === 'rock' && computerChoice === 'scissors') {
document.getElementById('result').innerHTML += 'You win!';
} else if (userChoice === 'paper' && computerChoice === 'rock') {
document.getElementById('result').innerHTML += 'You win!';
} else if (userChoice === 'scissors' && computerChoice === 'paper') {
document.getElementById('result').innerHTML += 'You win!';
} else {
document.getElementById('result').innerHTML += 'You lose!';
}
}
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmnJ 著作权归作者所有。请勿转载和采集!