title: Recursive algorithm with Minesweeper not working as expected
body: <p>I am trying to understand recursion and so was trying to write a recursive function for the minesweeper game. My program has no game interface as it is just for the purpose of understanding. </p>
  
  <p>Here is my understanding:   </p>
  
  <p>I have a grid of given size which will be filled with numbers. For simplicity I have assumed that the 0 will represent mine and an empty cell in grid will be represented by digit 9. Here is my grid:<br>
   0  1   9 9 9<br>
   1 1 9 9 9<br>
   9 9 9 9 9<br>
   9 9 9 1 1<br>
   9 9 9 1 0 </p>
  
  <p>Here is my recursive algorithm. I think I have put in the right  boundary conditions but seems like the algorithm that goes into infinite recursion:</p>
  
  <p>Base case:<br>
  IF the grid coordinates are less than 0 i.e x&lt;0 OR y&lt;0 OR x> grid.length-1 OR y > grid.length-1<br>
  return<br>
  ELSE<br>
  If the grid shows a number other than 0 or 9 , display the number i.e. grid[x][y]!=0 &amp;&amp;  grid[x][y]!=9<br>
  else If grid shows 0 that means it is a mine, display Game over<br>
  else<br>
    Recursive cases for all surrounding 8 cells as all surrounding cells are safe to open</p>
  
  <p>Here is some code to show what I have listed in the algorithm:</p>
  
  <pre><code>public static void playGame(int[][]grid, int x, int y){
          if(x &gt; grid.length-1 || x &lt;0 || y &gt; grid.length-1 || y&lt;0){
              return;
          }else{
              if(grid[x][y] !=0 &amp;&amp; grid[x][y]!=9){
                  //means this is a valid number to display to user
                  System.out.println("opening cell:"+x+","+y+" "+grid[x][y]);
              }else if(grid[x][y] == 0){
                  System.out.println("Game over!");
                  //might want to show all mine locations
              }else{
                  //all 8 cells are safe to open 
                  playGame(grid,x-1,y-1); //left
                  playGame(grid,x,y-1); //up
                  playGame(grid,x,y+1);//down
                  playGame(grid,x+1,y);//diagonal
                  playGame(grid,x-1,y); //diagonal
                  playGame(grid,x+1,y-1);//diagonal
                  playGame(grid,x+1,y+1);//right
                  playGame(grid,x-1,y+1);//diagonal
  
              }
          }
      }
  </code></pre>
  
  <p>I passed the cell as 2,2 and it gives me java.lang.StackOverflowError. 
  Can someone please guide me here. Is there a problem with my understanding on the algorithm or recursion or there is some bug that I have may have introduced. I am unable to spot the problem.</p>