3 Replies Latest reply: Nov 19, 2007 3:28 PM by 807600 RSS

    Help calling a method recursively

    807600
      i'm writing a little text-based minesweeper game and need some help. when you hit a blank space, all the spaces around it and the numbers bordering them are supposed to be revealed, just like in the real game:

      http://img252.imageshack.us/img252/7953/minesweeperwk3.jpg

      this method is called when a user enters a coordinate (1-9) of the 10x10 array.
      first, it's supposed to check if the cell they picked is not a mine (which is labeled as F for flag). there is a display array that shows what is currently on the screen and a solution array which holds the solution.
      next it is supposed to check the surrounding 8 squares, starting at the one above it and going clockwise. if a square is empty, then it should call the method again and start at that square, and once it's done, go back to the original square, therefore going through all the possible squares. also, the if statements check if a surrounding cell is not out of bounds. i just can't seem to figure it out though. any help is appreciated.
         public static boolean setSquareVisible(int row, int col) {
            if(solution[row][col] != 'F') {
               if (solution[row][col] == ' ') {
                  if ((row - 1) >= 0) {
                     if (solution[row - 1][col] != 'F') {
                        display[row - 1][col] = solution[row - 1][col];
                        setSquareVisible(row - 1, col);
                     }
                  }
                  if ((row - 1) >= 0 && (col + 1) < 10) {
                     if (solution[row - 1][col + 1] != 'F') {
                        display[row - 1][col + 1] = solution[row - 1][col + 1];
                        setSquareVisible(row - 1, col + 1);
                     }
                  }
                  if ((col + 1) < 10) {
                     if (solution[row][col + 1] != 'F') {
                        display[row][col + 1] = solution[row][col + 1];
                        setSquareVisible(row, col + 1);
                     }
                  }
                  if ((row + 1) < 10 && (col + 1) < 10) {
                     if (solution[row + 1][col + 1] != 'F') {
                        display[row + 1][col + 1] = solution[row + 1][col + 1];
                        setSquareVisible(row + 1, col + 1);
                     }
                  }
                  if ((row + 1) < 10) {
                     if (solution[row + 1][col] != 'F') {
                        display[row + 1][col] = solution[row + 1][col];
                        setSquareVisible(row + 1, col);
                     }
                  }
                  if ((row + 1) < 10 && (col - 1) >= 0) {
                     if (solution[row + 1][col - 1] != 'F') {
                        display[row + 1][col - 1] = solution[row + 1][col - 1];
                        setSquareVisible(row + 1, col - 1);
                     }
                  }
                  if ((col - 1) >= 0) {
                     if (solution[row][col - 1] != 'F') {
                        display[row][col - 1] = solution[row][col - 1];
                        setSquareVisible(row, col - 1);
                     }
                  }
                  if ((row - 1) >= 0 && (col - 1) >= 0) {
                     if (solution[row - 1][col - 1] != 'F') {
                        display[row - 1][col - 1] = solution[row - 1][col - 1];
                        setSquareVisible(row - 1, col - 1);
                     }
                  }
               }
               display[row][col] = solution[row][col];
               return true;
            }
            else
               return false;
         }
        • 1. Re: Help calling a method recursively
          807600
          Two things you can fix:
          1) You should create a class that represents each cell in your grid. It would keep track of it's own state, such as if it has a flag, a mine, if it's been exposed yet, etc. Parallel arrays, especially multidimensional, are very bug prone.

          2) You're setting the values of the current cell's neighbors, then recursing to the neighbors. Your method should set the values of the current cell, then recurse to the neighbors. That way each call is only responsible for manipulating the current cell, and only has to check the current cell's state.
          setVisible(int row, int col) 
              if the current cell needs to be changed
                  change it
              for each of the neighboring cells
                  if it's index is within bounds
                      call setVisible() on it
          The method is much much simpler that way. If you write another method that tells you if a given row and column are within bounds, this method shouldn't be more than 7 or 8 lines long.
          • 2. Re: Help calling a method recursively
            807600
            PS, you only need to recurse to the neighbors on the 4 sides, not the 4 on the diagonal. i.e., for the NE cell, you recurse to the E cell, which then recurses to the N cell, so you'd be checking it multiple times if you recursed to the NE directly.
            • 3. Re: Help calling a method recursively
              807600
              i'm not really sure how to implement a for loop for this problem because it needs to check for row, col, or row +/- 1, and col +/- 1. i don't really know how you would structure a for loop to do that. i figured something like this would make it a little more simple:
                 public static boolean setSquareVisible(int row, int col) {
                    if (row < 0 || row > 9 || col < 0 || col > 9) {
                       return false;
                    }
                    else {
                    if (solution[row][col] != 'F') {
                          display[row][col] = solution[row][col];
                       setSquareVisible(row - 1, col);
                       setSquareVisible(row - 1, col + 1);
                       setSquareVisible(row, col + 1);
                       setSquareVisible(row + 1, col + 1);
                       setSquareVisible(row + 1, col);
                       setSquareVisible(row + 1, col - 1);
                       setSquareVisible(row, col - 1);
                       setSquareVisible(row - 1, col - 1);
                       display[row][col] = solution[row][col];
                       return true;
                    }
                    else
                       return false;
                    }
                 }
              but that still doesn't work. i figure i am close, but i am failing to see what is going wrong with the program.