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];
}
}
}