jquery递归函数

发布于 2024-11-07 05:13:52 字数 1548 浏览 3 评论 0原文

我正在尝试用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2024-11-14 05:13:52

这是一个比您想象的更熟悉的问题。您可以通过实施洪水填充算法来实现您的需求。

This is a more familiar problem than you might think. You can achieve what you need by implementing the Flood Fill algorithm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文