Play Rock Paper Scissors Online - Free Game
<html>
<head>
<title>Play Rock Paper Scissors Online - Free Game</title>
</head>
<body>
<h1>Rock Paper Scissors</h1>
<h2>Choose your weapon!</h2>
<p><button onclick="playGame('rock')">Rock</button>
<button onclick="playGame('paper')">Paper</button>
<button onclick="playGame('scissors')">Scissors</button></p>
<p>You chose: <span id="userChoice"></span></p>
<p>The computer chose: <span id="computerChoice"></span></p>
<p>The result is: <span id="result"></span></p>
<script>
function playGame(userChoice) {
document.getElementById("userChoice").innerHTML = userChoice;
let computerChoice;
let randomNumber = Math.random();
if (randomNumber < 0.34) {
computerChoice = 'rock';
} else if (randomNumber <= 0.67) {
computerChoice = 'paper';
} else {
computerChoice = 'scissors';
}
document.getElementById("computerChoice").innerHTML = computerChoice;
if (userChoice === computerChoice) {
document.getElementById("result").innerHTML = "It's a 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 = "Computer wins!";
}
}
</script>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmnS 著作权归作者所有。请勿转载和采集!