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;
}
}
Im trying to make a minesweeper game in jquery.
When a user clicks on a table cell, a check is done to see if there is a number or an x in the square. If there is not, this function is called and the table cell is passed to it.
The function returns all the adjacent squares to the clicked square, and they are then uncovered.
The question is, from the selection of adjacent squares initially returned, how can i check if any of them are empty, and if they are, get the squares adjacent to them, and uncover them and check if any of them are empty....until all empty squares that were adjacent to any adjacent squares to the clicked one are uncovered?
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个比您想象的更熟悉的问题。您可以通过实施洪水填充算法来实现您的需求。
This is a more familiar problem than you might think. You can achieve what you need by implementing the Flood Fill algorithm.