I tried to write an OOP implementation of Conway's Game of Life in processing. However, it seems to be some other sort of automaton. Interesting, but not what I was going for. I can't find any problems with my code, that's what I'm hoping you can help with.
class Cell {
int state;
int x;
int y;
Cell update()
{
try {
int neighbors = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
neighbors += currentBoard[x+i][y+j].state;
}
}
if ((state == 1) && (neighbors < 2))return new Cell(x, y, 0);
else if ((state == 1) && (neighbors > 3))return new Cell(x, y, 0);
else if ((state == 0) && (neighbors == 3))return new Cell(x, y, 1);
else return new Cell(x, y, state);
}
catch(ArrayIndexOutOfBoundsException exception) {
return new Cell(x, y, 0);
}
}
Cell( int _x, int _y, int _s)
{
state = _s;
x = _x;
y = _y;
}
}
Cell[][] currentBoard;
Cell[][] newBoard;
void setup() {
fullScreen();
//frameRate(100);
currentBoard = new Cell[width][height];
newBoard = new Cell[width][height];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
currentBoard[i][j] = new Cell(i, j, int(random(2)));
}
}
}
void draw() {
print(frameCount);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
try {
newBoard[i][j] = currentBoard[i][j].update();
}
catch(ArrayIndexOutOfBoundsException exception) {
}
}
}
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
color setCol = color(255, 0, 0);
if (newBoard[i][j].state == 1)
{
setCol = color(0, 0, 0);
} else if (newBoard[i][j].state == 0)
{
setCol = color(255, 255, 255);
}
set(i, j, setCol);
}
}
currentBoard = newBoard;
}
What's wrong? Also, any information on the automaton I accidentally created would be cool, it creates some beautiful patterns.