Hey I'm working on a game of life program. This is the code I've got so far... I'm pretty close to finishing up but it's not displaying right... I don't see where my logic is wrong.
Code:
# include <iostream>
using namespace std;
const int N_ROW = 20;
const int N_COL = 20;
typedef char MapType[N_ROW][N_COL];
void Readint(int&, int&, MapType);
void Display(MapType);
void NextGeneration(MapType);
int CheckNeighbor(int, int, MapType);
void CopyMatrix(MapType, MapType);
void ClearMatrix(MapType);
int CheckNeighbor(int Row, int Col, MapType org)
{
int numN=0;
for(int i = Row-1; i <= Row+1; i++)
for(int j = Col-1; j <= Col+1; j++)
{
if (org[i][j]==1)
numN++;
}
return numN;
}//END OF CHECK NEIGHBOR FUNCTION
void NextGeneration(MapType orggen)
{
int Row, Col, numN;
MapType New;
for (Row = 1; Row < N_ROW-1; Row++)
for (Col = 1; Col < N_COL-1; Col++)
numN = CheckNeighbor(Row,Col,orggen);
if (numN == 3)
New[Row][Col] = 1;
else if((numN <2) || (numN >3))
New[Row][Col] = 0;
else
New[Row][Col] = orggen[Row][Col];
}//END OF NEXT GENERATION
void Display(MapType org)
{
for(int i = 0; i < N_ROW; i++)
for(int j = 0; j < N_COL; j++)
{
if(org[i][j]==1)
cout << '@';
else
cout<<' ';
}
cout << "\n";
}//END OF DISPLAY
void CopyMatrix(MapType org, MapType next)
{
for (int i=0;i<N_ROW;i++)
for (int j=0; j<N_COL;j++)
org[i][j]=next[i][j];
}//END OF COPY MATRIX
void ClearMatrix(MapType org)
{
for (int i = 0; i < N_ROW; i++)
for (int j = 0; j < N_COL; j++)
org[i][j] = 0;
}//END OF CLEAR MATRIX
void Readint(int& noc, int& nog, MapType org)
{
int rowno, colno;
cout<<"Please enter the number of initial cells: ";
cin>>noc;
for (int i = 0; i < noc; i++)
{
cout<<"The position of cell is (row, col): ";
cin>>rowno>>colno;
if((rowno < 1) || (rowno > 19))
{
cout<< "Invalid position, enter new position. ";
org[rowno][colno] = 0;
cin >> rowno>>colno;
}
if((colno < 1) || (colno > 19))
{
cout<< "Invalid position, enter new position. ";
org[rowno][colno] = 0;
cin >> rowno>>colno;
}
org[rowno][colno] = 1;
}
cout<<"How many generations do you want to generate? ";
cin >> nog;
}//END OF READ
int main()
{
int no_cells;
int no_gens;
int i=0;
MapType currmap, nextmap;
char yesno='n';
do
{
ClearMatrix(currmap);
Readint(no_cells,no_gens, currmap);
Display(currmap);
cin.get( );
for(i = 2; i <= no_gens; i++)
{
ClearMatrix(nextmap);
NextGeneration(currmap);
CopyMatrix(currmap, nextmap);
Display(nextmap);
cin.get( );
}
cout<<"Do you want to do it again? (Y/N)";
cin>> yesno;
}
while(yesno=='Y' || yesno =='y');
cout<<"Good Bye"<<endl;
return 0;
}//END OF MAIN
Actual Assignment.
The game of Life is intended to model life in a society of organisms. consider a rectangular array of cells, each of which may contain an organism. If the array is considered to extend indefinitely in both directions, then each cell has eight neighbors, the eight cells surrounding it. Births and deaths occur according to the following rules:
1. An organism is born in any empty cell having exactly three neighbors.
2. An organism dies from isolation if it has fewer than two neighbors.
3. An organism dies from overcrowding if it has more than three neighbors.
4. All other organisms survive to the next generation.
Write a program to play the game of Life and investigate the patterns produced by various initial configurations. Some configurations die off rather rapidly; others repeat after a certain number of generations; others change shape and size and may move across the array.
NOTE:
1. For representing each organism, use "@" characters.
2. For an array of cells, use 30x30 two-dimensional array.
3. Each generation should be displayed on the screen and should be paused.
(You can use cin.get( ) library function to pause running your program.)
4. For all programs for this class, you will be expected to hand in a diskette (3.5") with the appropriate files (a source file and an executable file).
5. Use a folder to hand in your assignment.
6. The source file should be called life.cpp
7. All assignments are expected to be INDIVIDUAL work. All work handed in must be original. Duplicate or very similar programs receive zero points.
Input
A set of initial cells will be given using the interactive way using a keyboard.
The followings are the prompt for the input:
Please Enter the number of initial cells: 4
The position of cell is 10 10
The position of cell is 10 11
The position of cell is 10 12
The position of cell is 11 11
How many generations do you want to display? 3
Output
The program then display each generation of organisms at a time.
The 1-generation
@
@@@
Press any key to continue!!
(This picture should be displayed on the new screen.)
The 2-generation
@
@@@
@@@
Press any key to continue!!
The 3-generation
@@@
@ @
@
Do you want to do it again?(Yes/No) N