Hi, I'm having to write Conway's game of life for school and I'm having a little trouble with it. Whenever it runs through the code, all of the 'cells' move to the left a bunch and warp around the screen.. I'm not sure why.. Any help would be great! Thanks!

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;

void seedGrid();
void runTick();
void outputGrid();
void setCellState();

char lifeGrid[80][22]={'*'};

int main(int argc, char *argv[])
{
    srand( time(NULL) );
    seedGrid();
    outputGrid();
    system("PAUSE");
    
    do
    {
    system("CLS");
    runTick();
    outputGrid();
    setCellState();
    outputGrid();
    system("PAUSE");
    }while(true);
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


//Runs rules and sets cell's status accordingly. 
void seedGrid()
{
    int randNum = 0;
    
    for( int i=0; i < 80; i++)
    {
        for( int j=0; j < 22; j++)
        {
             randNum = rand() % 100;
             
             switch( randNum)
             {
                 case 1:
                      lifeGrid[i][j] = '*';
                      break;
                 default: 
                      lifeGrid[i][j] = ' ';
             } 
        }
    }
}
    
void runTick()
{
    int numSurround = 0;
     
    for( int i=0; i < 80; i++)
    {
        for( int j=0; j < 22; j++)
        {
             numSurround = 0;
             
             if( lifeGrid[i+1][j] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i-1][j] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i][j+1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i+1][j-1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i+1][j+1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i-1][j-1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i+1][j-1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i-1][j+1] == '*')
             {
                 numSurround +=1;
             } 
             
             cout << numSurround << endl;
             
             if( numSurround >= 2 && numSurround <= 3 && lifeGrid[i][j]== ' ' )
             {
                lifeGrid[i][j] = 'a';
             }  
             else if(( numSurround < 2) || ( numSurround > 3))
             {
                 lifeGrid[i][j] = 'd';   
             }     
        }
    }
}

void setCellState()
{
    for( int i=0; i < 80; i++)
    {
        for( int j=0; j < 22; j++)
        {
             if( lifeGrid[i][j] == 'a')
             {
                 lifeGrid[i][j] = '*';
             }
             else if( lifeGrid[i][j] == 'd')
             {
                 lifeGrid[i][j] = ' ';
             }
        }
    }
}

void outputGrid()
{
    for( int i=0; i < 80; i++)
    {
        for( int j=0; j < 22; j++)
        {
             cout << setw(1) << lifeGrid[i][j];
        }
    }
}

Edited 5 Years Ago by tofugamer: n/a

It won't let me edit my original post, so here is an update with where I am now. I still get the same problem, however I think it is something wrong with my display..

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;

void seedGrid();
void runTick();
void outputGrid();
void setCellState();

const int gridH = 22;
const int gridW = 40;

char lifeGrid[gridH][gridW]={'*'};

int main(int argc, char *argv[])
{
    srand( time(NULL) );
    seedGrid();
    outputGrid();
    system("PAUSE");
    
    do
    {
    system("CLS");
    runTick();
    setCellState();
    outputGrid();
    system("PAUSE");
    }while(true);
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


//Runs rules and sets cell's status accordingly. 
void seedGrid()
{
    int randNum = 0;
    
    for( int y=0; y < gridH; y++)
    {
        for( int x=0; x < gridW; x++)
        {
             randNum = rand() % 100;
             
             switch( randNum)
             {
                 case 1:
                      lifeGrid[y][x] = '*';
                      break;
                 default: 
                      lifeGrid[y][x] = ' ';
             } 
        }
    }
}
    
void runTick()
{
    int numSurround = 0;
     
    for( int y=0; y < gridH; y++)
    {
        for( int x=0; x < gridW; x++)
        {
             numSurround = 0;
             
             if( lifeGrid[y+1][x] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[y-1][x] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[y][x+1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[y+1][x-1] == '*')
             {
                 numSurround +=1;    
             }
             
             
             if( lifeGrid[y+1][x+1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[y-1][x-1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[y+1][x-1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[y-1][x+1] == '*')
             {
                 numSurround +=1;
             } 
             
             //cout << numSurround << endl;
             
             if( numSurround >= 2 && numSurround <= 3 && lifeGrid[y][x]== ' ' )
             {
                lifeGrid[y][x] = 'a';
             }  
             if(( numSurround < 2) || ( numSurround > 3))
             {
                 lifeGrid[y][x] = 'd';   
             }     
        }
    }
}

void setCellState()
{
    for( int y=0; y < gridH; y++)
    {
        for( int x=0; x < gridW; x++)
        {
             if( lifeGrid[y][x] == 'a')
             {
                 lifeGrid[y][x] = '*';
             }
             if( lifeGrid[y][x] == 'd')
             {
                 lifeGrid[y][x] = ' ';
             }
        }
    }
}

void outputGrid()
{
    for( int y=0; y < gridH; y++)
    {
        for( int x=0; x < gridW; x++)
        {
             cout << lifeGrid[y][x];
        }
        cout << endl;
    }
}

Edited 5 Years Ago by tofugamer: n/a

One thing is, you need to treat the outer border (column 0 and 21, row 0 and 21)as special so you don't go out of bound on your array. For example on line 100, in cell (0,0) it tries to read lifeGrid[-1][-1]. You may be lucking out and there might be junk at that address, but usually it should just crash. Cells in the top row should only check the row below and each side, etc.

Thank you! That and the fact that I was forgetting to check one of the cells and instead checking one twice were my problems! :) Thanks!

This article has been dead for over six months. Start a new discussion instead.