We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Conway's Game of Life
Page Index Toggle Pages: 1
Conway's Game of Life (Read 938 times)
Conway's Game of Life
Mar 15th, 2008, 12:43am
 
I'm trying to relearn some programming skills by coding up a small Game of Life applet...but I'm stuck!

For those familiar with the Game of Life, I'm stuck with the 'special cases' of the corners and sides of the Game...where there are not 8 neighbors, but rather less...what is a good way to check a cells neighbors without having to have tons of special checking for corners and sides?

I know theres a code example in the Learning or Reference section, but it had 0 documentation and made absolutely no sense to me.
Re: Conway's Game of Life
Reply #1 - Mar 15th, 2008, 12:56pm
 
The example in the learning section wraps around the edges of the board - thus checking for the sides never happens.

http://processing.org/learning/topics/conway.html

Checking to the right:

(x + 1) % width

Checking to the left:

(x + width - 1) % width

There are always 8 neighbours in the example.
Re: Conway's Game of Life
Reply #2 - Mar 16th, 2008, 2:27am
 
Wrapping around the edges of the board is the simplest way.
Otherwise if a cell is on the edge, the board has to be enlarge.

Another way would be to impose a border condition, like having an outer ring of immutable cells. But this is not satisfactory as those cells do not follow the rules of the games.
Re: Conway's Game of Life
Reply #3 - Mar 16th, 2008, 11:09pm
 
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.
Re: Conway's Game of Life
Reply #4 - Mar 17th, 2008, 2:33am
 
I quickly check your program, and have 2 comments:
--- size() must be the 1st line in setup()
--- when the grid is created, "new cell()" is done for each cell, but each "new cell()" execute an "update()" which execute a "countNeighbors()" over cells not created yet.
Re: Conway's Game of Life
Reply #5 - Mar 20th, 2008, 12:12am
 
Some progress has been made, but its still not doing what its supposed to. I've narrowed the problem down to the code that checks for neighbors on the left side of a given cell.

Can anyone see whats wrong with this code?

Code:

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
Re: Conway's Game of Life
Reply #6 - Mar 20th, 2008, 5:40am
 
In Life, all cell updates are simultaneous (on each generation).
If instead cells are updated one by one, the result will be different, cause modifying a cell's state will change the count for it's later neighbors (but not for it's sooner neighbors).
Because of that, usually two copies of the grid are used. One is the reference one, the other is a scratch pad which will be copied into the reference one only when all updates are done.
Page Index Toggle Pages: 1