hello all!
i need help regarding my C programming homework of the Conway's game of life.
i have to make a C program that will read a textfile containing a 25x25 matrix of 'X' or 'blank space' and will print its content. This will serve as the initial generation and will be the basis of the calculations for the next 2 generations. Then I have to save the calculated next generations into another textfile using the same program.
Rules of the Game
The next generation of organisms is determined according to the follow-
ing criteria:
• Birth – An organism will be born in each empty location that has
exactly three neighbors.
• Death – An organism with four or more organisms as neighbors will
die from overcrowding. And organism with fewer than two neighbors
will die from loneliness.
• Survival – An organism with two or three neighbors will survive to the
next generation.
this is the code i have done so far and it isn't working yet, i posted the code together with the error displayed by the compiler..
please please do help me. :(
#include <STDIO.H>
#define ORGANISM 1
#define NO_ORGANISM 0
void neighbor_count(char Life[30][30]) { /* the compiler displays a note here saying: declared here, i don't know what it means*/
int i, j;
int neighbors;
neighbors=0;
for (i=0; i<4; i++) {
for (j=0; j<4; j++) {
if (i==0 && j==0) {
if (Life [i+1][j]=='X')
neighbors ++;
if (Life [i][j+1]=='X')
neighbors ++;
if (Life [i+1][j+1]=='X')
neighbors ++;
}
if (i==3 && j==0) {
if (Life [i-1][j]=='X')
neighbors ++;
if (Life [i-1][j+1]=='X')
neighbors ++;
if (Life [i][j+1]=='X')
neighbors ++;
}
if (i==0 && j==3) {
if (Life [i][j-1]=='X')
neighbors ++;
if (Life [i+1][j-1]=='X')
neighbors ++;
if (Life [i+1][j]=='X')
neighbors ++;
}
if (i==3 && j==3) {
if (Life [i][j-1]=='X')
neighbors ++;
if (Life [i-1][j-1]=='X')
neighbors ++;
if (Life [i-1][j]=='X')
neighbors ++;
}
if (j==0 && i!=0 && i!=3) {
if (Life [i-1][j]=='X')
neighbors ++;
if (Life [i-1][j+1]=='X')
neighbors ++;
if (Life [i][j+1]=='X')
neighbors ++;
if (Life [i+1][j]=='X')
neighbors ++;
if (Life [i+1][j+1]=='X')
neighbors ++;
}
if (j==3 && i!=0 && i!=3) {
if (Life [i-1][j]=='X')
neighbors ++;
if (Life [i-1][j-1]=='X')
neighbors ++;
if (Life [i][j-1]=='X')
neighbors ++;
if (Life [i+1][j-1]=='X')
neighbors ++;
if (Life [i+1][j+1]=='X')
neighbors ++;
}
if (i==0 && j!=0 && j!=3) {
if (Life [i][j-1]=='X')
neighbors ++;
if (Life [i][j+1]=='X')
neighbors ++;
if (Life [i+1][j-1]=='X')
neighbors ++;
if (Life [i+1][j]=='X')
neighbors ++;
if (Life [i+1][j+1]=='X')
neighbors ++;
}
if (i==3 && j!=0 && j!=3) {
if (Life [i][j-1]=='X')
neighbors ++;
if (Life [i][j+1]=='X')
neighbors ++;
if (Life [i-1][j-1]=='X')
neighbors ++;
if (Life [i-1][j]=='X')
neighbors ++;
if (Life [i-1][j+1]=='X')
neighbors ++;
}
if (i!=0 && i!=3 && j!=0 && j!=3) {
if (Life [i][j+1]=='X')
neighbors ++;
if (Life [i][j-1]=='X')
neighbors ++;
if (Life [i-1][j]=='X')
neighbors ++;
if (Life [i-1][j+1]=='X')
neighbors ++;
if (Life [i-1][j-1]=='X')
neighbors ++;
if (Life [i+1][j]=='X')
neighbors ++;
if (Life [i+1][j+1]=='X')
neighbors ++;
if (Life [i+1][j-1]=='X')
neighbors ++;
}
void next_generation(char Life [30][30]) { /*note: declared here*/
int i,j;
int neighbors;
for (i=0; i<4; i++) {
for (j=0; j<4; j++) {
neighbor_count();/* the compiler says error: too few arguments to function 'neighbor_count'*/
if (Life [i][j]==' ') {
if (neighbors==3)
Life [i][j]='X';
else
Life [i][j]=' ';
}
if (Life [i][j]=='X') {
if (neighbors==2 && neighbors==3)
Life [i][j]='X';
else
Life [i][j]=' ';
}
}
}
}
int main() { /*there's a warning here saying 'main' is normally a non-static function [-Wmain]*/
char filename[20], initialgen[6];
char Life[30][30];
int ctr=0, ctr2=0;
char *g;
FILE *fp1;
int i, j;
printf ("Enter filename of initial generation: ");
scanf ("%s", filename);
fp1 = fopen(filename, "r");
do {
g = fgets (initialgen, 6, fp1);
for(ctr2=0;ctr2<6;ctr2++){
Life[ctr][ctr2]=g[ctr2];
}
ctr++;
if (g!=NULL)
printf ("%s", initialgen);
}
while (g != NULL);
fclose (fp1);
printf ("\n");
for (i=0; i<4; i++) {
for (j=0; j<4; j++) {
if (Life[i][j]=='X')
Life[i][j]=ORGANISM;
else
Life[i][j]=NO_ORGANISM;
}
}
neighbor_count();/*error: too few arguments to function 'neighbor_count'*/
next_generation();/*error: too few arguments to function 'next_generation'*/
printf ("The new generation is:\n");
printf ("%d", Life[i][j]);
return 0;
} /*error: expected declaration or statement at end of input (repeated 3 times)*/