title: Going through a 2-d array to reach position with integer 0
body: <br/>
  I am trying to go through a 2-d array and reach the position that has a 0 and I want to print the direction the program has taken to reach the position (North, south, east, west). If it is not possible, I have to return false. Can someone help me. <br/>
  Thank you in advance for your help <br/>
  Here is the method I created, it has to be recursive:
  <pre><code>    public static Boolean MagicBoard_recursive(int[][] board, int size, int startRow, int startCol) {
          boolean result = true;
          int number = board[startRow][startCol];
          int destinationNumber;
  
          while(!result) {
              // If we reach the position that contains the 0
              if(board[startRow][startCol] == 0) {
                  System.out.println(&quot;We have reached the goal square containing 0.&quot;);
                  result = true;
                  break;
              }           
  
              // If we can only move in 1 direction, either North, South, East or West
              // Verify if we can only move south
              if((startRow+number) &lt; size &amp;&amp; (startRow-number) &lt; 0 &amp;&amp; ((startCol+number) &gt; size || (startCol-number) &lt; 0)) {
                  destinationNumber = board[startRow+number][startCol];
  
                  // Make sure that the destination position will not make us go back and forth
                  if((startRow == (startRow+number-destinationNumber))) {
                      System.out.print(&quot;The MagicBoard is unsolvable&quot;);
                      result = false;
                      break;
                  }
                  // If don't go back and forth, then we continue
                  else {
                      startRow = startRow+number;
                      System.out.print(&quot;Move south &quot; + number + &quot;, &quot;);
                      MagicBoard_recursive(board, size, startRow, startCol);
                  }
              }
              // Verify if we can only move North
              else if((startRow+number) &gt; size &amp;&amp; (startRow-number) &gt; 0 &amp;&amp; ((startCol+number) &gt; size || (startCol-number) &lt; 0)){
                  destinationNumber = board[startRow-number][startCol];
                  // Make sure that we won't go back and forth if we go at that position
                  if((startRow == (startRow-number+destinationNumber))) {
                      System.out.print(&quot;The MagicBoard is unsolvable&quot;);
                      result = false;
                      break;
                  }
                  // If don't go back and forth, then we continue
                  else {
                      startRow = startRow-number;
                      System.out.print(&quot;Move north &quot; + number + &quot;, &quot;);
                      MagicBoard_recursive(board, size, startRow, startCol);
                  }
              }
              // If can only go east
              else if((startCol+number)&lt;size &amp;&amp; (startCol-number)&lt;0 &amp;&amp; ((startRow+number)&gt;size || (startRow-number)&lt;0)) {
                  destinationNumber = board[startRow][startCol+number];
                  // Make sure that we won't go back and forth if we go at that position
                  if((startCol == (startCol+number-destinationNumber))) {
                      System.out.print(&quot;The MagicBoard is unsolvable&quot;);
                      result = false;
                      break;
                  }
                  // If we don't go back and forth, then we continue
                  else {
                      startCol = startCol+number;
                      System.out.print(&quot;Move east &quot; + number + &quot;, &quot;);
                      MagicBoard_recursive(board, size, startRow, startCol);
                  }
              }
              // If can't move any other direction that west
              else if((startCol+number) &gt; size &amp;&amp; (startCol-number)&gt; 0 &amp;&amp; ((startRow+number)&gt;size || (startRow-number)&lt;0)) {
                  destinationNumber = board[startRow][startCol-number];
                  // Make sure that we won't go back and forth if we go at that position
                  if((startCol == (startCol-number+destinationNumber))) {
                      System.out.print(&quot;The MagicBoard is unsolvable&quot;);
                      result = false; 
                      break;
                  }
                  // If we don't go back and forth, then we continue
                  else {
                      startCol = startCol-number;
                      System.out.print(&quot;Move west &quot; + number + &quot;, &quot;);
                      MagicBoard_recursive(board, size, startRow, startCol);
                  }
              }
  
              // If we can move any direction
              else {
                  // try moving south
                  SOUTH:
                      if(startRow-number &lt; 0) {
                          destinationNumber = board[startRow+number][startCol];
                          // Verify if we go back and forth if we go south, if we do, then we the result will be 
                          if((startRow == (startRow+number-destinationNumber))) {
                              break SOUTH;
                          }
                          else {
                              startRow = startRow+number;
                              System.out.print(&quot;Move south &quot; + number + &quot;, &quot;);
                              MagicBoard_recursive(board, size, startRow, startCol);
                          }
                      }
                  NORTH:
                      if(startRow-number &gt; 0) {
                          destinationNumber = board[startRow-number][startCol];
                          if((startRow == (startRow-number+destinationNumber))) {
                              break NORTH;
                          }
                          else {
                              startRow = startRow-number;
                              System.out.print(&quot;Move north &quot; + number + &quot;, &quot;);
                              MagicBoard_recursive(board, size, startRow, startCol);
                          }
                      }
                      
                      // try moving east
                  EAST:
                      if(startCol-number&lt;0) {
                          destinationNumber = board[startRow][startCol+number];
                          // If will go back and forth at that position, then we exit the EAST label and we go to the next label
                          if((startCol == (startCol+number-destinationNumber))) {
                              System.out.print(&quot;The MagicBoard is unsolvable&quot;);
                              break EAST;
                          }
                          else {
                              startCol = startCol+number;
                              System.out.print(&quot;Move east &quot; + number + &quot;, &quot;);
                              MagicBoard_recursive(board, size, startRow, startCol);
                          }
                      }
                      // Try moving west  
                  WEST:
                      if(startCol-number &gt; 0) {
                          destinationNumber = board[startRow][startCol-number];
                          // If we go back and forth in that position, then we exit the EAST label
                          if((startCol == (startCol-number+destinationNumber))) {
                              System.out.print(&quot;The MagicBoard is unsolvable&quot;);
                              result = false;
                              break WEST;
                          }
                      else {
                          startCol = startCol-number;
                          System.out.print(&quot;Move west &quot; + number + &quot;, &quot;);
                          MagicBoard_recursive(board, size, startRow, startCol);
                      }
                  }
              }
          }
  
          return result;
      }
  </code></pre>