Play Rock Paper Scissors Online - Free Game
Rock Paper Scissors Game
Choose either rock, paper, or scissors to play against the computer!
<form action="">
<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="Submit">
</form>
<script>
let userChoice = document.querySelector('input[name="choice"]:checked').value;
let compChoice = Math.floor(Math.random() * 3);
if (compChoice == 0) {
compChoice = 'rock';
} else if (compChoice == 1) {
compChoice = 'paper';
} else {
compChoice = 'scissors';
}
if (userChoice == compChoice) {
alert('It's a tie!');
} else if (userChoice == 'rock' && compChoice == 'paper') {
alert('Computer Wins!');
} else if (userChoice == 'rock' && compChoice == 'scissors') {
alert('You Win!');
} else if (userChoice == 'paper' && compChoice == 'rock') {
alert('You Win!');
} else if (userChoice == 'paper' && compChoice == 'scissors') {
alert('Computer Wins!');
} else if (userChoice == 'scissors' && compChoice == 'rock') {
alert('Computer Wins!');
} else if (userChoice == 'scissors' && compChoice == 'paper') {
alert('You Win!');
}
</script>
原文地址: https://www.cveoy.top/t/topic/lmn4 著作权归作者所有。请勿转载和采集!