Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Conway's Game of Life in C. Reading from a file and memory allocation. We were also give some code and I've figured out a little

Conway's Game of Life in C. Reading from a file and memory allocation.

image text in transcribed

We were also give some code and I've figured out a little myself, but need help with the last 3 functions. Code give below. We are not supposed to alter the toString function

#include "life.h"

// Constructs and returns a string (printable) representation of the grid char *toString(int rows, int cols, char **grid) { char *str = (char *) calloc(4 * rows * cols, sizeof(char)); char *ptr = str;

for (int i = 0; i

return str; }

// Creates a grid of rows x cols and initializes the grid with data from specified file char **loadGridFromFile(char *filename, int *rows, int *cols) { char **grid = NULL; char buf[1024]; // max length of line in input file FILE *file = fopen(filename,"r");

// read line from file fgets(buf,1024,file); fclose(file);

// get number of rows from the line read *rows = atoi(strtok(buf," "));

// get number of columns from the line read *cols = atoi(strtok(NULL," "));

grid =(char**)malloc(*rows * sizeof(char*));

for(int i = 0; i

return grid; }

// Saves the grid data to the specified file void saveGridToFile(char *filename, int rows, int cols, char **grid) { FILE *file = fopen(filename,"w");

fprintf(file,"%i",rows); fprintf(file,"%i",cols); for(int i=0;i for(int j=0;j fprintf(file,"%c",grid[i][j]); } }

fclose(file); return; }

// Creates and returns a new grid that is a duplicate of the given grid char **copyGrid(int rows, int cols, char **grid) { char **dup = NULL;

// COMPLETE THIS PART OF THIS FUNCTION

return dup; }

// Mutates the given grid one generation and return a new grid char **mutateGrid(int rows, int cols, char **grid) { char** newgrid = NULL; //copyGrid(rows,cols,grid);

// COMPLETE THIS PART OF THIS FUNCTION

return newgrid; }

// Returns the number of neighbors at postion (i,j) in the grid int nbrOfNeighbors(int i, int j, int rows, int cols, char **grid) { int neighbors = 0;

// COMPLETE THIS PART OF THIS FUNCTION

return neighbors; }

Assignment Your job is to create the Game of Life in C. The initial state of the game will be read in from a text file. The file must adhere to the following convention: First number is the length of the grid Second number is the width of the grid The remaining values are only Os or ls and correspond to a dead or live cell. There are length x width number of values after the first two values. The game can be played from command line using the command below. The file name (containing the initial state of the grid) must be passed to the program as a command-line parameter. The driver program (driver.c) is already implemented for you. You can run the program with this command: $ ./driver datafile Once the program has started, the board from the data file will be read. If that is not possible the program should print a message and exit. If the file is valid, the current state of the board will be displayed followed by a menu with the following options: Iterate once - will automatically iterate one generation. Iterate multiple - will ask the user how many generations and iterate each of those, displaying each iteration. Write current state - will ask for a file name and write the current board state to a file. Errors will be reported. If the file cannot be written the program will recover and continue. Quit - will gracefully exit, freeing any memory allocated

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Investing All In One For Dummies

Authors: Eric Tyson

2nd Edition

1119873037, 978-1119873037

Students also viewed these Databases questions