I am a beginner to java and I've coded the Game of Life! For those not familiar, the game entails creating a grid of specified dimensions with each box in the grid being either dead or alive. The grid starts with a random dead or alive state for each box. New generations have new dead/alive boxes based on the following conditions: If there are 3 alive boxes neighbouring any dead box, it becomes alive. If there are less than 2 alive boxes neighbouring an alive box, that box dies. It also dies if there are more than 3 alive boxes around it.
The way I am approaching this is by making a boolean[][]
array grid with true representing an alive box and false representing a dead one. The view class takes this boolean grid and turns it into a grid of boxes that are either coloured white or red (white = dead)(red = alive).
Game Class:
import java.util.Arrays;
import java.util.Random;
public class Game {
private boolean[][] grid;
private int genCount;
private int width;
private int height;
public Game(int i, int j) {
genCount = 0;
if (i < 1 || j < 1) {
throw new IllegalArgumentException("grid too small!");
}
if (i > 100 || j > 100) {
throw new IllegalArgumentException("grid too big!");
}
width = i;
height = j;
grid = new boolean[j][i];
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public boolean[][] getGrid() {
return grid;
}
public void setGrid(boolean[][] grid) {
this.grid = grid;
}
public int getGen() {
return genCount;
}
public void setGen(int gen) {
this.genCount = gen;
}
public void randomGrid(double probability) {
Random r = new Random();
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
grid[j][i] = Math.random() < probability;
}
}
}
public boolean[][] makeCopy() {
// making a copy of the current grid, avoiding aliasing.
boolean[][] nGrid = new boolean[height][width];
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
nGrid[j][i] = grid[j][i];
}
}
return nGrid;
}
public void newGen() {
boolean[][] nGrid = makeCopy();
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
if (grid[j][i] == false) {
// new life, due to having 3 neighbours!
if (getNeighbours(j, i) == 3) {
nGrid[j][i] = true;
}
} else {
// isolation death!
if (getNeighbours(j, i) <= 1) {
nGrid[j][i] = false;
}
// overcrowing death!
if (getNeighbours(j, i) >= 4) {
nGrid[j][i] = false;
}
}
}
}
genCount++;
grid = nGrid;
}
public int getNeighbours(int j, int i) {
int count = 0;
int jMax = grid[0].length - 1;
int iMax = grid[1].length - 1;
// checking up, down, right, left, allowing wrapping
if ((j < jMax && grid[j + 1][i]) || (j == jMax && grid[0][i])) {
count++;
}
if ((j > 0 && grid[j - 1][i]) || (j == 0 && grid[jMax][i])) {
count++;
}
if ((i < iMax && grid[j][i + 1]) || (i == iMax & grid[j][0])) {
count++;
}
if ((i > 0 && grid[j][i - 1]) || (i == 0 && grid[j][iMax])) {
count++;
}
// checking diagonals, allowing wrapping
if ((j < jMax && i < iMax && grid[j + 1][i + 1]) || (j == jMax && i < iMax && grid[0][i + 1])
|| (j < jMax && i == iMax && grid[j + 1][0]) || (j == jMax && i == iMax && grid[0][0])) {
count++;
}
if ((j < jMax && i > 0 && grid[j + 1][i - 1]) || (j == jMax && i > 0 && grid[0][i - 1])
|| (j < jMax && i == 0 && grid[j + 1][iMax]) || (j == jMax && i == 0 && grid[0][iMax])) {
count++;
}
if ((j > 0 && i < iMax && grid[j - 1][i + 1]) || (j == 0 && i < iMax && grid[jMax][i + 1])
|| (j > 0 && i == iMax && grid[j - 1][0]) || (j == 0 && i == iMax && grid[jMax][0])) {
count++;
}
if ((j > 0 && i > 0 && grid[j - 1][i - 1]) || (j == 0 && i > 0 && grid[jMax][i - 1])
|| (j > 0 && i == 0 && grid[j - 1][iMax]) || (j == 0 && i == 0 && grid[jMax][iMax])) {
count++;
}
return count;
}
}
View Class:
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class View extends JPanel {
public void paint(Graphics g) {
Game game = new Game(10, 10);
game.randomGrid(0.2);
game.newGen();
boolean[][] grid = game.getGrid();
g.setColor(Color.red);
for (int j = 0; j < game.getHeight(); j++) {
for (int i = 0; i < game.getWidth(); i++) {
g.drawRect(100 + (50 * i), 100 + (50 * j), 50, 50);
if (grid[j][i]) {
g.fillRect(100 + (50 * i), 100 + (50 * j), 50, 50);
}
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new View());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 1000);
frame.setVisible(true);
}
}