Tic-Tac-Toe JavaScript帮助(我没有得到我想要的警报)
以下是我的井字游戏的html:Tic-Tac-Toe JavaScript帮助(我没有得到我想要的警报),javascript,Javascript,以下是我的井字游戏的html: <html> <head> <title>Tic Tac Toe</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <h1>Tic-Tac-Toe</h1> <div id="message"></div> <tabl
<html>
<head>
<title>Tic Tac Toe</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>Tic-Tac-Toe</h1>
<div id="message"></div>
<table border="3">
<tr>
<td class="Square" id="s1" onclick="nextMove(this);"></td>
<td class="Square" id="s2" onclick="nextMove(this);"></td>
<td class="Square" id="s3" onclick="nextMove(this);"></td>
</tr>
<tr>
<td class="Square" id="s4" onclick="nextMove(this);"></td>
<td class="Square" id="s5" onclick="nextMove(this);"></td>
<td class="Square" id="s6" onclick="nextMove(this);"></td>
</tr>
<tr>
<td class="Square" id="s7" onclick="nextMove(this);"></td>
<td class="Square" id="s8" onclick="nextMove(this);"></td>
<td class="Square" id="s9" onclick="nextMove(this);"></td>
</tr>
</table>
</div>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
在用户以各种可能的方式连续获得三个“O”或“X”而获胜后,我想要一个警告,说“祝贺你,X/Y!你赢了”
我在switchTurn
函数中做了一个if(checkforWinner(turn)){
并在那之后发出一个警报,上面写着警报(“恭喜,+turn+”!你赢了”);
。但是,当我测试我的代码时,在用户连续获得三个O或X后,警报不会弹出
代码是否真的有问题,或者我是否将代码放在了不应该放的地方?我完全不知道。我想你忘了返回:
function getBox(number) {
return document.getElementById("s" + number).innerText
}
这里有一个解决方案:
var turn = "X";
function setMessage(msg) {
document.getElementById("message").innerText = msg;
}
function nextMove(square) {
if (square.innerHTML == "X" || square.innerHTML == "O") {
alert('This space has already been selected');
} else {
square.innerText = turn;
switchTurn();
}
};
function switchTurn() {
if (checkforWinner(turn)) {
alert("Congratulations, " + turn + "! You win");
} else if (turn == "X") {
turn = "O";
setMessage("It's " + turn + "'s turn!");
} else {
turn = "X";
setMessage("It's " + turn + "'s turn!");
}
}
function checkforWinner() {
var result = false;
if (checkRow(1,2,3, turn) ||
checkRow(4,5,6, turn) ||
checkRow(7,8,9, turn) ||
checkRow(1,4,7, turn) ||
checkRow(2,5,8, turn) ||
checkRow(3,6,9, turn) ||
checkRow(1,5,9, turn) ||
checkRow(3,5,7, turn)) {
result = true;
}
return result;
}
function checkRow(a,b,c, move) {
var result = false;
if (getBox(a) === move && getBox(b) === move && getBox(c) === move) {
result = true;
}
return result;
}
function getBox(number) {
return document.getElementById("s" + number).innerHTML;
}
问题的很大一部分是getBox()没有返回任何内容,因此它是函数链上的未定义变量。此外,
turn
是一个全局变量,因此您可以使用它,而无需将其传递到checkforWinner()中除此之外,根据,不是每个浏览器支持。N
var turn = "X";
function setMessage(msg) {
document.getElementById("message").innerText = msg;
}
function nextMove(square) {
if (square.innerHTML == "X" || square.innerHTML == "O") {
alert('This space has already been selected');
} else {
square.innerText = turn;
switchTurn();
}
};
function switchTurn() {
if (checkforWinner(turn)) {
alert("Congratulations, " + turn + "! You win");
} else if (turn == "X") {
turn = "O";
setMessage("It's " + turn + "'s turn!");
} else {
turn = "X";
setMessage("It's " + turn + "'s turn!");
}
}
function checkforWinner() {
var result = false;
if (checkRow(1,2,3, turn) ||
checkRow(4,5,6, turn) ||
checkRow(7,8,9, turn) ||
checkRow(1,4,7, turn) ||
checkRow(2,5,8, turn) ||
checkRow(3,6,9, turn) ||
checkRow(1,5,9, turn) ||
checkRow(3,5,7, turn)) {
result = true;
}
return result;
}
function checkRow(a,b,c, move) {
var result = false;
if (getBox(a) === move && getBox(b) === move && getBox(c) === move) {
result = true;
}
return result;
}
function getBox(number) {
return document.getElementById("s" + number).innerHTML;
}