You will design, implement, and test a program that runs the game of life in C++
ID: 3662125 • 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. The 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.
5. You must give the user the option for one of three starting shapes in the grid; an oscillator, a glider, and a glider cannon.
6. The user must be able to specify the starting locaiton of the selected pattern, they will specify a single cell and make it clear which cell they are using.
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 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. TOtherwise they could put a glider at an edge and the glider moves away and they never see it!
Explanation / Answer
#include <iostream>
#include <cstdlib>
//Making this a global constants of size 40 by 20
const int row_size = 40;
const int col_size = 20;
void Display(bool grid[gridsize+1][gridsize+1]){
for(int a = 1; a < gridsize; a++){
for(int b = 1; b < gridsize; b++){
if(grid[a][b] == true){
std::cout << " *";
}
else{
std::cout << " ";
}
if(b == gridsize-1){
std::cout << std::endl;
}
}
}
}
//This copy's the grid for comparision purposes.
void CopyGrid (bool grid[gridsize+1][gridsize+1],bool grid2[gridsize+1][gridsize+1]){
for(int a =0; a < gridsize; a++){
for(int b = 0; b < gridsize; b++){grid2[a][b] = grid[a][b];}
}
}
//Calculates Life or Death
void liveOrDie(bool grid[gridsize+1][gridsize+1]){
bool grid2[gridsize+1][gridsize+1] = {};
CopyGrid(grid, grid2);
for(int a = 1; a < gridsize; a++){
for(int b = 1; b < gridsize; b++){
int life = 0;
for(int c = -1; c < 2; c++){
for(int d = -1; d < 2; d++){
if(!(c == 0 && d == 0)){
if(grid2[a+c][b+d]) {++life;}
}
}
}
if(life < 2) {grid[a][b] = false;}
else if(life == 3){grid[a][b] = true;}
else if(life > 3){grid[a][b] = false;}
}
}
}
int main(){
//const int gridsize = 50;
bool grid[gridsize+1][gridsize+1] = {};
//Still have to manually enter the starting cells.
grid[gridsize/2][gridsize/2] = true;
grid[gridsize/2-1][gridsize/2] = true;
grid[gridsize/2][gridsize/2+1] = true;
grid[gridsize/2][gridsize/2-1] = true;
grid[gridsize/2+1][gridsize/2+1] = true;
while (true){
//The following copies our grid.
Display(grid); //This is our display.
liveOrDie(grid); //calculate if it lives or dies.
system("CLS");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.