Search code examples
javaconways-game-of-life

I dont get why this code for my Game of Life doesnt work. Can anyone help me?


The Problem is when I start the code the game kinda works but it doesnt behave like the normal conwys game of life. So I thought my logic section where the neighbours are checked doesnt work as I it should. After long debugging I still couldnt find the mistake. :)

import java.awt.*;

import javax.swing.*;

public class GUI extends JPanel {

private Field field;
private JFrame frame;

public GUI() {
    int width = 800, height = 800;
    frame = new JFrame();
    frame.setSize(width, height);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.add(this);
    field = new Field(this);
}

public static void main(String[] args) {
    new GUI();

}

public void paint(Graphics g) {
    super.paint(g);
    int j = 0;

    for (int i = 0; i < 400; i++) {
        j = 0;
        for (j = 0; j < 400; j++) {
            if (field.getAlive(i, j)) {

                g.drawRect(2 * j, 2 * i, 2, 2);
                g.fillRect(2 * j, 2 * i, 2, 2);
            }
        }

    }
    check();

}

public void check() {
    int j = 0;
    for (int i = 1; i < 399; i++) {
        j = 0;
        for (j = 1; j < 399; j++) {
            if (field.getAlive(i, j)) {

                int x = 0;
                if (field.getAlive(i - 1, j - 1)) {
                    x++;
                }
                if (field.getAlive(i, j - 1)) {
                    x++;
                }
                if (field.getAlive(i + 1, j - 1)) {
                    x++;
                }
                if (field.getAlive(i + 1, j)) {
                    x++;
                }
                if (field.getAlive(i - 1, j)) {
                    x++;
                }
                if (field.getAlive(i - 1, j + 1)) {
                    x++;
                }
                if (field.getAlive(i, j + 1)) {
                    x++;
                }
                if (field.getAlive(i + 1, j + 1)) {
                    x++;
                }

                if (x < 2 || x > 3) {
                    field.setAlive(i, j, false);
                } 

            } else {


                if (!field.getAlive(i, j)) {



                    int x = 0;


                    if (field.getAlive(i - 1, j - 1)) {
                        x++;
                    }
                    if (field.getAlive(i, j - 1)) {
                        x++;
                    }
                    if (field.getAlive(i + 1, j - 1)) {
                        x++;
                    }
                    if (field.getAlive(i + 1, j)) {
                        x++;
                    }
                    if (field.getAlive(i - 1, j)) {
                        x++;
                    }
                    if (field.getAlive(i - 1, j + 1)) {
                        x++;
                    }
                    if (field.getAlive(i, j + 1)) {
                        x++;
                    }
                    if (field.getAlive(i + 1, j + 1)) {
                        x++;
                    }

                    if (x == 3) {
                        field.setAlive(i, j, true);

                    }

                }
            }
        }
    }
    repaint();
   }

  }



   public class Field {
   private GUI gui;
   private int fieldLength = 1;
   private boolean[][] alive;

  public Field(GUI gui) {
    this.gui = gui;
    alive = new boolean[400][400];
    for(int i = 100 ; i<110; i++){alive[100][i] = true;    }
    for(int i = 100 ; i<110; i++){alive[i][100] = true ;   }
  }

 public boolean getAlive(int i, int j) {
    return alive[i][j];
  }

 public void setAlive(int i, int j, boolean alive) {
    this.alive[i][j] = alive;
  }

 }

Solution

  • Your problem is caused because you are using the same board instance for checking the rules and for updating. Every iteration you should create a new board based off the old one instead of re-using the old board.

    You should maintain two boards at any given time. One to read from and one to write to then just copy the written one at the end of each iteration