Play Rock Paper Scissors Online - Simple Game
<html>
<head>
<title>Rock Paper Scissors - Play Online</title>
</head>
<body>
<h1>Let's play Rock Paper Scissors!</h1>
<p>Select your weapon:</p>
<form>
<input type="radio" name="choice" value="rock">Rock<br>
<input type="radio" name="choice" value="paper">Paper<br>
<input type="radio" name="choice" value="scissors">Scissors<br>
<input type="submit" value="Play!">
</form>
<script>
const form = document.querySelector("form");
form.addEventListener("submit", function (event) {
event.preventDefault();
const choice = form.querySelector("input[name=choice]:checked").value;
<pre><code> const computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = 'rock';
} else if (computerChoice <= 0.67) {
computerChoice = 'paper';
} else {
computerChoice = 'scissors';
}
let result;
if (computerChoice === choice) {
result = 'It's a tie!';
} else if (computerChoice === 'rock') {
if (choice === 'paper') {
result = 'You win!';
} else {
result = 'Computer wins';
}
} else if (computerChoice === 'paper') {
if (choice === 'scissors') {
result = 'You win!';
} else {
result = 'Computer wins';
}
} else if (computerChoice === 'scissors') {
if (choice === 'rock') {
result = 'You win!';
} else {
result = 'Computer wins';
}
}
alert(`You chose ${choice} and the computer chose ${computerChoice}. ${result}`);
});
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmnW 著作权归作者所有。请勿转载和采集!