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

i need to generate a random pattern code for game of life code. also i need to c

ID: 644678 • Letter: I

Question

i need to generate a random pattern code for game of life code. also i need to calculate fill percentage. this is the code so far. image 1 is how it should look like but it obviously wont be the same as the initial pattern is completely random. then the code has to run for 50 cycles

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#define ALIVE true
#define DEAD false
using namespace std;
const int ROWS = 24;
const int COLS = 79;
void Display(bool grid[ROWS][COLS])
{
for(int a = 1; a < ROWS; a++){
for(int b = 1; b < COLS; b++){
if(grid[a][b] == true){
cout << "Q";
}
else{
cout << " ";
}
// if(b == COLS){
// std::cout << std::endl;
// }

}
cout << endl;
}

}

//This copy's the grid for comparision purposes.
void CopyGrid (bool grid[ROWS][COLS],bool grid2[ROWS][COLS]){
for(int a =0; a < ROWS; a++){
for(int b = 0; b < COLS; b++){grid2[a][b] = grid[a][b];}

}

}
//Calculates Life or Death
void liveOrDie(bool grid[ROWS][COLS])
{

bool grid2[ROWS][COLS] = {};
CopyGrid(grid, grid2);
for(int a = 1; a < ROWS-1; a++)
{
for(int b = 1; b < COLS-1; b++)
{
int neighbors = 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]) {++neighbors;}
}
}
}

if(neighbors < 2) {grid[a][b] = DEAD;}
else if(neighbors == 3) {grid[a][b] = ALIVE;}
else if(neighbors > 3) {grid[a][b] = DEAD;}

}

}
}

int main()

{

bool grid[ROWS][COLS] = {};

grid[ROWS/2][COLS/2] = true;
grid[ROWS/2-1][COLS/2] = true;
grid[ROWS/2][COLS/2+1] = true;
grid[ROWS/2][COLS/2-1] = true;
grid[ROWS/2+1][COLS/2+1] = true;

while (true){


Display(grid); //This is our display.
liveOrDie(grid); //calculate if it lives or dies.
Sleep(500);
// std::cin.get();


}
return 0;

}

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#define ALIVE true
#define DEAD false
using namespace std;
const int ROWS = 24;
const int COLS = 79;
void Display(bool grid[ROWS][COLS])
{
for(int a = 1; a < ROWS; a++){
for(int b = 1; b < COLS; b++){
if(grid[a][b] == true){
cout << "Q";
}
else{
cout << " ";
}
// if(b == COLS){
// std::cout << std::endl;
// }

}
cout << endl;
}

}

//This copy's the grid for comparision purposes.
void CopyGrid (bool grid[ROWS][COLS],bool grid2[ROWS][COLS]){
for(int a =0; a < ROWS; a++){
for(int b = 0; b < COLS; b++){grid2[a][b] = grid[a][b];}

}

}
//Calculates Life or Death
void liveOrDie(bool grid[ROWS][COLS])
{

bool grid2[ROWS][COLS] = {};
CopyGrid(grid, grid2);
for(int a = 1; a < ROWS-1; a++)
{
for(int b = 1; b < COLS-1; b++)
{
int neighbors = 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]) {++neighbors;}
}
}
}

if(neighbors < 2) {grid[a][b] = DEAD;}
else if(neighbors == 3) {grid[a][b] = ALIVE;}
else if(neighbors > 3) {grid[a][b] = DEAD;}

}

}
}

int main()

{
srand(time(NULL));
bool grid[ROWS][COLS] = {};

for(int i = 0; i < ROWS; ++i){
   for(int j = 0; j < COLS; ++j){
       grid[i][j] = rand() % 2;
   }
}

Display(grid);

grid[ROWS/2][COLS/2] = true;
grid[ROWS/2-1][COLS/2] = true;
grid[ROWS/2][COLS/2+1] = true;
grid[ROWS/2][COLS/2-1] = true;
grid[ROWS/2+1][COLS/2+1] = true;

for (int i = 0; i < 50; ++i){


Display(grid); //This is our display.
liveOrDie(grid); //calculate if it lives or dies.
Sleep(500);
// std::cin.get();


}
return 0;

}