recursion - jquery recursive function -
im trying make minesweeper game in jquery.
when user clicks on table cell, check done see if there number or x in square. if there not, function called , table cell passed it.
the function returns adjacent squares clicked square, , uncovered.
the question is, selection of adjacent squares returned, how can check if of them empty, , if are, squares adjacent them, , uncover them , check if of them empty....until empty squares adjacent adjacent squares clicked 1 uncovered?
if (isemptysquare(this)) { emp = adjacentsquares(this); $(emp).each(function() { $(this).removeclass('covered').addclass('uncovered'); }); } function adjacentsquares(square) { //find row , column of 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 adjacent td's current td, merge adjacent cells 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; } }
this more familiar problem might think. can achieve need implementing flood fill algorithm.
Comments
Post a Comment