Rock Paper Scissors Game: Play Now!
<html>
<head>
<title>Rock Paper Scissors Game</title>
<script>
function rockPaperScissors(){
var userChoice = prompt('Do you choose rock, paper or scissors?');
if (userChoice == null || userChoice == '') {
alert('You need to make a choice!');
} else {
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = 'rock';
} else if(computerChoice <= 0.67) {
computerChoice = 'paper';
} else {
computerChoice = 'scissors';
}
<pre><code> var compare = function(choice1,choice2) {
if(choice1 == choice2) {
return 'It's a tie!';
}
if(choice1 == 'rock') {
if(choice2 == 'scissors') {
return 'You win!';
} else {
return 'You lose!';
}
}
if(choice1 == 'paper') {
if(choice2 == 'rock') {
return 'You win!';
} else {
return 'You lose!';
}
}
if(choice1 == 'scissors') {
if(choice2 == 'rock') {
return 'You lose!';
} else {
return 'You win!';
}
}
};
alert('You chose ' + userChoice + ' and the computer chose ' + computerChoice + '\n' + compare(userChoice, computerChoice));
}
};
</script>
</code></pre>
</head>
<body>
<h1>Rock Paper Scissors Game</h1>
<button onclick='rockPaperScissors()'>Play</button>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmnY 著作权归作者所有。请勿转载和采集!