Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please anyone who can do this problem using c programing? The Game of Life, inve

ID: 3751980 • Letter: P

Question

Please anyone who can do this problem using c programing?

The Game of Life, invented by John Conway in 1970, is an example of a zero-player “game” known as a cellular automaton. The game consists of a two-dimensional world extending infinitely in all directions, divided into “cells.” Each cell is either “dead” or “alive” at a given “generation.” The game consists of a set of rules that describe how the cells evolve from generation to generation. These rules calculate the state of a cell in the next generation as a function of the states of its neighboring cells in the current generation. In a 2-D world, a cell’s neighbors are those 8 cells vertically, horizontally, or diagonally adjacent to that cell. Conway’s set of rules are summarized as:

A live cell with fewer than two live neighbors dies.

A live cell with more than three live neighbors also dies.

A live cell with exactly two or three live neighbors lives.

A dead cell with exactly three live neighbors becomes alive.

In this lab, you will implement Conway’s Game of Life, with the minor restriction that our 2-D world is finite. The neighbors of a cell on the edge of the world that would be beyond the edge are assumed dead. You can read more about Conway’s Game of Life on Wikipedia at http://en.wikipedia.org/wiki/Conway’s_Game_of_Life.

You will implement a game of size 40 (width) by 30 (height).

You will start the game by setting 5 states to be alive, and all other states to be dead. The alive cells are located at (1, 2), (2, 3), (3, 1), (3, 2), (3, 3).

You will use a 2D array to represent the state of the game, and another 2D array to represent the next generation of the game.

You will implement a method called next_generation(), which computes the state of each cell based on the rules above, stores the results in the 2D array that represents the state of next generation.

After the next_generation() is executed, you should set the current state of the game based on the results generated from the next_generation(). You can do this by copy the state of each cell from the next generation 2D array to the current state 2D array.

In the function next_generation(), you should call function (that you will define and implement) get_next_state(x, y) to compute the state of cell at (x, y) in next generation, based on the number of alive neighbors, which you can inquiry by call function alive_neighbors(x, y) (which you will define and implement).

You should be able to output the state of the game, by printing text in the console. For each row of the game, two adjacent cells are separated by symbol "|". If a cell is alive, you print a "*", and "_" otherwise.

In the main function, you will implement the game to execute 50 generations, print out the result.

Explanation / Answer

#include <stdio.h>

int a[30][40];


int get_next_state(int x,int y)
{
int live_neighbours = 0;
// right
if(y!=39&&a[x][y+1]==1)
live_neighbours++;
//left
if(y!=0&&a[x][y-1]==1)
live_neighbours++;
//top
if(x!=0&&a[x-1][y]==1)
live_neighbours++;
//down
if(x!=29&&a[x+1][y]==1)
live_neighbours++;
//top left
if(y!=0&&x!=0&&a[x-1][y-1]==1)
live_neighbours++;
//top right
if(y!=39&&x!=0&&a[x-1][y+1]==1)
live_neighbours++;
//down left
if(y!=0&&x!=29&&a[x+1][y-1]==1)
live_neighbours++;
//down right
if(y!=39&&x!=29&&a[x+1][y+1]==1)
live_neighbours++;
if(a[x][y]==1)
{
if(live_neighbours<2||live_neighbours>3)
{
return 0;
}
return 1;
}
else
{
if(live_neighbours==3)
return 1;
return 0;
}
}

void next_generation()
{
int b[30][40];
for(int i = 0;i<30;i++)
{
for(int j = 0;j<40;j++)
{
b[i][j] = get_next_state(i,j);  
}
}
for(int i = 0;i<30;i++)
{
for(int j = 0;j<40;j++)
{
a[i][j] = b[i][j];  
}
}

}
void print()
{
for(int i = 0;i<30;i++)
{
for(int j = 0;j<40;j++)
{
  
if(a[i][j]==1)
printf("*");
else
printf("_");
if(j!=39)
printf("|");
}
printf(" ");
}
}

int main()
{
//printf("Hello World! ");
int i,j;
for(i=0;i<30;i++)
{
for(j=0;j<40;j++)
a[i][j] = 0;
}
  
a[1][2] = 1;
a[2][1] = 1;
a[3][1] = 1;
a[3][2] = 1;
a[3][3] = 1;
for(i=0;i<50;i++)
{
next_generation();
}
print();
return 0;
}