我试图在 jquery 中制作一个扫雷游戏。
当用户单击表格单元格时,会检查方框中是否有数字或 x。如果没有,则调用此函数并将表格单元格传递给它。
该函数将所有相邻的方格返回到单击的方格,然后它们被揭开。
问题是,从最初返回的相邻方格的选择中,我如何检查它们是否为空,如果是,则获取与它们相邻的方格,然后发现它们并检查它们是否为空... .直到所有与被点击方格相邻的空方格都被揭开?
if (isEmptySquare(this)) {
emp = adjacentSquares(this);
$(emp).each(function() {
$(this).removeClass('covered').addClass('uncovered');
});
}
function adjacentSquares(square) {
//Find the row and column of the current td(square)
var thisRow = $(square).parent().parent().children().index($(square).parent());
var thisCol = $(square).parent().children().index($(square));
var prevRow = (thisRow - 1);
var nextRow = (thisRow + 1);
if (thisCol == 0) {
sliceFrom = 0;
} else {
sliceFrom = (thisCol - 1);
}
//Select all the adjacent td's to the current td, then merge the adjacent cells into a variable
var above = $('tr:eq(' + prevRow + ')').children('td').slice((sliceFrom), (thisCol + 2));
var below = $('tr:eq(' + nextRow + ')').children('td').slice((sliceFrom), (thisCol + 2));
var aboveBelow = $.merge(above, below);
var prevNext = $.merge(($(square).next('td')), ($(square).prev('td')));
var adjacents = $.merge(aboveBelow, prevNext);
return adjacents;
}
function isEmptySquare(square) {
if ($(square).filter(function() {
return !/[0-9]/.test($(square).text());
}).not(":contains('x')").length > 0) {
return true;
}
else {
return false;
}
}