What is the problem with my code? Please help...


RealBiggie

New Member
Recruit
Mar 19, 2012
2
0
0
[font=helvetica, arial, verdana,]Hello![/font]

[font=helvetica, arial, verdana,]I have a homework and that is to make a c program for the Game Of Life. I"m already done with my code. Everytime i run the code, the compiler doesn't display any error, however 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.[/font]

[font=helvetica, arial, verdana,]Rules of the game: [/font]

[font=helvetica, arial, verdana,]1. An alive cell that has exactly 2 or 3 neighbors will survive.[/font]

[font=helvetica, arial, verdana,]2. An alive cell that has less than 2 or more than 3 neighbors will die.[/font]

[font=helvetica, arial, verdana,]3. An alive cell will be born if it has exactly 3 neighbors.[/font]

[font=helvetica, arial, verdana,]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.[/font]

[font=helvetica, arial, verdana,]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 cell.[/font]

here is my code:

#include <stdio.h>

char initial [50][50];

char generation1 [50][50];

int counting_neighbors (int i, int j) {

int neighborscount;

neighborscount=0;

if (initial [j+1]=='X')

neighborscount ++;

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

if (neighbors==3)

generation1 [j]='X';

else

generation1 [j]=' ';

if (neighbors==2)

generation1 [j]='X';

else

generation1 [j]=' ';

}

if(initial [j]==' ') {

if (neighbors==3)

generation1 [j]='X';

else

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

}

fprintf (fp1, "\n");

}

}

fclose(fp1);

printf ("Generation1:");

for(i=0;i<25;i++) {

for(j=0;j<26;j++) {

printf ("%c", generation1 [j]);

} printf ("\n");

}

return 0;

}