我当前正在创建一个骰子游戏,您掷两个骰子,然后javascript决定您是输了还是被计算机绑了。这是简单的代码,但是我不确定我是否犯了语法错误或代码本身。
我已经尝试过查看代码,但是我对此并不陌生,有些专业人士会有所帮助。
<!DOCTYPE html>
<html>
<head>
<title> Game of dice </title>
<script>
var player;
var computer;
function randDice() {
var number = randNum(1,6);
return number;
}
function rollDice(){
player = randDice();
computer = randDice();
if (player == 1) {
document.getElementById("dieOne").src= "die1.png"
} else if (player == 2) {
document.getElementById("dieOne").src= "die2.png"
} else if (player == 3) {
document.getElementById("dieOne").src= "die3.png"
} else if (player == 4) {
document.getElementById("dieOne").src= "die4.png"
} else if (player == 5) {
document.getElementById("dieOne").src= "die5.png"
} else (player == 6) {
document.getElementById("dieOne").src= "die6.png"
if (computer == 1) {
document.getElementById("dieTwo").src= "die1.png"
} else if (computer == 2) {
document.getElementById("dieTwo").src= "die2.png"
} else if (computer == 3) {
document.getElementById("dieTwo").src= "die3.png"
} else if (computer == 4) {
document.getElementById("dieTwo").src= "die4.png"
} else if (computer == 5) {
document.getElementById("dieTwo").src= "die5.png"
} else (computer == 6) {
document.getElementById("dieTwo").src= "die6.png"
checkWin();
}
function checkWin(){
if (player == computer){
document.getElementById("winner").innerHTML = "You tied";
} else if (player > computer){
document.getElementById("winner").innerHTML = "You-won!";
} else (player < computer){
document.getElementById("winner").innerHTML = "You-lost";
}
}
</script>
</head>
<body>
<h1> Die roll </h1>
<img src="die1.png" id="dieOne"><image>
<img src="die2.png" id="dieTwo"><image>
<br>
<br>
<button onClick="rollDice();">Roll</button>
<br>
<p id="winner"></p>
</body>
</html>
答案 0 :(得分:2)
您的源代码中存在一些错误:
else (player == 6)
您可以按F12键并在控制台行中查看代码行中的详细错误消息。
<!DOCTYPE html>
<html>
<head>
<title> Game of dice </title>
</head>
<body>
<h1> Die roll </h1>
<img src="die1.png" id="dieOne"><image>
<img src="die2.png" id="dieTwo"><image>
<br>
<br>
<button onClick="rollDice();">Roll</button>
<br>
<p id="winner"></p>
<script>
var player;
var computer;
function randDice() {
var number = randNum(1,6);
return number;
}
function rollDice(){
player = randDice();
computer = randDice();
if (player == 1) {
document.getElementById("dieOne").src= "die1.png"
} else if (player == 2) {
document.getElementById("dieOne").src= "die2.png"
} else if (player == 3) {
document.getElementById("dieOne").src= "die3.png"
} else if (player == 4) {
document.getElementById("dieOne").src= "die4.png"
} else if (player == 5) {
document.getElementById("dieOne").src= "die5.png"
} else if(player == 6) {
document.getElementById("dieOne").src= "die6.png"
}
if (computer == 1) {
document.getElementById("dieTwo").src= "die1.png"
} else if (computer == 2) {
document.getElementById("dieTwo").src= "die2.png"
} else if (computer == 3) {
document.getElementById("dieTwo").src= "die3.png"
} else if (computer == 4) {
document.getElementById("dieTwo").src= "die4.png"
} else if (computer == 5) {
document.getElementById("dieTwo").src= "die5.png"
} else if(computer == 6) {
document.getElementById("dieTwo").src= "die6.png"
checkWin();
}
}
function checkWin(){
if (player == computer){
document.getElementById("winner").innerHTML = "You tied";
} else if (player > computer){
document.getElementById("winner").innerHTML = "You-won!";
} else if(player < computer){
document.getElementById("winner").innerHTML = "You-lost";
}
}
</script>
</body>
</html>
答案 1 :(得分:0)
在这是我的代码之前,我已经完成了此游戏
<html>
<head>
<script>
var roll1;
var roll2;
function rollTheDice()
{
roll1 = Math.floor(Math.random() * 6) + 1;
document.getElementById('imgDice').src = roll1 + ".jpg";
return roll1;
}
function rollTheDice2()
{
roll2 = Math.floor(Math.random() * 6) + 1;
document.getElementById('imgDice2').src = roll2 + "a.jpg";
return roll2;
}
function compareDice()
{
if (roll1 < roll2 ) {alert("You Loose")
;
} else if (roll1 > roll2 ) {
alert ("You Win")
;
} else {alert ("You Tie")
;
}
}
</script>
</head>
<body>
<input type="button"
onclick="rollTheDice();"
value="Get Your Die"/>
<img src="" id="imgDice"/>
<br>
<input type="button"
onclick="rollTheDice2();"
value="Choose Opponent's Die"/>
<img src="" id="imgDice2"/>
<br>
<input type="button"
onclick="compareDice();"
value="Who wins?"/>
<img src="" id="imgDice"/>
</body>
</html>