title: Checking adjacent tiles in 2-dimensional array
body: <p>So i'm trying to make a game of Connect Four in Java, instead I'm connecting 6 instead of 4. </p>
  
  <p>I have a 2-dimensional array and X amount of players. I have to check if 6 blocks in succession (horizontal, vertical and diagonal) are marked by the same player. If they are, the program should print who won. </p>
  
  <p>Now, I don't have problems with the checking or determining who won, though for the life of me I can't figure out how to prevent the program from crashing whenever it tries to check for a block that's outside the array. </p>
  
  <p>Now, I'm trying to avoid using try-catch or plopping 8 loops one after the other, and instead use one method for all of the directions with just variation in the parameters but I can't seem to make it work :\ </p>
  
  <p>Does anyone have a suggestion on how this might work? </p>
  
  <p>I'm a beginner in programming and I've possibly missed something so that's why I'm asking for help :)</p>
  
  <p>Cheers</p>
  
  <p>Edit: here's the code. It's a bit long, that's why i want to shorten it and make it work somehow. the Terminal class is the same as the System.out.println one.</p>
  
  <pre><code>void checkIfPlayerWins(Field field, Integer rowNumber, Integer colNumber) {
      Integer counter = 1;
  
      for (int i = 1; i &lt;= 6; i++) {
          if (field.isOccupied(rowNumber, colNumber + i)) {
              counter++;
          } else {
              break;
          }
      }
  
      for (int i = 1; i &lt;= 6; i++) {
          if (field.isOccupied(rowNumber - i, colNumber + i)) {
              counter++;
          } else {
              break;
          }
      }
  
      for (int i = 1; i &lt;= 6; i++) {
          if (field.isOccupied(rowNumber - i, colNumber)) {
              counter++;
          } else {
              break;
          }
      }
  
      for (int i = 1; i &lt;= 6; i++) {
          if (field.isOccupied(rowNumber - i, colNumber - i)) {
              counter++;
          } else {
              break;
          }
      }
  
      for (int i = 1; i &lt;= 6; i++) {
          if (field.isOccupied(rowNumber, colNumber - i)) {
              counter++;
          } else {
              break;
          }
      }
  
      for (int i = 1; i &lt;= 6; i++) {
          if (field.isOccupied(rowNumber + i, colNumber - i)) {
              counter++;
          } else {
              break;
          }
      }
  
      for (int i = 1; i &lt;= 6; i++) {
          if (field.isOccupied(rowNumber + i, colNumber)) {
              counter++;
          } else {
              break;
          }
      }
  
      for (int i = 1; i &lt;= 6; i++) {
          if (field.isOccupied(rowNumber + i, colNumber + i)) {
              counter++;
          } else {
              break;
          }
      }
  
      if (counter == 6) {
          Terminal.printLine("");
      }
  }
  </code></pre>
  
  <p>here's the isOccupied method</p>
  
  <pre><code>boolean isOccupied(Integer x, Integer y) {
      return !this.field[x][y].equals("**");
  
  }
  </code></pre>