Option 2: Conwav\'s Game of Life Your task is to write a program that plays Conw
ID: 3734175 • Letter: O
Question
Option 2: Conwav's Game of Life Your task is to write a program that plays Conway's Game of Life (for details and ex amples, see https://en.vikipedia.org/wiki/Convay's.Game of Life). The basic gam ts played on each generation, each cell changes according to the following instructions: nal grid of square cells, each of which is either alive or dead. With 1. Any dead cell with exactly three live neighbors (out of its eight nearest neighbors) comes to life [reproduction]. 2. Any live cell with fewer than two live neighbors dies (underpopulation). 3. Any live cell with more than three live neighbors dies (overpopulation). 4. Any live cell with two or three neighbors continues to live. The output of the game is therefore entirely determined by its initial state, which you may represent in terms of a matrix whose entries are all 0/1. The details of the program are up to you (whatever makes for an interesting result), but consider the following choices: 1. You will probably want to keep track of two matrices: one (e.g. thisGen) with the information for the current gencration, and one (c.g. nextGen) where you define in- formation for the next generation. Then swap them with thisCen nextGen; and repeat. 2. The grid may be infinite (where your matrix has to get larger over time), finite with a boundary, or toroidal (if an object moves off the right side of the screen, it reappears on the left side). 3. Include some functions that allow the user to progress g gencrations with frame rate r, then stop and await further instructions. (This as an alternative to running the program a fixed number of generations before quitting, or to running indefinitely). 4. Include some functions that allow the user to manually change the living/dead state of some of the cells. 5. Allow the user to upload an arbitrary matrix pattern for the initial state. 6. Allow the user to save the current state so that they can retrieve it at a later time. 7. Allow the user to save some portion of the animation as a GIFExplanation / Answer
Solution:
code:
#include <stdio.h>
#include <stdlib.h>
//function to load data from a file
int** load_generation(char *filename, int *rows, int *cols)
{
FILE *fp = fopen(filename, "r");
int **data;
int i, j;
int r, c;
if(fp == NULL)
{
printf("ERROR: could not load data from file %s ", filename );
exit(1);
}
fscanf(fp, "%d %d", &r, &c);
*rows = r;
*cols = c;
data = (int **)malloc(sizeof(int*) * r);
for(i = 0; i < r; i++)
{
data[i] = (int *)malloc(sizeof(int) * r);
for(j = 0; j < c; j++)
{
fscanf(fp, "%d", &data[i][j]);
}
}
fclose(fp);
return data;
}
//function to create an empty generation with rows1, cols1 dimension
int** creaate_empty_generation(int rows, int cols)
{
int **data;
int i, j;
//allocate memory
data = (int ** )malloc(sizeof(int*) * rows);
for(i = 0; i < rows; i++)
data[i] = (int *) malloc(sizeof(int) * cols);
//initialize
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
data[i][j] = 0;
}
return data;
}
void free_generation(int **data, int rows)
{
int i;
for(i = 0; i < rows; i++)
free(data[i]);
free(data);
}
//function to get number of live neighbours for a cell at r,c
int count_neighbours(int **data, int r, int c, int rows, int cols)
{
int total = 0;
int row, col;
for(row = r -1; row <= r+1; row++)
{
for(col = c -1 ; col <= c+1; col++)
{
if(row ==r && col == c)
continue;
if(row >=0 && row < rows && col >= 0 && col < cols && data[row][col] == 1)
total++;
}
}
return total;
}
//function to make this generation evolve
void evolve(int **data, int rows, int cols)
{
int **newgen = creaate_empty_generation(rows, cols);
int neigh, i, j;
for( i = 0; i < rows; i++)
{
for( j = 0; j < cols; j++)
{
neigh = count_neighbours(data, i, j, rows, cols);
if(data[i][j]) //living cell
{
if(neigh == 2 || neigh == 3) //only if 2 or 3 neighbours, will live again
newgen[i][j] = 1;
else
newgen[i][j] = 0;
}
else //dead cell
{
if(neigh == 3)//dead cell comes to life if exactly 3 living neighbours
newgen[i][j] = 1;
else
newgen[i][j] = 0;
}
}
}
//copy back the values
for( i = 0; i < rows; i ++)
for( j = 0; j < cols; j++)
data[i][j] = newgen[i][j];
free_generation(newgen, rows);
}
void printGrid(int **data, int rows, int cols)
{
for(int i = 0; i < rows; i++)
{
printf(" ");
for(int j = 0; j < cols; j++)
{
if(data[i][j])
printf("* ");
else
printf(" ");
}
}
printf(" ******* ") ;
}
void save(char *filename, int **data, int rows, int cols)
{
FILE *fp;
int i, j;
fp = fopen(filename, "w");
if(fp == NULL)
{
printf(" ERROR: could not save to file %s ", filename );
return;
}
fprintf(fp, "%d %d ",rows, cols);
for(i = 0; i < rows; i++)
{
for( j = 0; j < cols; j++)
{
if(data[i][j])
fprintf(fp, "1 ");
else
fprintf(fp, "* ");
}
fprintf(fp," ");
}
fclose(fp);
}
int main()
{
char infilename[100], outfilename[100];
int n = 4; // evolve to 4 more generations
int rows = 0, cols = 0;
int **data;
printf("Enter input filename containing game of life data: ");
scanf("%s", infilename);
printf("Enter output filename: ") ;
scanf("%s",outfilename);
data = load_generation(infilename, &rows, &cols);
printf(" generation 0 ");
printGrid(data, rows, cols);
for(int i = 1; i <= n; i++)
{
printf("generation %d ", i );
evolve(data, rows, cols);
printGrid(data, rows, cols);
}
save(outfilename, data, rows, cols);
free_generation(data, rows);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.