Javascript recursion not working
I'm making a jquery minesweeper and am currently working on the revealing function for when you click a block with 0
adjacent mines. The intended result is to loop through all 8 adjacent blocks reveal those blocks, if they are also '0
' blocks, it recurs for that block:
function reveal(block) {
block.removeClass('hide');
var thex = getXY(block)[0];开发者_C百科
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));
}
}
}
}
Currently this function is stopping after the first block checked for each time the function iterates. It seems as though the call is breaking the for loops
.
I'm pretty sure you have an infinite recursion - both directly and indirectly. Calling reveal(bl(2,2))
will call reveal(bl(2,2))
in the loop. In addition, if bl(1,2)
is also 0
, it will also call reveal(bl(2,2))
when searching for each neighbor.
You should check for the "base case" in the first line:
if(!block.hasClass('hide'))
return;
精彩评论