Play Rock Paper Scissors Online - Fun Game
<!DOCTYPE html>
<html>
<head>
<title>Play Rock Paper Scissors Online - Fun Game</title>
</head>
<body>
<h1>Rock Paper Scissors Game</h1>
<form>
<input type="radio" name="choice" value="rock">Rock
<input type="radio" name="choice" value="paper">Paper
<input type="radio" name="choice" value="scissors">Scissors<br>
<input type="submit" value="Submit" id="submit">
</form>
<div id="result" style="display:none;"></div>
<script>
document.getElementById('submit').addEventListener('click', function(e){
e.preventDefault();
let choice = document.querySelector('input[name="choice"]:checked').value;
let compChoice = getCompChoice();
let result;
if (choice === compChoice) {
result = 'It's a tie!';
} else if (choice === 'rock') {
result = compChoice === 'paper' ? 'You lose' : 'You win';
} else if (choice === 'paper') {
result = compChoice === 'scissors' ? 'You lose' : 'You win';
} else if (choice === 'scissors') {
result = compChoice === 'rock' ? 'You lose' : 'You win';
}
document.getElementById('result').innerHTML = `You chose ${choice}, the computer chose ${compChoice}. ${result}`;
document.getElementById('result').style.display = 'block';
});
function getCompChoice(){
let choices = ['rock', 'paper', 'scissors'];
return choices[Math.floor(Math.random()*3)];
}
</script>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmnE 著作权归作者所有。请勿转载和采集!