gpt4 book ai didi

javascript - Javascript 石头、剪刀、布等游戏的问题

转载 作者:行者123 更新时间:2023-12-03 11:29:37 26 4
gpt4 key购买 nike

我遇到了一个问题,选择根本没有更新。我已经列出了每次选择后应显示最后结果和当前分数的位置。分数运行良好,但选择根本没有更新。谢谢。

<html>
<head>
<title> Dynamic Web </title>
</head>

<body>


<div class="container">

<a class="contents content1">

<h1>Squirtle, Charmander, Bulbasuar</h1>

<div class="p-r-s">

<div class="one_three">

<h3>Take your pick</h3>

<ul class="choices">
<li>
<a onclick="compare('Squirtle', computerChoice)">
<img src="assets/img/paper.png" width="50px" />
</a>
</li>

<li>
<a onclick="compare('Charmander', computerChoice)">
<img src="assets/img/rock.png" width="50px" />
</a>
</li>

<li>
<a onclick="compare('Bulbasuar', computerChoice)">
<img src="assets/img/scissors.png" width="50px" />
</a>
</li>
</ul>

</div> <!-- one_three -->

<div>

<h3>Scores</h3>

<div class="scores">

<div class="score-box">

<div id="playerScore"></div><!-- .computerScore -->

<span>Player</span>

</div><!-- score-box -->

<div class="score-box">

<div id="computerScore"></div><!-- .computerScore -->

<span>Computer</span>

</div><!-- score-box -->

</div><!-- .scores -->

</div> <!-- two_three -->


<div>

<h3>Choices</h3>

<ul class="decider">

<li>

<span>Player:</span>
<span id="playerChoice">Pick one to get started!</span><!-- .playerChoice -->

</li>

<li>

<span>Computer:</span>
<span id="computerChoice">You first!</span><!-- .computerChoice -->

</li>


</ul>

</div>

</div> <!-- .row -->

Javascript

<script type="text/javascript" >

var computerScore = 0
var playerScore = 0

// INSERT SCORES
var playerScoreBox = document.getElementById('playerScore');
var computerScoreBox = document.getElementById('computerScore');

playerScoreBox.innerHTML = computerScore;
computerScoreBox.innerHTML = playerScore;

var playerChoice = document.getElementById('playerChoice');
var computerChoice = document.getElementById('computerChoice')


function compare(choice1, choice2) {

choice2 = Math.random();
if (choice2 < 0.34) {
choice2 = "Charmander";
} else if(choice2 <= 0.67) {
choice2 = "Squirtle";
} else {
choice2 = "Bulbasuar";
}

playerChoice = choice1;
computerChoice = choice2;


if (choice1 == choice2) {
return false;
}
if (choice1 == "Charmander") {
if (choice2 == "Bulbasuar") {

playerScore++;
}
else {
computerScore++;
}
return updateScores();
}
if (choice1 == "Squirtle") {
if (choice2 == "Charmander") {
playerScore++;
}
else {
computerScore++;
}
return updateScores();
}
if (choice1 == "Bulbasuar") {

if (choice2 == "Charmander") {
computerScore++;
}
else {
playerScore++;
}
return updateScores();
}
}

function updateScores() {
playerScoreBox.innerHTML = playerScore;
computerScoreBox.innerHTML = computerScore;
}

</script>

最佳答案

playerChoice = choice1; 应该是 playerChoice.value = choice1;playerChoice.innerHTML = choice1;,具体取决于它是输入还是不是。与 computerChoice 相同。

关于javascript - Javascript 石头、剪刀、布等游戏的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26793139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com