title: Make cells' contents in a grid ''listen'' to closest cells
body: <p>I'm a newbie in programming and I need some lights and help.I'm developing a game in which two players have to play with tokens (say red and blue) by placing them in cells (75x75 grid). The goal is to "capture" opponent's tokens by surrounding them. (See the image, which is the actual game output, the surrounding is drawn by hand)</p>
  
  <p>To do so, I need to make the tokens "listen" to neighborhood, meaning other cells in the grid. A token has to check for itself in the grid(what is its position in the grid) and check of there is another token close to it, checks it color (blue or red) then,a in certain conditions, trigger the capturing mechanism</p>
  
  <p>[![Game board with tokens][1]][1]</p>
  
  <p>What I have done, technically:</p>
  
  <ol>
  <li>Created the grid ( Grid/board is a 2 dimensional array of Token objects.)</li>
  <li>The token (which  is an enumeration: EMPTY, BLUE_TOKEN, RED_TOKEN. </li>
  <li>A <em>currentPlayer</em> is also a Token . 
  When it's  the user turn, they select an empty cell at which they point. They place the <em>currentPlayer</em> into the grid/board at cell <em>rowSelected, colSelected</em> then repaint the canvas with the newly added cell in the grid/board.</li>
  </ol>
  
  <p>Now i'm stuck on how to make the tokens listen the next cell surrounding them in order to see if there is an opponent or an ally.</p>
  
  <p>PS: I've posted the same here, got downgraded because i didn't respect rules (ignorant I was)</p>
  
  <p>Here is my code: </p>
  
  <pre><code>import javax.swing.JFrame;
      import java.awt.*;
      import java.awt.event.MouseAdapter;
      import java.awt.event.MouseEvent;
      import javax.swing.*;
  
      public class Phagocyte extends JFrame {
  
         public static final int ROWS = 75;  
         public static final int COLS = 75;
  
  
         public static final int CELL_SIZE = 18; 
         public static final int CANVAS_WIDTH = CELL_SIZE * COLS; 
         public static final int CANVAS_HEIGHT = CELL_SIZE * ROWS;
         public static final int GRID_WIDTH = 8;                   
         public static final int GRID_WIDHT_HALF = GRID_WIDTH / 2; 
  
         // Symbols (Blue token/Red Token) are displayed inside a cell with padding from borders
         public static final int CELL_PADDING = CELL_SIZE / 5;
         public static final int SYMBOL_SIZE = CELL_SIZE - CELL_PADDING * 2;
         public static final int SYMBOL_STROKE_WIDTH = 3; 
  
         //This represent the various states of the game
         public enum GameState {
            PLAYING, DRAW, BLUE_TOKEN_WON, RED_TOKEN_WON   //Haven't created a scenario yet
         }
  
         private GameState currentState;  // The current state of the game
  
         //This represent the Tokens and cell contents
         public enum Token {
            EMPTY, BLUE_TOKEN, RED_TOKEN
         }
         private Token currentPlayer;  // The current player (whether red or blue)
  
         private Token[][] board   ; // This is the game board of cells (ROWS by COLS)
         private DrawCanvas canvas; 
         private JLabel statusBar;  
  
         /**The components of the the game and the GUI are setup here */
         public Phagocyte() {
            canvas = new DrawCanvas();  
            canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
            canvas.addMouseListener(new MouseAdapter() {
               @Override
               public void mouseClicked(MouseEvent e) {  
                  int mouseX = e.getX();
                  int mouseY = e.getY();
  
                  // Here to get the row and column that is clicked
                  int rowSelected = mouseY / CELL_SIZE;
                  int colSelected = mouseX / CELL_SIZE;
  
                  if (currentState == GameState.PLAYING) {
                     if (rowSelected &gt;= 0 &amp;&amp; rowSelected &lt; ROWS &amp;&amp; colSelected &gt;= 0
                           &amp;&amp; colSelected &lt; COLS &amp;&amp; board[rowSelected][colSelected] == TOKEN.EMPTY) {
                        board[rowSelected][colSelected] = currentPlayer; 
                        updateGame(currentPlayer, rowSelected, colSelected); 
  
                        // Here's to switch player
                        currentPlayer = (currentPlayer == Token.BLUE_TOKEN) ? Token.RED_TOKEN : Token.BLUE_TOKEN;
                     }
                  } else {       
                     initGame();
                  }
                  // Drawing canvas are refresh
                  repaint();
               }
            });
  
            // Setup the status bar (JLabel) to display status message
            statusBar = new JLabel("  ");
            statusBar.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 15));
            statusBar.setBorder(BorderFactory.createEmptyBorder(2, 5, 4, 5));
  
            Container cp = getContentPane();
            cp.setLayout(new BorderLayout());
            cp.add(canvas, BorderLayout.CENTER);
            cp.add(statusBar, BorderLayout.NORTH);
  
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();  
            setTitle("Phagocyte by esQmo");
            setVisible(true);
  
            board = new Token[ROWS][COLS];
            initGame();
         }
  
         /** The game board contents and the status are initialised here*/
         public void initGame() {
            for (int row = 0; row &lt; ROWS; ++row) {
               for (int col = 0; col &lt; COLS; ++col) {
                  board[row][col] = Token.EMPTY;  
               }
            }
            currentState = GameState.PLAYING; 
            currentPlayer = Token.RED_TOKEN;       
         }
  
         /*this part need some improvements since hasWon and isDraw are not yet definied*/
         public void updateGame(Token theToken, int rowSelected, int colSelected) {
            if (hasWon(theToken, rowSelected, colSelected)) {  
               currentState = (theToken == Token.RED_TOKEN) ? GameState.RED_TOKEN_WON : GameState.BLUE_TOKEN_WON;
            } else if (isDraw()) {
               currentState = GameState.DRAW;
            }
  
         }
  
         /** This is supposed to return true if it is a draw (no more empty cell for exemple) */
         /** need to be improved **/
         /* public boolean isDraw() {
            for (int row = 0; row &lt; ROWS; ++row) {
               for (int col = 0; col &lt; COLS; ++col) {
                  if (board[row][col] == Token.EMPTY) {
                     return false; 
                  }
               }
            }
            return true;  
         } */
  
         /**Need to implement a Win scenario as well **/
  
         public boolean hasWon(Token theToken, int rowSelected, int colSelected){
          //   
         }
  
        class DrawCanvas extends JPanel {
            @Override
            public void paintComponent(Graphics g) {  
               super.paintComponent(g);    
               setBackground(Color.WHITE); 
  
               //Grid lines
               g.setColor(Color.LIGHT_GRAY);
               for (int row = 1; row &lt; ROWS; ++row) {
                  g.fillRoundRect(0, CELL_SIZE * row - GRID_WIDHT_HALF,
                        CANVAS_WIDTH-1, GRID_WIDTH, GRID_WIDTH, GRID_WIDTH);
               }
               for (int col = 1; col &lt; COLS; ++col) {
                  g.fillRoundRect(CELL_SIZE * col - GRID_WIDHT_HALF, 0,
                        GRID_WIDTH, CANVAS_HEIGHT-1, GRID_WIDTH, GRID_WIDTH);
               }
  
               // This draw the Tokkens in the cells if they are not empty
  
               Graphics2D g2d = (Graphics2D)g;
               g2d.setStroke(new BasicStroke(SYMBOL_STROKE_WIDTH, BasicStroke.CAP_ROUND,
                     BasicStroke.JOIN_ROUND));
               for (int row = 0; row &lt; ROWS; ++row) {
                  for (int col = 0; col &lt; COLS; ++col) {
                     int x1 = col * CELL_SIZE + CELL_PADDING;
                     int y1 = row * CELL_SIZE + CELL_PADDING;
                     if (board[row][col] == Token.RED_TOKEN) {
                        int x2 = (col + 1) * CELL_SIZE - CELL_PADDING;
                        int y2 = (row + 1) * CELL_SIZE - CELL_PADDING;
                        g2d.setColor(Color.RED);
                        g2d.drawOval(x1, y1, SYMBOL_SIZE, SYMBOL_SIZE);
                        g2d.fillOval(x1, y1, SYMBOL_SIZE, SYMBOL_SIZE);
                     } else 
                         if (board[row][col] == Token.BLUE_TOKEN) {
                        g2d.setColor(Color.BLUE);
                        g2d.drawOval(x1, y1, SYMBOL_SIZE, SYMBOL_SIZE);
                        g2d.fillOval(x2, y1, SYMBOL_SIZE, SYMBOL_SIZE);
                     }
                  }
               }
  
               // Print status-bar message
               if (currentState == GameState.PLAYING) {
                  statusBar.setForeground(Color.BLACK);
                  if (currentPlayer == Token.RED_TOKEN) {
                     statusBar.setText("Red, it's your move");
                     statusBar.setForeground(Color.RED);
                  } else {
                     statusBar.setText("Blue, it's your move");
                          statusBar.setForeground(Color.BLUE);
                          statusBar.addMouseMotionListener(null);
                  }
               } else if (currentState == GameState.DRAW) {
                  statusBar.setForeground(Color.RED);
                  statusBar.setText("It's a draw!");
               } else if (currentState == GameState.RED_TOKEN_WON) {
                  statusBar.setForeground(Color.RED);
                  statusBar.setText("Red wow!");
               } else if (currentState == GameState.BLUE_TOKEN_WON) {
                  statusBar.setForeground(Color.BLUE);
                  statusBar.setText("Blue Won! ");
               }
            }
         }
  
  
          public static void main(String[] args){
              SwingUtilities.invokeLater(() -&gt; {
                  Phagocyte phagocyte = new Phagocyte();
  
            });
         }
  }
  </code></pre>
  
  <p><em>I'm unable to post image, due to my reputation</em> :'(</p>