Home > OS >  What is a good and simple way to get state of neighbours in a 2d array in java for conway's gam
What is a good and simple way to get state of neighbours in a 2d array in java for conway's gam

Time:05-26

I am about to learn java right now (I came from C ), and I am trying myself in GUI-Programming.

My goal is to create a playable version of "conway's game of life" (which is a good beginner project i think).

What i have managed to do is, that i stored all of my cells in a 2d array (Here is my gode for that)

public class Panel extends JPanel
{
    private int PanelX = 1777, PanelY = 1000;
    public boolean[][] grid = new boolean[192][108];
       
    public Panel()
    {
        this.setBackground(Color.black);
        this.setSize(PanelX, PanelY);
        this.setVisible(true);  
    }

    @Override
    protected void paintComponent(Graphics g)
     {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setColor(Color.green);     
        prepareGrid();                                  // sets the entire index of grid[][] to zero for start and reset 
        drawGrid(g2d);                                  // draws the grid 
    }

    private void prepareGrid()
    {
        for (int i = 0; i<191; i  )
        {
            for (int t = 0; t<107; t  )
            {
                grid[i][t] = false;
            }
        }
    }

    private void drawGrid(Graphics g)
    {
        for (int i=0; i<=191; i  )
        {
            for (int t=0; t<=107; t  )
            {
                if (grid[i][t] == false) 
                {
                    g.drawRect(i*10, t*10, 10, 10);
                }                                                                              
                if(grid[i][t] == true) 
                {
                    g.fillRect(i*10, t*10, 10, 10);
                }
            }
        }
    } 
}

So what it does it creates an 2 dimensional array which stores all cells as either false = no cell, or true = cell. When an array index (for example grid[100][100] is true, it draws a filled rectangle on that position as a "living cell".

To implement the game and the rules of the game now, I need a way to access all the neighbor positions of an index of that 2d array, but I do not know how to to that. Could anyone help me with that ? :)

-> And if you have major optimizations for my code, feel free to write them as well.

CodePudding user response:

Adding to markspace's answer, it is important to ensure that the counting of alive cells does not interfere with their updating (letting cells be born or die) in the same generation. In other words: If you count 3 neighbors of a dead cell, you must not immediately set it to alive, because it must still be counted as dead for its other neighbors.

You could, for example, first count the neighbors for every cell and then, in a second nested loop, update every cell according to its previously counted number of neighbors. But you can do better with a more elaborate algorithm: you do not absolutely need an array of counters of the same size as your grid.

CodePudding user response:

It's just counting, it's not really hard. Use two loops to count all eight adjacent cells, skip the middle one when you find that cell.

   /**
    * Returns the number of "cells" that are alive and adjacent to
    * the cell at the given X,Y coordinates.
    * 
    */
   public int aliveAdjacent( int x, int y ) {
      int count = 0;
      for( int xd = -1; xd <= 1; xd   )
         for( int yd = -1; yd <= 1; yd   ) {
            if( xd == 0 && yd == 0 ) continue;
            if( isAlive( wrap(x xd, width), wrap(y yd,heigth) ) ) count  ;
         }
      return count;
   }
  • Related