How to make a c program for Conway's game of life?

icy

Recruit
Hi everyone!

Kindly help me with my C programming homework please..
<


My teacher asked us to have a textfile of a 25x25 array of 'X' and 'blank space', this textfile will serve as the initial generation of the game hence, the basis of the next 2 generations. We should make a C program that will read the textfile, print its content and calculate the next 2 generations then save it to another textfile.

here's the rules for the game of life:

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.

The code i made isn't still working and i don't know how to fix it. please help me to fix it.
<


#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 [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 [j+1]=='X')

neighbors ++;

}

if (i==0 && j==3) {

if (Life [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 [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 [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 [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 [j-1]=='X')

neighbors ++;

if (Life [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 [j-1]=='X')

neighbors ++;

if (Life [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 [j+1]=='X')

neighbors ++;

if (Life [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 [j]==' ') {

if (neighbors==3)

Life [j]='X';

else

Life [j]=' ';

}

if (Life [j]=='X') {

if (neighbors==2 && neighbors==3)

Life [j]='X';

else

Life [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[j]=='X')

Life[j]=ORGANISM;

else

Life[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[j]);

return 0;

} /*error: expected declaration or statement at end of input (repeated 3 times)*/

i posted the code together with the errors said by the compiler..
 
1. Int main -- use void main and remove "return 0;", if u wanna remove that warning.

2. Both your functions "Neighbor_count" and next_generation" are declared and defined at the same time, hence the note.

3. neighbor_count(char array[30][30]) is declared and defined with a two dimensional character array as a parameter at the beginning (the ([font=arial, sans-serif]char Life[30][30]) part) [/font], but you are calling it with out any argument. Hence the error.

4. Same for next_generation(); error.

Hope that made any sense
<
 
Back
Top