title: Finding the neighbors of 2D array
body: <p>I've a 2D grid of 75rows by 75 cols, I draw symbols of 2 colors (red and blue) on users clicks, it works fine but I'm stuck on a major problem I want to find the neighbor of those symbols: Example when the user click on a cell, it prints a symbol (oval) now I want that click also checks in its neighbors if the cell are occupied or empty, if occupied, by which symbol.</p>
  
  <p>I found the algorithm below which seems to fit the need, but can't figure out how to adapt to my code:</p>
  
  <pre><code>private static int[][] array = new int[3][3];
  
      private static void initialiseArray() {
          int counter = 1;
          for (int row = ; row &lt; 3; row++) {
              for (int col = ; col &lt; 3; col++) {
                  array[row][col] = counter;
                  counter++;
              }
          }
  
      }
  
      public static void main(String args[]) {
          initialiseArray();
          for (int row = ; row &lt; 3; row++) {
              for (int col = ; col &lt; 3; col++) {
                  System.out.print(array[row][col]);
                  if( col == 2 ){
                      System.out.println("\n");
                  }
              }
          }
          neighbours();
      }
  
      public static void neighbours(){  
          int posX = 2;
          int posY = 2;
  
          for( int row = posX - 1; row &lt;= posX + 1; row++){
              for(int col =  posY -1;  col &lt;= posY + 1; col++){
                  if( !(posX == row &amp;&amp;  posY == col) &amp;&amp; row &gt;= &amp;&amp; col &gt;= &amp;&amp; row &lt; 3 &amp;&amp; col &lt; 3 ){
                      System.out.println(array[row][col]);
                  }
              }
          }
      }
  </code></pre>
  
  <p>Below is my code, I need you help me solve this. Sorry bothering you.</p>
  
  <pre><code>public final class Pha extends JFrame {
  
      public static int ROWS = 75; 
      public static int COLS = 75;
  
  
      public static int cellSize = 15; 
      public static int canvasWidth = cellSize * COLS + (ROWS *4) ;
      public static int canvasHeight = cellSize * ROWS ;
      public static int gridWidth = 1;
      public static int halfGridWidth = gridWidth / 2;
  
  
      public static int cellPadding = cellSize / 5;
      public static int symbolSize = cellSize - cellPadding * 2; 
      public static int symbolStrokeWidth = 3; 
  
  
      public enum GameState{
          JOUE, NUL, CERCLE_ROUGE_GAGNE, CERCLE_BLEU_GAGNE
      }
  
      private GameState actualState;
  
  
      public enum Token{
          VIDE, CERCLE_ROUGE, CERCLE_BLEU
      }
  
      private Token actualPlayer; 
  
  
      private Token[][] board; 
      private final DrawCanvas canvas;
      private JLabel statusBar;
      private JLabel indexIndicator;
  
  
  
      public Pha(){
  
          canvas = new DrawCanvas(); 
          canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
  
  
          canvas.addMouseListener(new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent e) {
          int x = e.getX();
          int y = e.getY();
  
          int selectedRow = y / cellSize;
          int selectedCol;
              selectedCol = x / cellSize;
  
  
          if(actualState == GameState.JOUE){
              if(selectedRow &gt;= 0 &amp;&amp; selectedRow &lt; ROWS &amp;&amp; selectedCol &gt;= 0
                      &amp;&amp; selectedCol &lt; COLS &amp;&amp;
                      board[selectedRow][selectedCol] == Token.VIDE){
                  board[selectedRow][selectedCol] = actualPlayer;
                  actualiseJeu(actualPlayer, selectedRow, selectedCol); 
                  actualPlayer = (actualPlayer == Token.CERCLE_BLEU)? Token.CERCLE_ROUGE : Token.CERCLE_BLEU;
              }
          } else { 
              initGame(); 
          }
  
          repaint(); 
      }
  
    });
  
  
  
  
      statusBar = new JLabel("  ");
      statusBar.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 15));
      statusBar.setBorder(BorderFactory.createEmptyBorder(2, 5, 4, 5));
  
  
      Container cp = getContentPane();
      cp.setLayout(new BorderLayout());
      cp.add(canvas, BorderLayout.EAST);
      cp.add(statusBar, BorderLayout.NORTH);
  
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      pack(); 
      setTitle("Pha par esQmo");
      setVisible(true);
  
      board = new Token[ROWS][COLS];
      initGame();  
  }
  
      public void initGame(){
          for(int ligne = 0; ligne &lt; ROWS; ++ligne){
              for(int colonne = 0; colonne &lt; COLS; ++colonne){
                  board[ligne][colonne] = Token.VIDE;
              }
          }
          actualState = GameState.JOUE;
          actualPlayer = Token.CERCLE_ROUGE;
      }
  
      public void updateGame(Token theSeed, int ligneSelectionnee, int colonneSelectionnee) {
        if (aGagne(theSeed, ligneSelectionnee, colonneSelectionnee)) { 
           actualState= (theSeed == Token.CERCLE_ROUGE) ? GameState.CERCLE_ROUGE_GAGNE : GameState.CERCLE_BLEU_GAGNE;
        } else if (estNul()) {  
           actualState = GameState.CERCLE_BLEU_GAGNE;
        }
  
     }
   public boolean estNul() {
  
        return false; 
     }
  
         (rowSelected, colSelected) */
     public boolean aGagne(Token theSeed, int ligneSelectionnee, int colonneSelectionnee) {
        return false; 
  
        /* (planche[ligneSelectionnee][colonneSelectionnee] == Jeton.CERCLE_BLEU  
              &amp;&amp; planche[ligneSelectionnee][colonneSelectionnee - 1] == Jeton.CERCLE_BLEU
              &amp;&amp; planche[ligneSelectionnee][colonneSelectionnee + 1] == Jeton.CERCLE_BLEU
         || planche[ligneSelectionnee - 1][colonneSelectionnee] == Jeton.CERCLE_BLEU      
              &amp;&amp; planche[1][colonneSelectionnee] == theSeed 
              &amp;&amp; planche[2][colonneSelectionnee] == theSeed
         || ligneSelectionnee == colonneSelectionnee            
              &amp;&amp; planche[0][0] == theSeed
              &amp;&amp; planche[1][1] == theSeed
              &amp;&amp; planche[2][2] == theSeed
         || ligneSelectionnee + colonneSelectionnee == 2 
              &amp;&amp; planche[0][2] == theSeed
              &amp;&amp; planche[1][1] == theSeed
              &amp;&amp; planche[2][0] == theSeed);*/
  
  
  }
  
  class DrawCanvas extends JPanel{
  
          @Override
          public void paintComponent(Graphics g){ 
              super.paintComponent(g); 
              setBackground(Color.WHITE);
  
  
              g.setColor(Color.BLACK);
              for(int ligne = 1; ligne &lt; ROWS; ++ligne){
                  g.fillRoundRect(0, cellSize * ligne - halfGridWidth, canvasWidth - 1,
                          gridWidth, gridWidth, gridWidth);
              }
              for(int colonne = 1; colonne &lt; COLS; ++colonne){
                  g.fillRoundRect(cellSize * colonne - halfGridWidth, 0
                          , gridWidth, canvasHeight - 1,
                          gridWidth, gridWidth);
              }
  
  
              Graphics2D g2d = (Graphics2D)g;
              g2d.setStroke(new BasicStroke(symbolStrokeWidth,
                      BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); 
              for(int ligne = 0; ligne &lt; ROWS; ++ligne){
                  for(int colonne = 0; colonne &lt; COLS; ++colonne){
                      int x1 = colonne * cellSize + cellPadding;
                      int y1 = ligne * cellSize + cellPadding;
  
                      if(board[ligne][colonne] == Token.CERCLE_ROUGE){
                          g2d.setColor(Color.RED);
                          g2d.drawOval(x1, y1, symbolSize, symbolSize);
                          g2d.fillOval(x1, y1, symbolSize, symbolSize);
                      } else
                          if(board[ligne][colonne] == Token.CERCLE_BLEU){
                              int x2 = colonne * cellSize + cellPadding;
                              g2d.setColor(Color.BLUE);
                              g2d.drawOval(x1, y1, symbolSize, symbolSize);
                              g2d.fillOval(x2, y1, symbolSize, symbolSize);
                          }
                  }
  
              }
  
              if(actualState == GameState.JOUE){
                  if(actualPlayer == Token.CERCLE_ROUGE){
                      statusBar.setText("ROUGE, c'est votre tour");
                      statusBar.setForeground(Color.RED);
  
                  } else {
                      statusBar.setText("BLEU, c'est votre tour");
                      statusBar.setForeground(Color.BLUE);
                      statusBar.addMouseMotionListener(null);
                  }
              } else
                  if(actualState == GameState.NUL){
                      statusBar.setForeground(Color.yellow);
                      statusBar.setText("Match nul! Cliquez pour rejouer");
                  } else
                      if(actualState == GameState.CERCLE_ROUGE_GAGNE){
                          statusBar.setText("Le jouer X a remporté la partie, cliquez pour rejouer");
                          statusBar.setForeground(Color.RED);
                      } else
                          if(actualState == GameState.CERCLE_BLEU_GAGNE){
                              statusBar.setForeground(Color.BLUE);
                              statusBar.setText("Le joueur O a remporté la partie, cliquez pour rejouer");
                          }
          }
      } 
  
  
      public static void main(String[] args){
          SwingUtilities.invokeLater(() -&gt; {
              Pha pha = new Pha();
          });
      }
  }
  </code></pre>