I have implemented Conway's Game of Life simulation. What can I improve? How could I structure my programme better? All suggestions would be appreciated.
World.java:
public class World {
private int WORLDSIZE;
private final int DEAD = 0;
private final int ALIVE = 1;
private int world[][];
private int worldCopy[][];
public World(int WORLDSIZE) {
this.WORLDSIZE = WORLDSIZE;
this.world = new int[WORLDSIZE][WORLDSIZE];
this.worldCopy = new int[WORLDSIZE][WORLDSIZE];
}
public int getWorldSize() {
return this.WORLDSIZE;
}
public int[][] getWorld() {
return this.world;
}
public int getCellState(int x, int y) {
if (world[x][y] == ALIVE)
return ALIVE;
else
return DEAD;
}
public int getAlive() {
return this.ALIVE;
}
public int getNeighborCells(int x, int y) {
int neighbors = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (world[i][j] == ALIVE && (x != i || y != j))
neighbors += 1;
}
}
return neighbors;
}
public void createRandomWorld() {
for (int y = 0; y < WORLDSIZE; y++) {
for (int x = 0; x < WORLDSIZE; x++) {
if (Math.random() > 0.9) {
world[x][y] = ALIVE;
} else
world[x][y] = DEAD;
}
}
}
public int[][] applyRules() {
for (int i = 1; i < WORLDSIZE - 1; i++) {
for (int j = 1; j < WORLDSIZE - 1; j++) {
int neighbors = getNeighborCells(i, j);
if (world[i][j] == ALIVE) {
if ((neighbors < 2) || (neighbors > 3)) {
worldCopy[i][j] = DEAD;
}
if (neighbors == 2 || neighbors == 3) {
worldCopy[i][j] = ALIVE;
}
}
if (world[i][j] == 0 && neighbors == 3) {
worldCopy[i][j] = ALIVE;
}
}
}
return worldCopy;
}
public void updateWorld() {
world = applyRules();
}
Board.java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class Board extends JPanel {
private int width = 250;
private int height = 250;
private World world;
private final int CELLSIZE = 5;
public Board(World world) {
this.world = world;
world.createRandomWorld();
setPreferredSize(new Dimension(width, height));
setBackground(Color.YELLOW);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < world.getWorldSize(); i++) {
for (int j = 0; j < world.getWorldSize(); j++) {
if (world.getCellState(i, j) == world.getAlive()) {
g.setColor(Color.RED);
g.fillRect(i * CELLSIZE, j * CELLSIZE, CELLSIZE, CELLSIZE);
}
else {
g.setColor(Color.YELLOW);
g.fillRect(i * CELLSIZE, j * CELLSIZE, CELLSIZE, CELLSIZE);
}
}
}
}
}
Simulation.java:
import java.util.Timer;
import java.util.TimerTask;
public class Simulation {
private World world;
private Board board;
private Timer timer = new Timer();
private final int period = 100;
public Simulation(Board board, World world) {
this.board = board;
this.world = world;
this.simulate();
}
public void simulate() {
timer.schedule(new TimerTask() {
@Override
public void run() {
world.applyRules();
world.updateWorld();
board.repaint();
}
}, 0, period);
}
}
Main.java:
import javax.swing.JFrame;
public class Main {
private World world;
private Board board;
private Simulation simulation;
private JFrame frame = new JFrame("GameOfLife");
private final int width = 250, height = 250;
public Main() {
world = new World(50);
board = new Board(world);
simulation = new Simulation(board, world);
frame.setSize(width, height);
frame.add(board);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
Thank you in advance.