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

You will design, implement, and test a program that runs the game of life in C++

ID: 3662115 • Letter: Y

Question

You will design, implement, and test a program that runs the game of life in C++. Conway’s Game of Life is a standard example of a cellular automaton. This means that you have an array or matrix of cells. Each turn or generation the value of each cell may change based upon its neighbors. he rules are: Each cell has eight neighbor cells. The neighbors are the cells directly above, below, to the
right, to the left, diagonally above to the right and left, and diagonally below to the right and left.
1. If an occupied cell has zero or one neighbor, it dies of loneliness.
2. If an occupied cell has more than three neighbors, it dies of overcrowding.
3. If an empty cell has exactly three occupied neighbor cells, there is a birth of a new cell
to replace the empty cell.
4. Births and deaths are instantaneous and occur at the changes of generation.

There are significant initial questions that you must address in your design. We will limit the visible “world” to 40 x 20 cells. That’s a lot of characters to type in from the keyboard so don’t make the user do it. You must give the user the option for one of three starting shapes in the grid; an oscillator, a glider, and a glider cannon.

You will need to handle the cells on the edges. Remember this is a window on an infinite grid. The patterns go on forever. You are just showing them while they are visible in this small window. You may not be able to just stop the patterns at the edge as that may change the behavior. Specifically, any pattern should not change as the object moves out of the visible grid.

After you have everything designed, coded, and tested add an option for the user to specify the starting location of the selected pattern. They will specify a single cell. Make it clear which cell of the pattern they are using. Otherwise they could put a glider at an edge and the glider moves away and they never see it!

Explanation / Answer

#include <iostream>
#include <iomanip>
#define H 40 //Define height
#define W 20 //Define width
using namespace std;

//Sets matrix to all dead

void clear(bool mat[][W]) {
for (int m = 0; m < H; m++)
{
for (int n = 0; n < W; n++)
mat[m][n] = 0;
}
}

//Prints matrix to screen
void print(bool mat[][W])
{
cout << setw(3) << " ";
for (int p = 0; 5*p < W; p++) cout << setw(5) << 5*p+1;
cout << endl;
for (int m = 0; m < H; m++)
{
cout << setw(3) << m+1;
for (int n = 0; n < W; n++)
{
if (mat[m][n]) cout << "";
else cout << /*""*/"-";
}
cout << endl;
}
}

//Prints matrix to screen

void print2(unsigned int mat[][W]) {
for (int m = 0; m < H; m++)
{
for (int n = 0; n < W; n++)
cout << mat[m][n] << " ";
cout << endl;
}
}

void calculate(bool mata[][W], bool matb[][W])
{
unsigned int neighbors;
for (int m = 0; m < H; m++)
{
for (int n = 0; n < W; n++)
{
neighbors = 0;
//Begin counting number of neighbors:
if (mata[m-1][n-1] == 1) neighbors += 1;
if (mata[m-1][n] == 1) neighbors += 1;
if (mata[m-1][n+1] == 1) neighbors += 1;
if (mata[m][n-1] == 1) neighbors += 1;
if (mata[m][n+1] == 1) neighbors += 1;
if (mata[m+1][n-1] == 1) neighbors += 1;
if (mata[m+1][n] == 1) neighbors += 1;
if (mata[m+1][n+1] == 1) neighbors += 1;

//Apply rules to the cell:
if (mata[m][n] == 1 && neighbors < 2)
matb[m][n] = 0;
else if (mata[m][n] == 1 && neighbors > 3)
matb[m][n] = 0;
else if (mata[m][n] == 1 && (neighbors == 2 || neighbors == 3))
matb[m][n] = 1;
else if (mata[m][n] == 0 && neighbors == 3)
matb[m][n] = 1;
}
}
}

//Replaces first matrix with second

void swap(bool mata[][W], bool matb[][W]) {
for (int m = 0; m < H; m++)
{
for (int n = 0; n < W; n++)
mata[m][n] = matb[m][n];
}
}

int main()
{
//Creates now and then matrixes
bool now[H][W], next[H][W];
int x, y, cont; //Used for user input
  
cout << left << "Welcome to Conway's Game of Life." << endl << endl;
cout << "1. Any live cell with fewer than two live neighbors dies, as if by loneliness." << endl;
cout << "2. Any live cell with more than three live neighbors dies, as if by overcrowding." << endl;
cout << "3. Any live cell with two or three live neighbors lives, unchanged." << endl;
cout << "4. Any dead cell with exactly three live neighbors comes to life." << endl << endl;
cout << "To play: Press any key to begin. Enter the column and row of a cell to make alive, separated by a space. ";
cout << "When you are ready, enter "-1" to begin the simulation. Then enter any number to continue or "-1" to quit." << endl;
cin.get();
  
clear(now);
print(now);
  
//Get initial state
//using do while loop for iteration
do
{
cin >> x;
//User is done inputting
if (x == -1) break;
cin >> y;
//Sets cell to alive
now[y-1][x-1] = 1;

//Updates screen

print(now);
}while(x != -1);
  
//Keep updating new generations
do
{
clear(next);
calculate(now, next);
swap(now, next);
print(now);
cin>>cont;
}while(cont != -1);
  
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote