1

我正在制作一个 jquery 扫雷器,目前正在研究当您单击具有0相邻地雷的块时的显示功能。预期的结果是遍历所有 8 个相邻块以显示这些块,如果它们也是 ' 0' 块,它会针对该块重复:

function reveal(block) {
    block.removeClass('hide');
    var thex = getXY(block)[0];
    var they = getXY(block)[1];
    if (blockNumber(block) == '0') {
        alert('test');
        --they;
        --thex;
        var nearmines = 0;
        for (mody=0;mody<3;mody++){
            for (modx=0;modx<3;modx++){
                var newx = thex + modx;
                var newy = they + mody;
                reveal(bl(newx,newy));
            }
        }
    }
}

目前,该函数在每次函数迭代时检查第一个块后停止。似乎呼叫正在中断for loops.

4

1 回答 1

1

我很确定你有一个无限递归 - 直接和间接。调用reveal(bl(2,2))reveal(bl(2,2))在循环中调用。另外, if bl(1,2)is also ,在搜索每个邻居时0也会调用。reveal(bl(2,2))

您应该检查第一行中的“基本情况”:

if(!block.hasClass('hide'))
    return;
于 2011-10-11T06:16:17.227 回答