Question: Your mission for this assignment is to first complete and then modify a starter program that plays mathematician John Conway's ``Game of Life'', described below.

Your mission for this assignment is to first complete and then modify a starter program that plays mathematician John Conway's ``Game of Life'', described below. (You can also find more information on the Web.

Your mission for this assignment is to first complete and then modify

Code talked about:

#include

#include

#include

#include

/*

* builds 2D array in form of an array of pointers into one big 1D array.

* return value is array of pointers, or NULL if there was a problem

* (in which case the function prints a suitable error message).

*/

bool **build2D(long size);

/*

* frees 2D array, built as in build2D().

*/

void free2D(long size, bool **array2D);

/*

* reads initial board configuration from file.

* builds 2D array in form of an array of pointers into one big 1D array.

* parameters:

* infile is an already-opened file.

* (out) *size_p is board size

* return value is array of pointers, or NULL if there was a problem

* (in which case the function prints a suitable error message).

*/

bool **read_board(FILE* infile, long *size_p);

/*

* generates new board configuration from old.

*/

void update_board(long size, bool **old_board, bool **new_board);

/*

* * prints board.

* */

void print_board(long size, bool **board);

/*

* main program

*/

int main(int argc, char* argv[]) {

/* process command-line arguments */

if (argc != 3) {

fprintf(stderr, "parameters; infile num_steps ");

return EXIT_FAILURE;

}

FILE* infile = fopen(argv[1], "r");

if (infile == NULL) {

fprintf(stderr, "unable to open input file %s ", argv[1]);

return EXIT_FAILURE;

}

char* end_ptr_for_strtol;

long steps = strtol(argv[2], &end_ptr_for_strtol, 10);

if ((*end_ptr_for_strtol != '\0') || (steps

fprintf(stderr, "invalid num_steps ");

return EXIT_FAILURE;

}

/* create board from input file */

long size;

bool **board = read_board(infile, &size);

if (board == NULL) {

fclose(infile);

return EXIT_FAILURE;

}

fclose(infile);

/* create second board to use for updates */

bool **new_board = build2D(size);

if (new_board == NULL) {

free2D(size, board);

return EXIT_FAILURE;

}

/* print initial configuration */

printf("Initial board: ");

print_board(size, board);

putchar(' ');

/* loop to update board and print */

for (long step = 1; step

/* update (results in new_board) */

update_board(size, board, new_board);

printf("Board after step %ld: ", step);

print_board(size, new_board);

putchar(' ');

/* swap old and new boards by exchanging pointers(!) */

bool **temp = board;

board = new_board;

new_board = temp;

}

/* tidy up and return */

free2D(size, board);

free2D(size, new_board);

return EXIT_SUCCESS;

}

/*

* functions (described above)

*/

bool **build2D(long size) {

bool *data = malloc(size * size * sizeof(data[0]));

if (data == NULL) {

fprintf(stderr,

"unable to allocate space for board of size %ld ",

size);

return NULL;

}

bool **array2D = malloc(size * sizeof(array2D[0]));

if (array2D == NULL) {

fprintf(stderr,

"unable to allocate space for board of size %ld ",

size);

return NULL;

}

for (long r = 0; r

array2D[r] = &data[r*size];

}

return array2D;

}

void free2D(long size, bool **array2D) {

bool *data = array2D[0];

free(data);

free(array2D);

}

bool **read_board(FILE* infile, long *size_p) {

/* read size from input file */

long size;

if ((fscanf(infile, "%ld", &size) != 1) || (size

fprintf(stderr, "invalid size in input file ");

return NULL;

}

*size_p = size;

/* build 2D array */

bool **board = build2D(size);

if (board == NULL) {

return NULL;

}

/* fill with data from file */

int inchar;

for (long i = 0; i

for (long j = 0; j

/* get next nonspace character from input file */

while (isspace(inchar = fgetc(infile))) { }

if (inchar == '1') {

board[i][j] = true;

}

else if (inchar == '.') {

board[i][j] = false;

}

else {

fprintf(stderr,

"unable to read values for board ");

free2D(size, board);

return NULL;

}

}

}

/* check for anything else in input file */

while (isspace(inchar = fgetc(infile))) { }

if (!feof(infile)) {

fprintf(stderr, "unable to read values for board ");

free2D(size, board);

return NULL;

}

return board;

}

int isValid(long i, long j, long SIZE) {

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

return 1;

else

return 0;

}

int findActiveNeighbor(bool **cells, long i, long j, long SIZE) {

int activeNeighbors = 0;

if (isValid(i - 1, j - 1, SIZE) && cells[i - 1][j - 1])

activeNeighbors++;

if (isValid(i - 1, j, SIZE) && cells[i - 1][j])

activeNeighbors++;

if (isValid(i - 1, j + 1, SIZE) && cells[i - 1][j + 1])

activeNeighbors++;

if (isValid(i, j - 1, SIZE) && cells[i][j - 1])

activeNeighbors++;

if (isValid(i, j + 1, SIZE) && cells[i][j + 1])

activeNeighbors++;

if (isValid(i + 1, j - 1, SIZE) && cells[i + 1][j - 1])

activeNeighbors++;

return activeNeighbors;

}

void update_board(long size, bool **old_board, bool **new_board) { for (long i = 0; i

for (long j = 0; j

int n = findActiveNeighbor(old_board, i, j, size); if (old_board[i][j]) {

if (n == 0 || n == 1) {

new_board[i][j] = false;

} else if (n >= 4) {

new_board[i][j] = false;

} else {

new_board[i][j] = old_board[i][j];

}

} else {

if (n == 3) {

new_board[i][j] = true;

} else {

new_board[i][j] = old_board[i][j];

}

}

}

}

}

void print_board(long size, bool **board) {

for (long i = 0; i

for (long j = 0; j

if (board[i][j])

printf("1 ");

else

printf(". ");

}

putchar(' ');

}

}

?The second program adapts the first one to experiment with generating the initial board randomly rather than reading it in from a file and observing how the game evolves with different board sizes and parameters for generating random" data. Your program should get all its input from command-line arguments: - Size of board (a long)-, Seed for random number sequence (a long). Fraction of cells that should be"live" initially (a double) (not really a great name, explained better later) * Number of steps (a long). Optionally, the word print The program should then work as follows: . Generate an initial configuration by first calling srand ( ) and then calling rand ( ) 0nce per cell, setting the cell to true based on whether what rand() returns, divided by RAND MAX S less than the specified fraction For each step, update the board (no change from first program), but then rather than always printing the new board, count and print the number of cells that are "live", and print the board if the print" option was specified. (My idea here is that such a program could be a prototype for something that would allow you to try out more-sophisticated ways of generating" random" configurations and observing how they evolve.) ?The second program adapts the first one to experiment with generating the initial board randomly rather than reading it in from a file and observing how the game evolves with different board sizes and parameters for generating random" data. Your program should get all its input from command-line arguments: - Size of board (a long)-, Seed for random number sequence (a long). Fraction of cells that should be"live" initially (a double) (not really a great name, explained better later) * Number of steps (a long). Optionally, the word print The program should then work as follows: . Generate an initial configuration by first calling srand ( ) and then calling rand ( ) 0nce per cell, setting the cell to true based on whether what rand() returns, divided by RAND MAX S less than the specified fraction For each step, update the board (no change from first program), but then rather than always printing the new board, count and print the number of cells that are "live", and print the board if the print" option was specified. (My idea here is that such a program could be a prototype for something that would allow you to try out more-sophisticated ways of generating" random" configurations and observing how they evolve.)

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!