【发布时间】:2015-09-02 18:22:03
【问题描述】:
我创建了一个简单的 javascript 石头、纸、剪刀、蜥蜴、spock 游戏。我遇到的问题是:
var computerChoice 在每次页面加载时设置一次。多次按下按钮保持不变,直到页面被刷新。
赢/输/平计数不更新。
我的代码:
<h1>Rock, Paper, Scissors, Lizard, Spock</h1><br>
<div id="user-choice">
<button id="Rock" value="Rock" onclick="choose('Rock')"><i class="fa fa-hand-rock-o fa-3x"></i></button>
<button id="Paper" value="Paper" onclick="choose('Paper')"><i class="fa fa-hand-paper-o fa-3x"></i></button>
<button id="Scissors" value="Scissors" onclick="choose('Scissors')"><i class="fa fa-hand-scissors-o fa-3x"></i></button>
<button id="Lizard" value="Lizard" onclick="choose('Lizard')"><i class="fa fa-hand-lizard-o fa-3x"></i></button>
<button id="Spock" value="Spock" onclick="choose('Spock')"><i class="fa fa-hand-spock-o fa-3x"></i></button>
</div>
<div id="results">
<br>
<br>
<span id="win"></span><br>
<span id="lose"></span><br>
<span id="tie"></span><br>
<script>
var win = 0
var lose = 0
var tie = 0
document.getElementById("win").textContent = 'Wins: ' + win
document.getElementById("lose").textContent = 'Losses: ' + lose
document.getElementById("tie").textContent ='Ties: ' + tie
</script>
<script>
var win = 0
var lose = 0
var tie = 0
var computerChoice = Math.random();
if (computerChoice <= 0.2) {
computerChoice = "Rock";
} else if (computerChoice <= 0.4) {
computerChoice = "Paper";
} else if (computerChoice <= 0.6) {
computerChoice = "Scissors";
} else if (computerChoice <= 0.8) {
computerChoice = "Lizard";
} else {
computerChoice = "Spock";
}
var playerChoice;
function choose(choice){
playerChoice = choice;
alert("I chose " + computerChoice + ".");
// Rock Outcomes
if (playerChoice == computerChoice) {
alert("It's a tie!");
tie++;
} else if (playerChoice == 'Rock' && computerChoice == 'Paper') {
alert(computerChoice + ' covers ' + playerChoice + '. I win!');
lose++;
} else if (playerChoice == 'Rock' && computerChoice == 'Spock') {
alert(computerChoice + ' vaporises ' + playerChoice + '. I win!');
lose++;
} else if (playerChoice == 'Rock' && computerChoice == 'Lizard') {
alert(playerChoice + ' crushes ' + computerChoice + '. You win!');
win++;
} else if (playerChoice == 'Rock' && computerChoice == 'Scissors') {
alert(playerChoice + ' crushes ' + computerChoice + '. You win!');
win++;
}
// End Rock Outcomes
// Paper Outcomes
else if (playerChoice == 'Paper' && computerChoice == 'Scissors') {
alert(computerChoice + ' cuts ' + playerChoice + '. I win!');
lose++;
} else if (playerChoice == 'Paper' && computerChoice == 'Lizard') {
alert(computerChoice + ' eats ' + playerChoice + '. I win!');
lose++;
} else if (playerChoice == 'Paper' && computerChoice == 'Rock') {
alert(playerChoice + ' covers ' + computerChoice + '. You win!');
win++;
} else if (playerChoice == 'Paper' && computerChoice == 'Spock') {
alert(playerChoice + ' disproves ' + computerChoice + '. You win!');
win++;
}
// End Paper Outcomes
// Scissors Outcomes
else if (playerChoice == 'Scissors' && computerChoice == 'Rock') {
alert(computerChoice + ' crushes ' + playerChoice + '. I win!');
lose++;
} else if (playerChoice == 'Scissors' && computerChoice == 'Spock') {
alert(computerChoice + ' smashes ' + playerChoice + '. I win!');
lose++;
} else if (playerChoice == 'Scissors' && computerChoice == 'Paper') {
alert(playerChoice + ' cuts ' + computerChoice + '. You win!');
win++;
} else if (playerChoice == 'Scissors' && computerChoice == 'Lizard') {
alert(playerChoice + ' decapitates ' + computerChoice + '. You win!');
win++;
}
// End Scissors Outcomes
// Lizard Outcomes
else if (playerChoice == 'Lizard' && computerChoice == 'Rock') {
alert(computerChoice + ' crushes ' + playerChoice + '. I win!');
lose++;
} else if (playerChoice == 'Lizard' && computerChoice == 'Scissors') {
alert(computerChoice + ' decapitates ' + playerChoice + '. I win!');
lose++;
} else if (playerChoice == 'Lizard' && computerChoice == 'Paper') {
alert(playerChoice + ' eats ' + computerChoice + '. You win!');
win++;
} else if (playerChoice == 'Lizard' && computerChoice == 'Spock') {
alert(playerChoice + ' poisons ' + computerChoice + '. You win!');
win++;
}
// End Lizard Outcomes
// Spock Outcomes
else if (playerChoice == 'Spock' && computerChoice == 'Paper') {
alert(computerChoice + ' disproves ' + playerChoice + '. I win!');
lose++;
} else if (playerChoice == 'Spock' && computerChoice == 'Lizard') {
alert(computerChoice + ' poisons ' + playerChoice + '. I win!');
lose++;
} else if (playerChoice == 'Spock' && computerChoice == 'Rock') {
alert(playerChoice + ' vaporises ' + computerChoice + '. You win!');
win++;
} else if (playerChoice == 'Spock' && computerChoice == 'Scissors') {
alert(playerChoice + ' smashes ' + computerChoice + '. You win!');
win++;
}
// End Scissors Outcomes
}
</script>
<div id="results">
<br>
<br>
<span id="win"></span><br>
<span id="lose"></span><br>
<span id="tie"></span><br>
<script>
document.getElementById("win").textContent = 'Wins: ' + win
document.getElementById("lose").textContent = 'Losses: ' + lose
document.getElementById("tie").textContent ='Ties: ' + tie
</script>
【问题讨论】:
标签: javascript