Play Rock Paper Scissors - HTML Game
<html>
<head>
<title>My HTML Game</title>
<script>
function startGame() {
var userChoice = prompt('Do you choose rock, paper or scissors?');
if (! userChoice) {
document.write('<p>You didn't choose anything! Refresh the page to try again.</p>');
return;
}
userChoice = userChoice.toLowerCase();
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = 'rock';
} else if(computerChoice <= 0.67) {
computerChoice = 'paper';
} else {
computerChoice = 'scissors';
}
<pre><code> document.write('<p>You chose <strong>' + userChoice + '</strong> and I chose <strong>' + computerChoice + '</strong>.</p>');
var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return 'It's a tie!';
}
if (choice1 === 'rock') {
if (choice2 === 'scissors') {
return 'You win!';
} else {
return 'I win!';
}
}
if (choice1 === 'paper') {
if (choice2 === 'rock') {
return 'You win!';
} else {
return 'I win!';
}
}
if (choice1 === 'scissors') {
if (choice2 === 'rock') {
return 'I win!';
} else {
return 'You win!';
}
}
};
var results = compare(userChoice, computerChoice);
document.write('<p><strong>' + results + '</strong></p>');
}
</script>
</code></pre>
</head>
<body>
<h2>Let's play Rock, Paper, Scissors!</h2>
<button type="button" onclick="startGame()">Start Game</button>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lms7 著作权归作者所有。请勿转载和采集!