我正在用javascript构建一个connect 4 web应用程序。我目前正在尝试创建一个‘结束游戏’检查,如果整个网格被填满,没有赢家,游戏将结束。
function endGame(yTarget, xTarget) {
let topRowOpen = true;
// must have all yTarget index to have class for game to end
for (let i = 0; i < yTarget + 1; i++) {
let box = $(`#g-${i}-${0}`);
console.log(box)
if (box.hasClass("selectedP0Box") || box.hasClass("selectedP1Box")) {
topRowOpen = false;
}
}
return !topRowOpen;
}我似乎不能使用for循环来解决这个问题(我需要考虑到不同大小的游戏板)。
发布于 2017-10-11 08:05:24
这是你正在寻找的更多东西:
var moveAvailable = function() {
var n = 0;
while (true) {
var topCell = $('g-' + (n++) + '-0');
if (!topCell.length) break;
// It's important to use &&, not || here:
if (!topCell.hasClass('selectedP0Box')
&& !topCell.hasClass('selectedP1Box')) return true;
}
// All top cells were checked; none were empty
return false;
}(注意:我的jquery相当生疏,我相信你可以让这段代码看起来更好)
https://stackoverflow.com/questions/46676175
复制相似问题