javascript - jQuery: too much recursion But I need recursion for minesweeper -


i relatively new javascript. trying code web version of minesweeper. here recursive function needed, , looks work fine until browser gives "too recursion" error. problem need recursion. there other way code minesweeper? here demo: http://altynachar.com/minesweeper/

i can post php code if needed.

function recursive(id){                 var id = id;                 //determine kind of cell is: clean, bomb or adjasent bomb                 if($("#"+id).hasclass("adj")== true)                     var under = "adj";                 if($("#"+id).hasclass("bomb")==true)                     var under = "bomb";                 if($("#"+id).hasclass("clean")==true)                     var under = "clean";                  //open cell                           $("#"+id).hide();                 $("#under_"+id).show();                   //if bomb, open whole grid , button restart                 if(under == 'bomb')                 {                            $(".cover").hide();                     $(".under").show();                     $("body").append("<br /><input type='button' value='restart' onclick='javascript:window.location.reload();' />");                 } else {                              //if clean cell                            if(under == "clean")                     {                         //get adjasent cell ids                         var split = id.split('-');                         var row = parseint(split[0]);                         var col = parseint(split[1]);                         var adjasent = new array();                         adjasent[0] = (row-1)+"-"+ (col+1);                         adjasent[1] =  row +"-"+(col+1);                         adjasent[2] = (row+1)+"-"+(col+1);                         adjasent[3] = (row+1)+"-"+col;                         adjasent[4] = (row+1)+"-"+(col-1);                         adjasent[5] =  row+"-"+(col-1);                         adjasent[6] = (row -1)+"-"+(col-1);                         adjasent[7] = (row -1)+"-"+col;                          //loop through adjasent cells                         for(var i=0; i<adjasent.length; i++)                         {                             var split2 = adjasent[i].split('-');                             var row2 = parseint(split2[0]);                             var col2 = parseint(split2[1]);                              //check if cell existent                             if(row2 > 0 && row2 < 17)                             {                                 if(col2 > 0 && col2 < 17)                                 {                                     //perform recursion                                     var adj = adjasent[i];                                     recursive(adj);                                 }                              }                                                        }                     }                 }              }            

my guess if have 2 clean cells next each other code in infinite recursion.

each iteration recurses adjacent cells. cell , b next each other, , both clean. call recurse b, recurse a, recurses b, , on.

you can either try clean recursion doesn't @ cells seen, or remove recursion. can accomplish same thing adding unseen clean cells queue, , keep popping off end of queue until it's empty. might make easier avoid checking same cell twice too.


also, please don't build strings split them separate data later. instead of:

adjasent[0] = (row-1)+"-"+ (col+1); /* ... */ var split2 = adjasent[i].split('-'); var row2 = parseint(split2[0]); var col2 = parseint(split2[1]); 

just do

adjacent[0] = { row: row-1, col: col+1 }; /* ... */ var row2 = adjacent[0].row var col2 = adjacent[0].col 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -