我用 HTML、CSS 和 Javascript 制作了一个井字游戏。单击方框、显示消息以及其他所有操作都可以正常工作,但检查某人是否赢得了游戏则不行。
该代码不会生成错误,它只是不起作用。
这是我使用的代码:
var currentPlayer = "X";
var tds = document.querySelectorAll("td");
var playerDisplay = document.querySelector("#playerDisplay")
var warning = document.querySelector("#warning")
var winDisplay = document.querySelector("#win")
function switchPlayers() {
if(currentPlayer === "X") {
currentPlayer = "O";
playerDisplay.innerHTML = "O"
} else {
currentPlayer = "X";
playerDisplay.innerHTML = "X"
}
}
function checkWin() {
if (tds[0].textContent === tds[1].textContent === tds[2].textContent) {
return tds[0].textContent;
} else if (tds[3].textContent === tds[4].textContent ==tds[5].textContent){
return tds[3].textContent;
} else if (tds[6].textContent === tds[7].textContent === tds[8].textContent){
return tds[6].textContent;
} else if (tds[0].textContent === tds[3].textContent === tds[6].textContent){
return tds[0].textContent;
} else if (tds[1].textContent === tds[4].textContent === tds[7].textContent){
return tds[1].textContent;
} else if (tds[2].textContent === tds[5].textContent === tds[8].textContent){
return tds[2].textContent;
} else if (tds[0].textContent === tds[4].textContent === tds[8].textContent){
return tds[0].textContent;
} else if (tds[2].textContent === tds[4].textContent === tds[6].textContent){
return tds[2].textContent;
} else if (tds[0].textContent != "-" && tds[1].textContent != "-" && tds[2].textContent != "-" && tds[3].textContent != "-" && tds[4].textContent != "-" && tds[5].textContent != "-" && tds[6].textContent != "-" && tds[7].textContent != "-" && tds[8].textContent != "-"){
return -1;
} else {
return -2;
}
}
我只是不明白为什么它不起作用。:)
HUH函数
函数式编程
相关分类