我一直在尝试制作一个井字游戏,但出于某种原因,条件不起作用..
我试图将第一行中的前三个元素作为目标,如果它们都是相同的颜色,我想浏览器提示“赢”弹出窗口,但它不会发生。
有谁知道为什么?
这是我的代码:
var one = document.getElementById("one");
var two = document.getElementById("two");
var three = document.getElementById("three");
var four = document.getElementById("four");
var five = document.getElementById("five");
var six = document.getElementById("six");
var seven = document.getElementById("seven");
var eight = document.getElementById("eight");
var nine = document.getElementById("nine");
var counter = 1;
//code that changes the box color
one.addEventListener("click", function() {
if (counter < 10 && counter % 2 === 0) {
one.style.backgroundColor = "yellow";
} else if (counter < 10 && counter % 2 != 0) {
one.style.backgroundColor = "red";
}
counter++;
console.log(counter);
});
two.addEventListener("click", function() {
if (counter < 10 && counter % 2 === 0) {
two.style.backgroundColor = "yellow";
} else if (counter < 10 && counter % 2 != 0) {
two.style.backgroundColor = "red";
}
counter++;
console.log(counter);
});
three.addEventListener("click", function() {
if (counter < 10 && counter % 2 === 0) {
three.style.backgroundColor = "yellow";
} else if (counter < 10 && counter % 2 != 0) {
three.style.backgroundColor = "red";
}
counter++;
console.log(counter);
});
four.addEventListener("click", function() {
if (counter < 10 && counter % 2 === 0) {
four.style.backgroundColor = "yellow";
} else if (counter < 10 && counter % 2 != 0) {
four.style.backgroundColor = "red";
}
counter++;
console.log(counter);
});
five.addEventListener("click", function() {
if (counter < 10 && counter % 2 === 0) {
five.style.backgroundColor = "yellow";
} else if (counter < 10 && counter % 2 != 0) {
five.style.backgroundColor = "red";
}
counter++;
console.log(counter);
});
six.addEventListener("click", function() {
if (counter < 10 && counter % 2 === 0) {
six.style.backgroundColor = "yellow";
} else if (counter < 10 && counter % 2 != 0) {
six.style.backgroundColor = "red";
}
counter++;
console.log(counter);
});
seven.addEventListener("click", function() {
if (counter < 10 && counter % 2 === 0) {
seven.style.backgroundColor = "yellow";
} else if (counter < 10 && counter % 2 != 0) {
seven.style.backgroundColor = "red";
}
counter++;
console.log(counter);
});
eight.addEventListener("click", function() {
if (counter < 10 && counter % 2 === 0) {
eight.style.backgroundColor = "yellow";
} else if (counter < 10 && counter % 2 != 0) {
eight.style.backgroundColor = "red";
}
counter++;
console.log(counter);
});
nine.addEventListener("click", function() {
if (counter < 10 && counter % 2 === 0) {
nine.style.backgroundColor = "yellow";
} else if (counter < 10 && counter % 2 != 0) {
nine.style.backgroundColor = "red";
}
counter++;
console.log(counter);
});
//logic for winning
if (one.style.backgroundColor == "red" && two.style.backgroundColor == "red" && three.style.backgroundColor == "red") {
console.log("red wins");
}
.knobs {
background-color: blue;
border: none;
padding: 50px;
margin: 10px;
}
.knobs:focus {
outline: none;
}
#total {
text-align: center;
}
<!DOCTYPE html>
<div id="total">
<button id="one" class="knobs"></button>
<button id="two" class="knobs"></button>
<button id="three" class="knobs"></button>
<br>
<button id="four" class="knobs"></button>
<button id="five" class="knobs"></button>
<button id="six" class="knobs"></button>
<br>
<button id="seven" class="knobs"></button>
<button id="eight" class="knobs"></button>
<button id="nine" class="knobs"></button>
</div>
非常感谢您的帮助!
答案 0 :(得分:3)
问题是你的if语句只在开头运行一次。您需要将其转换为函数,然后在每个事件后调用它。像
这样的东西function check() {
if (one.style.backgroundColor == "red" && two.style.backgroundColor == "red" && three.style.backgroundColor == "red"){
console.log("red wins");
}
}
在事件监听器结束时,您可以这样称呼它:
check();
(就像Xufox上面说的那样,你应该真正关注委托,这样才能更容易管理,而且你不必重复所有事情。)
答案 1 :(得分:0)
在您的代码中:
var one = document.getElementById("one");
var two = document.getElementById("two");
var three = document.getElementById("three");
var four = document.getElementById("four");
var five = document.getElementById("five");
var six = document.getElementById("six");
var seven = document.getElementById("seven");
var eight = document.getElementById("eight");
var nine = document.getElementById("nine");
您不需要像这样声明每个按钮。您可以使用数组和for
或forEach
之类的循环对其进行优化。像这样:
var knobs = [
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine'
];
knobs.forEach(function(id) {
var element = document.getElementById(id);
element.addEventListener('click', function() {
...
}
}
您正在使用addEventListener
为每个按钮添加一个侦听器。这可以在我的第1点提到时进行优化。你得到了带有var element = document.getElementById(id);
的按钮元素,你可以在同一forEach
中添加听众。
forEach
为每个元素初始化board
数组-1
。这意味着它是空的。verifyWinner(index);
并传递了按钮的索引; one.style.backgroundColor == 'red'
中仅==
是不够的,因此建议您使用===
进行比较strings
。red
为例。这就是为什么我建议你使用yellow
数组来映射&#34;游戏状态&#34;。board
作为数组,如果board
,则1
如果red
则2
。yellow
这有助于您简化验证过程。这是代码示例。快乐的编码。 注意:我减小了旋钮大小,以便在代码片段中获得更好的可视化效果。
var knobs = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
var board = [];
var isRed = true;
var isEndGame = false;
var counter = 0;
knobs.forEach(function(id) {
var element = document.getElementById(id);
board.push(-1);
element.addEventListener('click', function() {
var index = knobs.indexOf(id);
if (!isEndGame && board[index] == -1) {
if (isRed) {
element.style.backgroundColor = 'red';
board[index] = 1;
} else {
element.style.backgroundColor = 'yellow';
board[index] = 2;
}
verifyWinner(index);
isRed = !isRed;
counter++;
//console.log(counter)
}
if (counter == 9) {
isEndGame = true;
console.log('End of game.');
}
});
});
function verifyWinner(index) {
//logic for winning
var player = isRed ? 1 : 2;
switch (index) {
case 0:
case 2:
case 6:
case 8:
case 4:
verifyVertial(index);
verifyHorizontal(index);
verifyDiagonal(index);
break;
case 1:
case 3:
case 5:
case 7:
verifyVertial(index);
verifyHorizontal(index);
break;
}
if (isEndGame) {
console.log((isRed ? 'Red' : 'Yellow') + ' has won.');
console.log('End of game.');
}
}
function verifyVertial(index) {
//logic for winning
if (!isEndGame) {
var player = isRed ? 1 : 2;
switch (index) {
case 0:
case 3:
case 6:
if (board[0] == player && board[3] == player && board[6] == player) {
isEndGame = true;
}
break;
case 1:
case 4:
case 7:
if (board[1] == player && board[4] == player && board[7] == player) {
isEndGame = true;
}
break;
case 2:
case 5:
case 8: // edges
if (board[2] == player && board[5] == player && board[8] == player) {
isEndGame = true;
}
break;
}
}
}
function verifyHorizontal(index) {
//logic for winning
if (!isEndGame) {
var player = isRed ? 1 : 2;
switch (index) {
case 0:
case 1:
case 2: // edges
if (board[0] == player && board[1] == player && board[2] == player) {
isEndGame = true;
}
break;
case 3:
case 4:
case 5:
if (board[3] == player && board[4] == player && board[5] == player) {
isEndGame = true;
}
break;
case 6:
case 7:
case 8: // edges
if (board[6] == player && board[7] == player && board[8] == player) {
isEndGame = true;
}
break;
}
}
}
function verifyDiagonal(index) {
//logic for winning
if (!isEndGame) {
var player = isRed ? 1 : 2;
switch (index) {
case 0:
case 4:
case 8: // edges
if (board[0] == player && board[4] == player && board[8] == player) {
isEndGame = true;
}
break;
case 2:
case 4:
case 6: // edges
if (board[2] == player && board[4] == player && board[6] == player) {
isEndGame = true;
}
break;
}
}
}
&#13;
.knobs {
background-color: blue;
border: none;
padding: 25px;
margin: 3px;
}
.knobs:focus .clear:focus {
outline: none;
}
#total {
text-align: center;
}
.clear {
clear: both;
}
&#13;
<!DOCTYPE html>
<div id="total">
<button id="one" class="knobs"></button>
<button id="two" class="knobs"></button>
<button id="three" class="knobs"></button>
<div class="clearfix"></div>
<button id="four" class="knobs"></button>
<button id="five" class="knobs"></button>
<button id="six" class="knobs"></button>
<div class="clearfix"></div>
<button id="seven" class="knobs"></button>
<button id="eight" class="knobs"></button>
<button id="nine" class="knobs"></button>
</div>
&#13;