0

Hi!
My homework is to make a c program for game of life. I have made my code already, the problem is when i'm running the code the compiler doesn't display any error, but my code still doesn't work well. I have to print in the screen an initial generation coming from a textfile, from that initial generation, I have to calculate the next generation using the rules of the game. After calculating the next generation I should save it from another textfile.
Rules of the game:
1. An organism will survive if it has exactly 2 or 3 neighbors.
2. An organism will die if it has less than 2 or more than 3 neighbors.
3. An organism will be born if it has exactly 3 neighbors.

I think I applied the rules of the game well, however I have no idea why my code still can not calculate the next generation.

I hope you guys could help me.. Thanks a lot!! by the way my board is a 25x25 matrix of the character 'X' and ' '. 'X' corresponds to a living organism.

#include <stdio.h>

char initial [50][50];
char generation1 [50][50];

int counting_neighbors (int i, int j)	{

	int neighborscount;
	neighborscount=0;
	
				if (initial [i][j+1]=='X')
					neighborscount ++;
				if (initial [i][j-1]=='X')
					neighborscount ++;
				if (initial [i-1][j]=='X')
					neighborscount ++;
				if (initial [i-1][j+1]=='X')
					neighborscount ++;
				if (initial [i-1][j-1]=='X')
					neighborscount ++;
				if (initial [i+1][j]=='X')
					neighborscount ++;
				if (initial [i+1][j+1]=='X')
					neighborscount ++;
				if (initial [i+1][j-1]=='X')
					neighborscount ++;
				
				
	return neighborscount;
}


int main()	{

  int i, j;
  int neighbors;
  char filename[10], initialgen[26];
  char *g;
  FILE *fp1;

	printf ("Enter filename of initial generation: ");
	scanf ("%s", filename);
	
	fp1=fopen(filename, "r");

	i=0;
	j=0;
	do {
	   g = fgets (initialgen, 26, fp1);
		for(j=0;j<26;j++){
			initial [i][j]=g[j];
		}
		i++;
	   if (g!=NULL)
	      printf ("%s", initialgen);
	   }
	while (g != NULL);
	fclose (fp1);
	printf ("\n");

neighbors=0;

      for(i=0;i<25;i++) {
	  for(j=0;j<26;j++)
          {
			neighbors=counting_neighbors(i,j);

			if (initial [i][j]=='X') {
				if (neighbors==3)
					generation1 [i][j]='X';
				else
					generation1 [i][j]=' ';
				if (neighbors==2)
					generation1 [i][j]='X';
				else
					generation1 [i][j]=' ';
			}
			if(initial [i][j]==' ') {
				if (neighbors==3)
					generation1 [i][j]='X';
				else
					generation1 [i][j]=' ';
			}
		}
	}
      
	  fp1=fopen ("gen1.txt", "w");
	  if (fp1!=NULL) {
		for(i=0;i<25;i++) {
			for(j=0;j<26;j++) {
				fprintf (fp1, "%c", generation1 [i][j]);
			}
			fprintf (fp1, "\n");
		}
	}
	fclose(fp1);
	
		printf ("Generation1:");
	  
	for(i=0;i<25;i++) {
	  for(j=0;j<26;j++) {
			printf ("%c", generation1 [i][j]);
		} printf ("\n");
	}

      return 0;
  }
1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by vired
0
#include <stdio.h>

char initial [50][50];
char generation1 [50][50];

int counting_neighbors (int i, int j)	{

	int neighborscount;
	neighborscount=0;
	
	if (initial [i][j+1]=='X')
	        neighborscount ++;
	if (initial [i][j-1]=='X')
		neighborscount ++;
	if (initial [i-1][j]=='X')
		neighborscount ++;
	if (initial [i-1][j+1]=='X')
		neighborscount ++;
	if (initial [i-1][j-1]=='X')
		neighborscount ++;
	if (initial [i+1][j]=='X')
		neighborscount ++;
	if (initial [i+1][j+1]=='X')
		neighborscount ++;
	if (initial [i+1][j-1]=='X')
		neighborscount ++;
				
				
	return neighborscount;
}


int main()	{

  int i, j;
  int neighbors;
  char filename[10], initialgen[26];
  char *g;
  FILE *fp1;

	printf ("Enter filename of initial generation: ");
	scanf ("%s", filename);
	
	fp1=fopen(filename, "r");

	i=0;
	j=0;
	do {
	   g = fgets (initialgen, 26, fp1);
		for(j=0;j<26;j++){
			initial [i][j]=g[j];
		}
		i++;
	   if (g!=NULL)
	      printf ("%s", initialgen);
	   }
	while (g != NULL);
	fclose (fp1);
	printf ("\n");

  neighbors=0;

    for(i=0;i<25;i++) {
      for(j=0;j<26;j++){
	neighbors=counting_neighbors(i,j);

	if (initial [i][j]=='X') {
		if (neighbors==3)
			generation1 [i][j]='X';
		else
			generation1 [i][j]=' ';
		if (neighbors==2)
			generation1 [i][j]='X';
		else
			generation1 [i][j]=' ';
			}
	if(initial [i][j]==' ') {
		if (neighbors==3)
			generation1 [i][j]='X';
		else
		        generation1 [i][j]=' ';
		}
      }
}
fp1=fopen ("gen1.txt", "w");
    if (fp1!=NULL) {
	for(i=0;i<25;i++) {
	    for(j=0;j<26;j++){
		fprintf (fp1, "%c", generation1 [i][j]);
		}
		fprintf (fp1, "\n");
	}
   }
fclose(fp1);
	
printf ("Generation1:");
	  
    for(i=0;i<25;i++) {
      for(j=0;j<26;j++) {
	printf ("%c", generation1 [i][j]);
	} printf ("\n");
     }

      return 0;
  }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.