Play Rock Paper Scissors Online - Simple HTML Game
<html>
<head>
<title> Rock, Paper, Scissors </title>
</head>
<body>
<h1> Rock, Paper, Scissors </h1>
<p> Choose your weapon: </p>
<form>
<input type = "radio" name="weapon" value="rock">Rock
<input type = "radio" name="weapon" value="paper">Paper
<input type = "radio" name="weapon" value="scissors">Scissors
<input type = "submit" value="Commit Choice">
</form>
<script>
let userChoice;
let computerChoice;
let possibleChoices = ['rock', 'paper', 'scissors'];
<pre><code> document.querySelector("form").addEventListener("submit", (e) => {
e.preventDefault();
userChoice = document.querySelector('input[name="weapon"]:checked').value;
computerChoice = possibleChoices[Math.floor(Math.random() * 3)];
if (userChoice === computerChoice) {
alert("It's a tie!");
}
else if (userChoice === "rock" && computerChoice === "paper") {
alert("Computer wins!");
}
else if (userChoice === "rock" && computerChoice === "scissors") {
alert("You win!");
}
else if (userChoice === "paper" && computerChoice === "rock") {
alert("You win!");
}
else if (userChoice === "paper" && computerChoice === "scissors") {
alert("Computer wins!");
}
else if (userChoice === "scissors" && computerChoice === "rock") {
alert("Computer wins!");
}
else if (userChoice === "scissors" && computerChoice === "paper") {
alert("You win!");
}
});
</script>
</body>
</code></pre>
</html>
原文地址: https://www.cveoy.top/t/topic/lmnj 著作权归作者所有。请勿转载和采集!