d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > (vb) Avoid Stack Overflow?
Add Reply New Topic New Poll
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Apr 8 2013 11:11pm
I'm making a minesweeper clone, however when user clicks a square it checks the boxes around it. If boxes around it aren't touching a mine then they trigger the boxes around them. After calling one function inside another I'm getting the stack overflow error. What can I do? If I code this clone differently it will take ages...
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 8 2013 11:38pm
I have the feeling you have a bug in your code and it's not terminating properly. Unless you have a crazy large number of boxes.

To avoid the recursion problem, you can just queue them up and write a loop.
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Apr 9 2013 01:12pm
Quote (carteblanche @ Apr 9 2013 12:38am)
I have the feeling you have a bug in your code and it's not terminating properly. Unless you have a crazy large number of boxes.

To avoid the recursion problem, you can just queue them up and write a loop.


currently it's 9x9, could you give me an example of the loop way of doing this?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 9 2013 05:35pm
in pseudocode

Code
onClick(ClickedCell){
   q = new queue();
  q.add(ClickedCell);
  while (q not empty){
     Cell = q.remove();
     cell.showHiddenValue();
     cell.markAsProcessed();
     if (cell is number){
         // do nothing special
     } else if (cell is mine){
         // lost the game. throw exception or whatever
     } else{
        // blank. we need to show all its neighbors and repeat this logic
        // we are guarenteed none of the neighbors are a bomb.
        q.add(cell.getNeighbors());
     }
  }
}


Something along those lines. make sure getNeighbors doesnt crash on corners/sides and excludes any flagged cells and any cells already marked as processed

/edit: that's probably happening to you, now that i think of it. when you're trying to recurse on your neighbors you're prolly adding back the cell you just processed, so it goes on forever adding each other back.

This post was edited by carteblanche on Apr 9 2013 05:40pm
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Apr 9 2013 06:45pm
Quote (carteblanche @ Apr 9 2013 06:35pm)
in pseudocode

Code
onClick(ClickedCell){
   q = new queue();
  q.add(ClickedCell);
  while (q not empty){
     Cell = q.remove();
     cell.showHiddenValue();
     cell.markAsProcessed();
     if (cell is number){
         // do nothing special
     } else if (cell is mine){
         // lost the game. throw exception or whatever
     } else{
        // blank. we need to show all its neighbors and repeat this logic
        // we are guarenteed none of the neighbors are a bomb.
        q.add(cell.getNeighbors());
     }
  }
}


Something along those lines. make sure getNeighbors doesnt crash on corners/sides and excludes any flagged cells and any cells already marked as processed

/edit: that's probably happening to you, now that i think of it. when you're trying to recurse on your neighbors you're prolly adding back the cell you just processed, so it goes on forever adding each other back.


Makes sense now, thanks!
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll