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)*/

Recommended Answers

All 7 Replies

There are so many examples of Game of Life already on this site.
You could probably find help with something already created.


Although it might not matter for your OS, stdio.h should not be capitalized.
ALL the compiler says is "declared here"?

You are missing some parentheses, make sure you proof-read your code carefully. (at the end of your first function.)
Also, you didn't pass any actual parameters to your functions. orz

You are missing some parentheses, make sure you proof-read your code carefully. (at the end of your first function.)
Also, you didn't pass any actual parameters to your functions. orz

yes, i've seen it, thanks for that. what parameters and where??

There are so many examples of Game of Life already on this site.
You could probably find help with something already created.


Although it might not matter for your OS, stdio.h should not be capitalized.
ALL the compiler says is "declared here"?

I already tried, but, i haven't seen a code that works so far.. yes it only says, 'declared here'. i have no idea what it means..

You need to pass parameters to your function if you stated.
e.g. @line 175, neighbor_count();
you should put a 2-D char array into it, since Life is not a global variable.

You need to pass parameters to your function if you stated.
e.g. @line 175, neighbor_count();
you should put a 2-D char array into it, since Life is not a global variable.

since i do not know how to use parameters, i just made my code this way,

#include <STDIO.H>


int main() {
 
	char filename[20], initialgen[6];
	char Life[30][30];
	int ctr=0, ctr2=0;
	char *g;
	FILE *fp1;
	int i, j;
	int neighbors;

	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");

	
	/*neighbor_count()*/
	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 ++;
		}
	}
	
	/*next_generation();*/
	for (i=0; i<4; i++) {
		for (j=0; j<4; j++) {
			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]=' ';
			}
		}
	}
	
	printf ("The new generation is:\n");
	printf ("%s", Life[i][j]);


	return 0;
	}

it does not display anymore error, however, it still doesn't work, the new generation it prints is null, what could be wrong in this code? :(

@ line 133 you misplaced the ending bracket

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.