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 \n", 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("\n");
for(int j = 0; j < cols; j++)
{
if(data[i][j])
printf("* ");
else
printf(" ");
}
}
printf("\n*******\n") ;
}
void save(char *filename, int **data, int rows, int cols)
{
FILE *fp;
int i, j;
fp = fopen(filename, "w");
if(fp == NULL)
{
printf("\nERROR: could not save to file %s \n", filename );
return;
}
fprintf(fp, "%d %d\n",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,"\n");
}
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("\ngeneration 0\n");
printGrid(data, rows, cols);
for(int i = 1; i <= n; i++)
{
printf("generation %d\n", i );
evolve(data, rows, cols);
printGrid(data, rows, cols);
}
save(outfilename, data, rows, cols);
free_generation(data, rows);
}
Option 2: Conwav's Game of Life Your task is to write a program that plays Conway's...
JAVA. Threading the Game of Life Write a Java program to implement the Game of Life, as defined by John Conway: Any live cell with fewer than two live neighbors dies, as if caused by under-population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by...
You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's Life is played on a matrix of cells, kind of like a chess board but theoretically extending infinitely in every direction. Each individual cell in the matrix can either be alive or dead. A live cell in the matrix is shown in our simulation by printing an asterisk (*) to the screen. A dead cell is shown by leaving that area of the matrix...
I am really struggling with how to approach this program. Conway's Game of life, whereby, 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population. 2. Any live cell with two or three live neighbours lives on to the next generation. 3. Any live cell with more than three live neighbours dies, as if by over-population. 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. Submit...
I have a question about my assignment. Could you please make program and give me some explanation of codes? I need c++ codes. Conway's Game of Life is a game invented by mathematician John Conway in 1970. The rules are as follows: Each cell lives in a cell in a square (make it 10x10) grid. A cell can either be dead or alive. Before you start the game, you need to provide an initial state. You can do this by...
I have a question about my assignment. Could you please make program and give me some explanation of codes? Conway's Game of Life is a game invented by mathematician John Conway in 1970. The rules are as follows: Each cell lives in a cell in a square (make it 10x10) grid. A cell can either be dead or alive. Before you start the game, you need to provide an initial state. You can do this by randomly setting 20 of...
Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques we have been using in class are allowed. Nothing else. The program should read the initial state of the board by reading in "alive" cells from a user input data file. Meaning your program should ask the user the name of the data file. Assume all the other cells are "dead." Make sure to use modular coding techniques. The main program should be pretty...
Write a program that consists of a 10 by 10 game board, with 10 generations. Project The Game of Life The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the neighbors of this cell. Each game state is called "generation". The game progresses from one generation to the next according to the following rules: A creature that has...
Objective: Write a program that implements the Game of Life cellular automata system invented by John Conway. 1. Create two game grids of size at least 50x50. These grid cells can be either Boolean or integer. In the following, I’ll refer to these as gridOne and gridTwo. 2. Set all cells in both grids to false. 3. Start by initializing gridOne. Allow the user to specify two different ways of initializing the grid: 1) by specifying a pattern file to...
Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...
Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...