Question: Using linked-lists, pointers, and I/O files. Implement Conways Game of Life. The primary data points will be stored in a file then each value in

Using linked-lists, pointers, and I/O files. Implement Conways Game of Life. The primary data points will be stored in a file then each value in grid is allocated dynamically, then referenced using a pointer in a linked list. Here are general steps:

a. Input the file.

b. Make the 1st line listing the number of time steps to simulate

c. Make 2nd line # of rows followed by the # of columns in the grid to be simulated then the rest of the lines in the file will have the positions of the initial cells that are "alive"

d. Make linked list of nodes allowing every node to be a single time-step (the start pointer should point to the initial grid, and each time step should follow.

Do not use arrays instead allocate memory dynamically.

Create linked list node using the stuct:

struct link_node {

int rows;

int columns;

int * gridd; //the pointer to grid that with size rows*columns

struct link_node *next_step; //the pointer to node that has grid for next time-step

};

struct link_node *makeNode(FILE *inputf, int *nsteps); // reads data from file pointers input, makes node with the data and # of time steps in nsteps
void nextnStep(struct link_node *start); // adds a struct node to end of the list pointed to by start for the next time step, use the data in the last nod of list to make the new timestep. 
void printTheList(struct link_node *start); // prints all of the grids in the list pointed by start.

Below is using arrays, need to transform this code using linked list and the functions above:

#include

#define MAXNUM 60 //defines the Maximum NUM

#define MAXTIM 60//define maximum time step size

/*function definition for returns the number of neighbors of position i,j that are alive at time step*/

int neighbors(int anArray[MAXNUM][MAXNUM][MAXTIM+1], int i, int j, int size, int step)

{

//initilaizes the total count

int totalCount=0;

if(i>=0&&j>=0&&i

{

if(j-1>=0)

{

if(anArray[i][j-1][step]==1)

totalCount++;

if(i-1>=0)

if(anArray[i-1][j-1][step]==1)

totalCount++;

if(i+1

if(anArray[i+1][j-1][step]==1)

totalCount++;

}

if(j+1

{

if(anArray[i][j+1][step]==1)

totalCount++;

if(i-1>=0)

if(anArray[i-1][j+1][step]==1)

totalCount++;

if(i+1

if(anArray[i+1][j+1][step]==1)

totalCount++;

}

if(i-1>=0)

if(anArray[i-1][j][step]==1)

totalCount++;

if(i+1

if(anArray[i+1][j][step]==1)

totalCount++;

}

return totalCount;

}

//function definition for print the grid of size sizexsize at time step

void printGrid(int myGrid[MAXNUM][MAXNUM][MAXTIM+1],int size,int step)

{

int r=0,c=0;

while(r

{

printf(" ");

c=0;

while(c

{

printf(" %d",myGrid[r][c][step]);

c++;

}

r++;

}

}

//main function

int main()

{

//print the given message

printf("Conway's game of Life ");

printf("Please enter the n for the n x n grid to simulate, max size for n is 60. :");

//initializes the n

int n=0;

//gets the maximum size for n

scanf("%d",&n);

//3d grid array

int gridArr[MAXNUM][MAXNUM][MAXTIM+1]={0};

printf(" Enter the initial x y coordinates (the board is indexed starting from 0 0) for the initial live cells, enter -1 -1 when done ");

//initializes the row r and column c is 0

int r=0,c=0;

//get the r and c upto done r = -1 and c = -1

while(r!=-1||c!=-1)

{

scanf("%d %d",&r,&c);

gridArr[r][c][0]=1;

}

//initializes the time step ts is 0

int ts=0;

//gets the time step from user

printf(" Please enter the number of time steps you wish to simulate, max number allowed is 50.");

scanf("%d",&ts);

//print the time step

printf("Num time steps %d ",ts);

printf("After %d timesteps the grid is: ",ts);

//compute the grid after the given time step

int i,j,k=1;

k=0;

while(k<=ts)

{

i=0;

while(i

{

j=0;

while(j

{

if(gridArr[i][j][k]==1)

{

//call neighbors() function

if(neighbors(gridArr,i,j,n,k)==2||neighbors(gridArr,i,j,n,k)==3)

gridArr[i][j][k+1]=1;

else

gridArr[i][j][k+1]=0;

}

else if(gridArr[i][j][k]==0)

{

if(neighbors(gridArr,i,j,n,k)==3)

gridArr[i][j][k+1]=1;

}

j++;

}

i++;

}

k++;

}

//call printGrid() function to display the grid after given ts

printGrid(gridArr,n,ts);

printf(" ");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!