Implementing Conway’s Game Of Life in the language C Your program will take as i
ID: 3681270 • Letter: I
Question
Implementing Conway’s Game Of Life in the language C
Your program will take as input from standard in: (1) The name of the input pgm to act as the seed (2) The number of additional frames to compute. You will write the sequence of frames to standard out.
1 PGM Files
Format:
P2 MN W #1 #2 #3 ...
#MxN -1 #MxN
At the top of the file is the special keyword ”P2”, read it in as a string and make sure it matches. M and N are the integer dimensions of the image. M is the number of columns, N is the number of rows. W is the integer for the value of white on a grey scale. 0 is black. If you want a black and white image then W should be 1. Every M numbers after W populate a complete row.
2 Internal Representation Of The Map
After reading in the dimensions of the board, You will allocate a 2D array (M+2)x(N+2). The outer most rows and columns (index 0 and M+1/N+1)
are fixed at 0. You will read the board in from the PGM file into the indeces [1..M][1..N] in memory.
3 Rules Of Conway’s Game Of Life
Rules
Any live cell with fewer than two live neighbours dies, as if caused by under-population.
Any live cell with two or three live neighbours lives on to the next gener- ation.
Any live cell with more than three live neighbours dies, as if by over- population.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
For a given cell, its neighborhood is every cell that is +/- 1 in all directions.
4 Computing The Next Frame
Starting from the seed, you will compute the indicated number of additional frames. Each frame’s border is fixed at 0, indicating it is permanently empty. To create a new frame you will allocate the space for the new frame, initializing the border to zero, and for every internal cell in the new frame you will look at the neighborhood in the old frame to determine if the cell should be occupied or empty.
The simplest organization is to have a function which takes as input a frame and returns as output a frame.
5 Output To Standard Out
First you will output the seed frame:
Seed: M N
OXXOOOOOOXOX
OXOOOXXOOXOX
OXOXOOOXXXOX
...
You will print ”Frame:” followed by the dimensions.
The following N lines will contain the contents for the rows where ”O” (capital O) is 0 or empty and ”X” (capital X) is 1 or occupied, each row’s content separated by a single space.
After computing each from you should output the following information:
Frame: #
OXXOOOOOOXOX
OXOOOXXOOXOX
OXOXOOOXXXOX
...
You will print ”Frame:” followed by the number of the frames (starting from 1).
The following N lines will contain the contents for the rows where ”O” (capital O) is 0 or empty and ”X” (capital X) is 1 or occupied, each row’s content separated by a single space.
Example:
Seed: 3 3
OOOOO
OOXOO
OOXOO
OOXOO
OOOOO
Frame: 1
OOOOO
OOOOO
OXXXO
OOOOO
OOOOO
Frame: 2
OOOOO
OOXOO
OOXOO
OOXOO
OOOOO
Frame: 3
OOOOO
OOOOO
OXXXO
OOOOO
OOOOO
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
int frame[100][100];
void life(int n,int m) {
//Copies the main array to a frame array so changes can be entered into a grid
//without effecting the other cells and the calculations being performed on them.
int count;
for(int i = 1 ; i <=n ; i++) {
for(int j = 1; j <= m; j++) {
count = 0;
count = a[i-1][j] + a[i][j-1] + a[i+1][j] + a[i][j+1] + a[i-1][j+1]
+ a[i+1][j-1] + a[i-1][j-1] + a[i+1][j+1];
//The cell dies.
if(count < 2 || count > 3)
frame[i][j] = 0;
//The cell stays the same.
if(count == 2)
frame[i][j] = a[i][j];
//The cell either stays alive, or is "born".
if(count == 3)
frame[i][j] = 1;
}
}
}
void print(int m,int n)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
printf("%d",frame[i][j]);
printf("/n");
}
}
int main()
{
char *filename;
int frames;
printf("Enter the file name:");
scanf("%c",&filename);
printf("Enter no. of additional frames:");
scanf("%c",&frames);
FILE *file = open(fileName, "r");
char c=(fgetc(file));
int m,n,w;
if(c=='P')
{
if(fgetc(file)=='N')
{
m = (int)fgetc(file);
n = (int)fgetc(file);
w = (int)fgetc(file);
int i=1,j=1;
for(int i=0;i<n+2;i++)
{
for(int j=0;j<m+2;j++)
{
if(i==0 || i==n+1) frame[i][j] = 0;
else fscanf(file, "%1d", &frame[i][j]);
}
}
}
} in.close();
for(int i=0;i<frames;i++)
{
printf("/nFrame %d",i+1);
life(m,n);
print(m+2,n+2);
printf("/n");
}
}
return 0;
}