!! IN C LANGUAGE CONWAY\'S GAME OF Life The universe of the Game of Life is an i
ID: 3723762 • Letter: #
Question
!! IN C LANGUAGE
CONWAY'S GAME OF Life
The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead, or "populated" or "unpopulated". Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overpopulation.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.
Your project is to create a game of life on a 2-d grid that will be no more than 50 x 50.
Your code should be able to simulate and display up to 50 timesteps.
The exact size of the grid and the number of timesteps to be simulated will be specified by the user.
Your code must have a 3d array declared in main of size [MAXSIZE][MAXSIZE][MAXTIME+1], where MAXSIZE is 50 and MAXTIME is 50
Your code must include the following two functions
int neighbors(int anArray[MAXSIZE][MAXSIZE][MAXTIME+1], int i, int j, int size, int step) // returns the number of neighbors of position i,j that are alive at time step. void printGrid(int myGrid[MAXSIZE][MAXSIZE][MAXTIME+1],int size,int step) // print the grid of size sizexsize at time step.
For example:
Welcome to Conway's game of Life
Please enter the n for the n by n grid to simulate, max size for n is 50. :10
Thank You, now enter the initial x y coordinates (the board is indexed starting from 0 0) for the initial live cells, enter
-1 -1 when done
7 7
7 8
7 9
8 7
9 8
-1 -1
Please enter the number of time steps you wish to simulate, max number allowed is 50. 12
Num time steps 12
After 12 timesteps the grid is:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
!! IN C LANGUAGE PLEASE !!
Explanation / Answer
#include <stdio.h>
int neighbors(int arr[50][50][51], int i, int j, int size, int step);
void printGrid(int arr[50][50][51],int size,int step);
int main()
{
int arr[50][50][51]={0};
int n=0,i,j,k,a,b;
printf("Please enter the n for the n by n grid to simulate, max size for n is 50.");
scanf("%d",&n);
int x=-999,y=-999;
printf("Thank You,");
printf("now enter the initial x y coordinates (the board is indexed starting from 0 0) for the initial live cells, ");
printf("enter -1 -1 when done ");
while(x!=-1 ||y!=-1)
{
scanf("%d",&x);
scanf("%d",&y);
arr[x][y][0]=1;
}
int steps;
printf("Please enter the number of time steps you wish to simulate, max number allowed is 50.");
scanf("%d",&steps);
int c=0;
for(k=1;k<=50;k++)
{for(i=0;i<50;i++)
{for(j=0;j<50;j++)
{c=neighbors(arr,i,j,50,k-1);
if(arr[i][j][k-1]==1 && c<2)
arr[i][j][k]=0;
else if(arr[i][j][k-1]==1 &&(c==2 || c==3) )
arr[i][j][k]=1;
else if(arr[i][j][k-1]==1 &&(c>3) )
arr[i][j][k]=0;
else if(arr[i][j][k-1]==0 &&(c==3) )
arr[i][j][k]=1;
}
}
}
printf("After %d steps the array is: ",steps);
printGrid(arr,n,steps);
return 0;
}
int neighbors(int arr[50][50][51], int i, int j, int size, int step)
{
int alive=0;
if(i-1>=0 && j-1>=0)
if(arr[i-1][j-1][step]==1)
alive++;
if(i-1>=0)
if(arr[i-1][j][step]==1 )
alive++;
if(i-1>=0 && j+1<size)
if(arr[i-1][j+1][step]==1)
alive++;
if(j-1>=0)
if(arr[i][j-1][step]==1)
alive++;
if(j+1<=size)
if(arr[i][j+1][step]==1)
alive++;
if(i+1<size && j-1>=0)
if(arr[i+1][j-1][step]==1)
alive++;
if(i+1<size)
if(arr[i+1][j][step]==1 )
alive++;
if(i+1<size && j+1<size)
if(arr[i+1][j+1][step]==1)
alive++;
return alive;
}
void printGrid(int arr[50][50][51],int size,int step)
{int i,j;
for(i=0;i<size;i++)
{for(j=0;j<size;j++)
printf("%d ",arr[i][j][step]);
printf(" ");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.