Game of life

Good day! Can you help me with my code regarding the game, GAME OF LIFE. Please try to edit my code cause it keeps on lagging when I run it. Your help would be so much appreciated. Thank you!:)



/*Game of Life*/

#include<stdio.h>
#include<stdlib.h>

#define BSIZE 25

char **readfile(char *argv[],char **board) {
int i,j;
char temp;
FILE *fp;

fp=fopen(argv[1],"rt");
if (fp!=NULL) {
board=(char**)malloc(sizeof(char*)*BSIZE);
for (i=0; i<BSIZE; i++)
board[i]=(char*)malloc(sizeof(char)*(BSIZE+1));

for (i=0; i<BSIZE; i++)
board[i]=fgets(board[i],1000,fp);

fclose(fp);
}
else
board=NULL;

return board;
}

int writefile(char *argv[],char **board) {
int i,j;
FILE *fp;

fp=fopen(argv[2],"wt");
if (fp!=NULL) {
for (i=0; i<BSIZE; i++) {
for (j=0; j<BSIZE; j++)
fprintf(fp,"%c",board[i][j]);

fprintf(fp,"\n");
}
fclose(fp);
}
else
return 1;

return 0;
}


char **nextgen(char **board) {
int i,j, k, y; /*y is #of neighbors storage*/
char **temp;
int neighbor[BSIZE][BSIZE]; /*neighbors counting, storage per box*/
temp=(char**)malloc(sizeof(char*)*BSIZE);
for (i=0; i<BSIZE; i++)
temp[i]=(char*)malloc(sizeof(char)*(BSIZE+1));

/*
Your algorithm goes here. You don't need to modify any other part of the program.

You can use the array temp[][] to store the next generation.
The code below copies the contents of temp[][] back to the array board[][]
before returning.
*/


/*neighbor counting*/
for(k=0; k<=25; k++){
y=0;
if(board[1][k]== 'x') y++;
if(board[BSIZE][k]== 'x') y++;
if(board[k][1]== 'x') y++;
if(board[k][BSIZE]== 'x') y++;
neighbor[i][j]=y;
}
for(i=0;i<BSIZE;i++)
for(j=0;j<BSIZE;j++){
y=0;
if (board[i-1][j-1]=='x')
y++;
if (board[i][j-1]=='x')
y++;
if (board[i+1][j-1]=='x')
y++;
if (board[i-1][j]=='x')
y++;
if (board[i+1][j]=='x')
y++;
if (board[i-1][j+1]=='x')
y++;
if (board[i][j+1]=='x')
y++;
if (board[i+1][j+1]=='x')
y++;
neighbor[i][j]=y;
}


/*forming of 2nd generation*/
temp[i][j] = board[i][j];
for(i=0;i<BSIZE;i++)
for(j=0;j<BSIZE;j++){
if (board[i][j]=='x'){
if (neighbor[i][j]>=4 || neighbor[i][j]<=1)
temp[i][j]=' ';
if (neighbor[i][j]==2 || neighbor[i][j]==3)
temp[i][j]='x';
}
if (board[i][j]==' '){
if (neighbor[i][j]==3)
temp[i][j]='x';
else
temp[i][j]=' ';
}

}


for (i=0; i<BSIZE; i++) {
for (j=0; j<BSIZE; j++)
board[i][j]=temp[i][j];
free(temp[i]);
}
free(temp);

return board;
}

/*-----------------------------------------------------------------*/

int main (int argc,char *argv[]) {
int i,j;
char **board;

if (argc>2) {
board=readfile(argv,board);
if (board!=NULL) {

board=nextgen(board);

if (writefile(argv,board))
printf("\nError writing %s\n\n",argv[2]);

for (i=0; i<BSIZE; i++)
free(board[i]);
free(board);
}
else
printf("\nError opening %s\n\n",argv[1]);
}
else
printf("\nUsage: %s <inputfile> <outputfile>\n\n",argv[0]);
system ("PAUSE");
return 0;
}
Last edited on
 
Last edited on
@gotit

I'm a newbie to reading C++ code so I can only understand bits your code completely. Can you please tell me with it's suppose to do? (I found the title of your thread interesting :))
Last edited on
 
Last edited on
Thanks for helping.I'm still figuring out my mistakes though. Thanks:)
Last edited on
I have edited the first one with this but still the 2nd gen produced is wrong.:( help please!

for(i=1;i<BSIZE-1;i++)
for(j=1;j<BSIZE-1;j++){
y=0;
if (board[i-1][j-1]=='x')
y++;
if (board[i][j-1]=='x')
y++;
if (board[i+1][j-1]=='x')
y++;
if (board[i-1][j]=='x')
y++;
if (board[i+1][j]=='x')
y++;
if (board[i-1][j+1]=='x')
y++;
if (board[i][j+1]=='x')
y++;
if (board[i+1][j+1]=='x')
y++;

}


/*forming of 2nd generation*/


for (i=0; i<BSIZE; i++){
for (j=0; j<BSIZE; j++){
if (board[i][j] == ' ' && y == 3 ){
temp[i][j] = 'x';
}
else if (board[i][j] == 'x' && y == 2){
temp[i][j] = 'x';
}
else if (board[i][j] == 'x' && y == 3){
temp[i][j] = 'x';
}
else {
temp[i][j] = ' ';
}
}
}
 
Last edited on
I really don't get how to go with the edges and we have a deadline for this and I'm really running out of time. but anyway thanks for the help:))
  
Last edited on
thank you!:)
Last edited on
Topic archived. No new replies allowed.