C++ gameOfLife Problem

hey guys i can't seem to find out what is wrong with this code, it would compile, but it won't switch to next generation.
#include <iostream>
#include <cstdlib>


using namespace std;
const int row = 20;
const int column = 20;
char a[column][row];
char b[column][row];
void start(char a[][row]);
void display(char a[][row]);
void preset(char a[][row]);
int countLife(char a[][row]);
void checkLife(char a[][row], char b[][row]);


int main()
{
int count;
int num;
start(a);
start(b);
preset(a);
checkLife(a,b);


return 0;

}

void start(char a[][row])
{

for(int i=0; i<column; i++)
{
for(int j=0; j<row; j++)
{
a[i][j]='.';
}
}
}
void display(char a[][row])
{
for(int i=0; i<column; i++)
{
for(int j=0; j<row; j++)
{
cout<<a[i][j];
}
cout<<endl;
}
}
void preset(char a[][row])
{
int board;

cout<< "please enter what board you would like to run(1 = block) (2 = beehive) (3 = loaf) (4 = boat) (5 = blinker) (6 = toad)";
cin>> board;

switch (board)
{
case 1:

a[1][2]='*';
a[1][3]='*';
a[2][2]='*';
a[2][3]='*';
break;

case 2:

a[2][3]='*';
a[2][4]='*';
a[3][2]='*';
a[3][5]='*';
a[4][3]='*';
a[4][4]='*';
break;

case 3:

a[2][3]='*';
a[2][4]='*';
a[3][2]='*';
a[3][5]='*';
a[4][3]='*';
a[4][5]='*';
a[5][4]='*';
break;

case 4:
a[2][2]='*';
a[2][3]='*';
a[3][2]='*';
a[3][4]='*';
a[4][3]='*';
break;

case 5:
a[3][2]='*';
a[3][3]='*';
a[3][4]='*';
break;

case 6:
a[3][3]='*';
a[3][4]='*';
a[3][5]='*';
a[4][2]='*';
a[4][3]='*';
a[4][4]='*';
break;
default:
cout<<"No board Avalible";
}
}
int countLife(char a[][row])
{
int life;
for(int i=0; i<column; i++)
{
for(int j=0; j<row; j++)
{
life=0;

if (a[i][j]== '*'){
life++;
}
else if (a[i+1][j]== '*'){
life++;
}
else if (a[i+1][j+1]== '*'){
life++;
}
else if (a[i+1][j-1]== '*'){
life++;
}
else if (a[i-1][j]== '*'){
life++;
}
else if (a[i-1][j-1]== '*'){
life++;
}
else if (a[i-1][j+1]== '*'){
life++;
}
else if (a[i][j-1]== '*'){
life++;
}
else if (a[i][j+1]== '*') {
life++;
}
}
}
return life;
}

void checkLife(char a[][row], char b[][row])
{
int num;
int totalLife;
int count;
int cycle;
char goAgain;

do
{
num = count%2;
switch(num)
{
case 0:
for(int i=0; i<column; i++)
{
for(int j=0; j<row; j++)
{
totalLife=countLife(a);
if (a[i][j]=='*' && totalLife<2)
{

b[i][j]='.';//dead
}
else if (a[i][j]=='*' && totalLife==2 || totalLife==3)
{

b[i][j]='*';//alive
}
else if (a[i][j]=='*' && totalLife>3)
{

b[i][j]='.';//dead
}
else if (a[i][j]=='.' && totalLife==3)
{

b[i][j]='*';//alive
}
}
}
display(b);
count++;
break;

case 1:
for(int i=0; i<column; i++)
{
for(int j=0; j<row; j++)
{
totalLife=countLife(b);
if (b[i][j]=='*' && totalLife<2)
{

a[i][j]='.';//dead
}
else if (b[i][j]=='*' && totalLife==2 || totalLife==3)
{

a[i][j]='*';//alive
}
else if (b[i][j]=='*' && totalLife>3)
{

a[i][j]='.';//dead
}
else if (b[i][j]=='.' && totalLife==3)
{

a[i][j]='*';//alive
}

}
}
display(a);
count++;
break;
}
cout << "Enter y to go again:";
cin >> goAgain;
}

while(goAgain == 'y');

}
Your countLife function is wrong. You have to count the number of alive cells in the direct neighborhood of each point. You're actually counting the number of alive cells in the whole array.
Topic archived. No new replies allowed.