Well, I thought I understood, but it looks like I don't. I'm using the terms "row" and "col" to refer to the position of a cell in the overall Cell List. I then use the main "draw" function to update each cell by checking their neighbors and rendering (just drawing the correctly colored square).
I don't know if this is too much for one of you to look at, but I'd appreciate any help!
Code:
int GridWidth = 60;
int GridHeight = 60;
int CellSize = 5;
Cell[][] CellList = new Cell[GridHeight][GridWidth];
boolean[][] Ons = new boolean[GridHeight][GridWidth];
void setup() {
background(100);
size(GridWidth*CellSize,GridHeight*CellSize);
Ons[20][20] = true;
Ons[20][21] = true;
Ons[20][22] = true;
// Create the grid
for(int i=0; i < GridWidth; i++) {
for(int j=0; j < GridHeight; j++) {
CellList[j][i] = new Cell( j,i,CellSize,Ons[j][i] );
}
}
noStroke();
frameRate(1);
}
void draw() {
/*
for(int i=0; i < GridWidth; i++) {
for(int j=0; j < GridHeight; j++) {
CellList[j][i].update();
}
} */
}
Code:
class Cell {
int row, col; // allocated spot in row/col
int x, y; // actual coordinates on screen
int Size; // size
boolean State; // on/off
Cell( int _row, int _col, int _size, boolean _state ) {
row = _row;
col = _col;
Size = _size;
State = _state;
x = col*Size;
y = row*Size;
update();
}
void render() {
if(State)
fill(#FFFFFF);
rect(x,y,Size,Size);
fill( 100 );
}
void update() {
// check all neighbors
// apply rules of game
int n = countNeighbors();
if(State==true) {
if(!(n==3) && !(n==2)) State = false;
} else {
if(n==3) State = true;
}
render();
println(State);
}
int countNeighbors() {
int n = 0;
if(CellList[row][(col+1)%GridWidth].getState()) n++; // right middle
if(CellList[(row+1)%GridHeight][(col+1)%GridWidth].getState()) n++; // right bottom
if(CellList[(row+GridHeight-1)%GridHeight][(col+1)%GridWidth].getState()) n++; // right top
if(CellList[row][(col+GridWidth-1)%GridWidth].getState()) n++; // left middle
if(CellList[(row+1)%GridHeight][(col+GridWidth-1)%GridWidth].getState()) n++; // left bottom
if(CellList[(row+GridHeight-1)%GridHeight][(col+GridWidth-1)%GridWidth].getState()) n++; // left top
return n;
}
boolean getState() {
return State;
}
}
The error I'm getting is a NullPointerException at the first line of the logic inside countNeighbors. All signs point to an error on my part inside the logic of counting neighbors.