我正在尝试为简单的四连赢游戏制定制胜法则,此代码用于检查水平线中是否有4个相同的值:
function win(clicked) {
var cellco = clicked.className.match(/[a-zA-Z]+|[0-9]+/g); // example output:["col", "3", "row", "7"]
var playervalue = document.getElementById('tbl').rows[cellco[3]].cells[cellco[1]].children[0];
var count = 0;
for (var i = 0; i < GRID_SIZE; i++) {
var slot = document.getElementById('tbl').rows[cellco[3]].cells[i].children[0];
if (slot == playervalue) {
count++;console.log(count)//it only shows 1 even after having 4 objects
if (count >=4) {
return true;
} else {
count = 0;
}
}
}
return false;
}
答案 0 :(得分:4)
var count = 0;
计数从零开始。
count++;console.log(count)
您增加它并记录下来。现在是1。
if (count >=4) {
不是。 1小于4。
count = 0;
您将其设置为零。
(然后回到将其递增到1并记录的步骤)。