Game of Life Clone

closed account (2NywAqkS)
Just for practice, I'm making a colone of Conway's Game of Life.
At the start of the program I fill the window with 1024 cells like this:

1
2
3
4
5
6
7
for (int i = 0; i < 32; i++)
{
	for (int j = 0; j < 32; j++)
	{
		cellVector.push_back(Cell(j, i));
	}
}


Then every step each cell is calcualted like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Cell::Calculate()
{
	if (cellVector[(y-1)*32+x-1].alive) nbrs ++;
	if (cellVector[(y-1)*32+x].alive) nbrs ++;
	if (cellVector[(y-1)*32+x+1].alive) nbrs ++;
	if (cellVector[y*32+x-1].alive) nbrs ++;
	if (cellVector[y*32+x+1].alive) nbrs ++;
	if (cellVector[(y+1)*32+x-1].alive) nbrs ++;
	if (cellVector[(y+1)*32+x].alive) nbrs ++;
	if (cellVector[(y+1)*32+x+1].alive) nbrs ++;
	if (nbrs < 2) alive = false;
	if (nbrs > 3) alive = false;
	if (nbrs == 3) alive = true;
	nbrs = 0;
	gen = prevGen;
}


This works but not quite like how I expected. You do get weird patterns and movements but it doesn't seem to follow the traditional rules. What's more I occasionally get and unhandled exception error. I think this is because sometimes it has to calculate place that don't exist in the vector array. Can someone please help me with this.

If I haven't made myself very clear or haven't provided enough code, I've uploaded the sorce here: http://www.wizardchip.com/main.cpp

Thanks,
Rowan.
I just made a little GLUT program, GLUT windows/callback is much easier than windows.h. Here's a site I found: http://www.lighthouse3d.com/tutorials/glut-tutorial/

Not sure about your drawing pattern.

x and y are somewhere between 0 and 32, right? Why the 32 in your operations? You must go beyond 1024 at some point.

Maybe try checking values via a MessageBox for each operation:
1
2
if ((y-1)*32+x-1 >= 1024) MessageBox(NULL, L"(y-1)*32+x-1", NULL, NULL);
else if (cellVector[(y-1)*32+x].alive) nbrs ++;


Just to figure out which one is going over 1023

Last edited on
closed account (2NywAqkS)
ok, thanks LowestOne. That seemed to clear up the edge problems but the rules are still not right. Any ideas?
Could you please explain what the pattern is supposed to do? (in Calculate())

Also, in Cell, what are the variables nbrs and prevGen for?

I'm getting that x and y are the position and alive dictates which color to color the cell

And one more thing... I'm surprised you're not getting any segmentation faults.
 
if (cellVector[(y-1)*32+x-1].alive) nbrs ++;


Just this first line, if y was 0 and x was less than 32 you'd have yourself an index access using a negative index.

Since this:
1
2
3
4
5
6
7
for (int i = 0; i < 32; i++)
	{
		for (int j = 0; j < 32; j++)
		{
			cellVector.push_back(Cell(j, i));
		}
	}

is how you initialize the cells, I don't see how you don't end up getting this segmentation fault.
closed account (2NywAqkS)
I'm making a clone of Conway's Game of Life
http://en.wikipedia.org/wiki/Conway's_Game_of_Life

And The problem you described was fixed thanks to LowestOne.
LowestOne showed you that your indicies may be going over 1023, I was suggesting that they may be negative (less than 0).

Cool program, never heard about it until now.
closed account (2NywAqkS)
I found the problem. Without going into to much detail, I found you have to separate the getting neighbours function and deciding on whether something is alive or not.
Topic archived. No new replies allowed.