0
我试图在jquery中做一个扫雷游戏。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;
}
}
干杯。在查看洪水填充后,我发现了我所需要的@ http://www.htmlgoodies.com/primers/jsp/article.php/3622321/Javascript-Basics-Part-12.htm – callumander 2011-05-12 17:23:02