Please anyone who can do this problem using c programing? The Game of Life, inve
ID: 3751879 • 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
As per my knowledge, I have implemented the code in C++. I have implemented Conway's Game of life up to 50 generation
Find the code below.
#include <iostream>
using namespace std;
const int ROWS = 40;
const int COLS = 30;
int futureBoard[ROWS][COLS];
void GameBoard(int board[ROWS][COLS]);
void SwitchBoard (int board[ROWS][COLS]);
void NextGeneration(int board[ROWS][COLS]);
int main()
{
int board[ROWS][COLS] = {0};
board[1][2] = 1;
board[2][3] = 1;
board[3][1] = 1;
board[3][2] = 1;
board[3][3] = 1;
// Displaying the board
cout << "Original Generation" << endl;
for (int r = 0; r < ROWS; ++r)
{
for (int c = 0; c < COLS; ++c)
{
if (board[r][c] == 0)
cout << "|_|";
else
cout << "|*|";
}
cout << endl;
}
cout << endl;
cout << "print upto 50 generation" << endl;
for (int l = 0; l<50; l++)
{
cout << "Next Generation" << endl;
NextGeneration(board);
GameBoard(board);
SwitchBoard(board);
} return 0;
}
void GameBoard(int board[ROWS][COLS])
{
for (int r = 0; r < ROWS; ++r)
{
for (int c = 0; c < COLS; ++c)
{
if (board[r][c] == 0)
cout << "|_|";
else
cout << "|*|";
}
cout << endl;
}
}
void SwitchBoard (int board[ROWS][COLS])
{
futureBoard[ROWS][COLS] = {0};
for (int r = 0; r < ROWS; ++r)
{
for (int c = 0; c < COLS; ++c)
{
board[r][c]= futureBoard[r][c];
}
}
}
void NextGeneration(int board[ROWS][COLS])
{
// Loop through every cell
for (int r = 1; r < ROWS - 1; ++r)
{
for (int c = 1; c < COLS - 1; ++c)
{
// finding no Of Neighbours that are alive
int aliveNeighbours = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
aliveNeighbours += board[r + i][c + j];
// The cell needs to be subtracted from
// its neighbours as it was counted before
aliveNeighbours -= board[r][c];
// Implementing the Rules of Life
// Cell is lonely and dies
if ((board[r][c] == 1) && (aliveNeighbours < 2))
futureBoard[r][c] = 0;
// Cell dies due to over population
else if ((board) && (aliveNeighbours > 3))
futureBoard[r][c] = 0;
// A new cell is born
else if ((board[r][c] == 0) && (aliveNeighbours == 3))
futureBoard[r][c] = 1;
// Remains the same
else
futureBoard[r][c] = board[r][c];
}
}
}