- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用 HTML、CSS 和 Javascript 制作了一个 tic tac toe 游戏。单击方框、显示消息以及其他所有操作都可以正常工作,但检查某人是否赢得了游戏则不行。
代码不会生成错误,只是不起作用。
这是我使用的代码:
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;
}
}
function game() {
for(var i = 0; i < tds.length; i++) {
tds[i].addEventListener("click", function(){
if(this.textContent === "-") {
this.textContent = currentPlayer;
if(checkWin() === "X") {
winDisplay.textContent = "X wins!!!";
return;
} else if(checkWin() === "O"){
winDisplay.textContent = "O wins!!!";
return;
} else if (checkWin() === -1) {
winDisplay.textContent = "Tied!!!"
return;
}
switchPlayers();
} else {
warning.textContent = "That block has already been filled, choose and empty one."
setTimeout(function(){
warning.textContent = "";
}, 1000);
}
});
}
}
game();
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Tic Tac Toe</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Open+Sans|Roboto&display=swap" rel="stylesheet">
</head>
<body>
<h1>Tic Tac Toe</h1>
<table>
<tr>
<td id="1">-</td>
<td id="2" class="top-middle">-</td>
<td id="3" >-</td>
</tr>
<tr>
<td id="4" class="left">-</td>
<td id="5" class="middle">-</td>
<td id="6" class="right">-</td>
</tr>
<tr>
<td id="7" >-</td>
<td id="8" class="bottom-middle">-</td>
<td id="9">-</td>
</tr>
</table>
<p id = "win">It's <span id="playerDisplay">X</span>'s turn.</p>
<p id="warning"></p>
<script src="script.js"></script>
</body>
</html>
我只是不明白为什么它不起作用。 :)
最佳答案
安德鲁的答案非常有效,但如果您想了解为什么您的解决方案不起作用,那是因为这一行(以及其他类似的行):
if (tds[0].textContent === tds[1].textContent === tds[2].textContent)
不幸的是,您无法像这样测试三件事是否相等。您实际上正在做的是将 tds[0].textContent
(假设为“X”)与 tds[1].textContent === tds[2].textContent 的结果进行比较
(如果它们都是“X”,则不会再次成为“X”,而是 bool 值 true
)。
要进行比较,您可以使用:
if (tds[0].textContent === tds[1].textContent && tds[0].textContent === tds[2].textContent)
您可以在这里阅读更多相关信息:
我修改了您的代码片段,但为了简洁起见,使用了一个函数来返回比较。
希望这是有道理的!
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 (check3Same(0, 1, 2)) {
return tds[0].textContent;
} else if (check3Same(3, 4, 5)){
return tds[3].textContent;
} else if (check3Same(6, 7, 8)){
return tds[6].textContent;
} else if (check3Same(0, 3, 6)){
return tds[0].textContent;
} else if (check3Same(1, 4, 7)){
return tds[1].textContent;
} else if (check3Same(2, 5, 8)){
return tds[2].textContent;
} else if (check3Same(0, 4, 8)){
return tds[0].textContent;
} else if (check3Same(2, 4, 6)){
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;
}
}
function check3Same(a, b, c) {
return tds[a].textContent === tds[b].textContent && tds[a].textContent === tds[c].textContent;
}
function game() {
for(var i = 0; i < tds.length; i++) {
tds[i].addEventListener("click", function(){
if(this.textContent === "-") {
this.textContent = currentPlayer;
if(checkWin() === "X") {
winDisplay.textContent = "X wins!!!";
return;
} else if(checkWin() === "O"){
winDisplay.textContent = "O wins!!!";
return;
} else if (checkWin() === -1) {
winDisplay.textContent = "Tied!!!"
return;
}
switchPlayers();
} else {
warning.textContent = "That block has already been filled, choose and empty one."
setTimeout(function(){
warning.textContent = "";
}, 1000);
}
});
}
}
game();
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Tic Tac Toe</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Open+Sans|Roboto&display=swap" rel="stylesheet">
</head>
<body>
<h1>Tic Tac Toe</h1>
<table>
<tr>
<td id="1">-</td>
<td id="2" class="top-middle">-</td>
<td id="3" >-</td>
</tr>
<tr>
<td id="4" class="left">-</td>
<td id="5" class="middle">-</td>
<td id="6" class="right">-</td>
</tr>
<tr>
<td id="7" >-</td>
<td id="8" class="bottom-middle">-</td>
<td id="9">-</td>
</tr>
</table>
<p id = "win">It's <span id="playerDisplay">X</span>'s turn.</p>
<p id="warning"></p>
<script src="script.js"></script>
</body>
</html>
关于javascript - 如何用javascript检查某人是否在井字棋游戏中获胜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60781311/
我正在开发一个 CMD 批次。我想在里面做一些数学运算。这个公式:(x+1)100:y 所以批量处理,x = %x%, and y = %y% .我知道如何设置变量。现在,如何批量计算这个? (WIN
我正在使用 Electron 制作桌面应用程序,我制作了自己的最小化最大化和关闭按钮,所有这些按钮都工作正常,但是当您单击最大化并且它已经最大化时,它并没有取消最大化。这是我的代码: const $
目前我在这里面临着危机。 问题是,当我尝试使用 Windows 窗体的默认 WebBrowser 控件打开 G-Mail 时,它说浏览器不支持较新版本的 HTML 即 XHTML。 那么,有人能建议我
我遇到了一个让我发疯的问题。我在一个网站上工作,该网站在与主导航相同的 div 中有一个框。它一直显示到右边(栏的宽度为 100%),我需要它显示在带有主导航的 div 中(宽度 1020px) 我正
我是一名优秀的程序员,十分优秀!