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
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.