Sure, here's an implementation of the Game of Life using a Cell class:
class Cell {
int x, y;
boolean alive, nextState;
int size;
Cell(int x, int y, int size) {
this.x = x;
this.y = y;
this.size = size;
alive = false;
nextState = false;
}
void update(boolean[][] grid) {
int neighbors = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue;
int row = x + i;
int col = y + j;
if (row < 0) row = grid.length - 1;
if (row > grid.length - 1) row = 0;
if (col < 0) col = grid[0].length - 1;
if (col > grid[0].length - 1) col = 0;
if (grid[row][col]) neighbors++;
}
}
if (alive) {
if (neighbors < 2) nextState = false;
else if (neighbors > 3) nextState = false;
else nextState = true;
} else {
if (neighbors == 3) nextState = true;
else nextState = false;
}
}
void draw() {
if (alive) fill(0);
else fill(255);
rect(x * size, y * size, size, size);
}
void setState(boolean state) {
alive = state;
}
void nextState() {
alive = nextState;
}
}
int rows = 50;
int cols = 50;
int cellSize = 10;
Cell[][] cells = new Cell[rows][cols];
void setup() {
size(cols * cellSize, rows * cellSize);
noStroke();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cells[i][j] = new Cell(i, j, cellSize);
}
}
randomize();
}
void draw() {
background(255);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cells[i][j].update(getGrid());
cells[i][j].draw();
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cells[i][j].nextState();
}
}
}
boolean[][] getGrid() {
boolean[][] grid = new boolean[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
grid[i][j] = cells[i][j].alive;
}
}
return grid;
}
void mousePressed() {
int row = mouseY / cellSize;
int col = mouseX / cellSize;
cells[row][col].setState(!cells[row][col].alive);
}
void randomize() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (random(1) < 0.5) {
cells[i][j].setState(true);
} else {
cells[i][j].setState(false);
}
}
}
}
In this implementation, the Cell class represents an individual cell in the grid. It keeps track of its position, size, and whether it is currently alive or not (alive), as well as its state in the next generation (nextState). The update() method computes the next state of the cell based on the rules of the Game of Life and stores it in nextState, while the draw() method draws the cell on the screen using rectangles. The setState() method allows external code (such as the mousePressed() function) to set the state of the cell, and the nextState() method updates the alive field based on the value of nextState.
In the setup() function, we create a 2D array of Cell objects and initialize them with random values using a randomize() function. In the draw() function, we iterate over all cells, update their state using the update() method, and draw them using the draw() method. Finally, we update the alive field of each cell based on its nextState using the nextState() method. The mousePressed() function allows the user to toggle the state of a cell by clicking on it.