I am having some problems with this C++ assignment. It\'s the Game of Life assig
ID: 3684177 • Letter: I
Question
I am having some problems with this C++ assignment.
It's the Game of Life assignment (have to run 50 cycles of a particular pattern). I have written the code for the game and that part is working fine. My issue is that I am not sure how to output the fill percentage at the end and how to make it so that the program clears the screen after every cycle and there's a one second pause in between each cycle so that it's easier to see the cell changes. I tried using system commands like clear and sleep but they weren't working.
The rules are simple
The game is played on a rectangular grid of any size.
The game consists of cycles
At each cycle of the game, every cell on the grid is either alive, “1”, or dead, “0”.
In the 0 cycle a starting pattern is generated.
At each subsequent cycle the pattern is evolved according to the rules.
The state of a cell may change from cycle to cycle depending on the population of the 8 adjacent cells.
If a cell is dead, “0” and exactly three of its closest 8-neighbors are alive, “1”, in the current cycle, then the cell will be born, “1”, in the next cycle.
If a cell is alive, “1” in the current cycle, and either 2 or 3 of its neighbors are alive it will survive, “1”, into the next cycle.
In all other cases, the cell dies “0” in the next cycle.
The game continues for as long as the user chooses to run the cycling.
Some patterns grow, some are stable, and some die off over time.
You will display the current state at each cycle by printing spaces for dead cells and a character for live cells in the corresponding positions on your display.
Solving the problem requires 2 arrays, one to calculate the next state and one to hold and display the current state. During recalculation, the calculation array accepts the next state information. Once the calculation of the entire array is complete, the calculation array is copied to the display array and the new state is printed to the screen along with a fill percentage, a cycle count, and a signature.
Part 1
Write a program to generate and display a random starting array
Calculate and display fill percentage. (2 decimal places please)
Save the output screen.
Write a function to update “cycle” the array.
Write a function to display the array
Cycle through the array
Print a ‘0’ in every live cell.
Print a space ‘ ‘ in every dead cell.
Calculate and display fill percentage and cycle number
Run the program for 50 update cycles and save the output screen.
Thank you in advance.
Here's the code so far:
#include
#include
#include
#include
#include
using namespace std;
void copy(int array1[24][79], int array2[24][79])
{
for(int j = 0; j < 24; j++)
{
for(int i = 0; i < 79; i++)
array2[j][i] = array1[j][i];
}
}
void life(int array[24][79], char choice)
{
int temp[24][79];
copy(array, temp);
for(int j = 1; j < 24; j++)
{
for(int i = 1; i < 79; i++)
{
if(choice == 'm')
{
int count = 0;
count = array[j-1][i] +
array[j-1][i-1] +
array[j][i-1] +
array[j+1][i-1] +
array[j+1][i] +
array[j+1][i+1] +
array[j][i+1] +
array[j-1][i+1];
.
if(count < 2 || count > 3)
temp[j][i] = 0;
else if(count == 2)
temp[j][i] = array[j][i];
else if(count == 3)
temp[j][i] = 1;
}
else if(choice == 'v')
{
int count = 0;
count = array[j-1][i] +
array[j][i-1] +
array[j+1][i] +
array[j][i+1];
if(count < 2 || count > 3)
temp[j][i] = 0;
else if(count == 2)
temp[j][i] = array[j][i];
else if(count == 3)
temp[j][i] = 1;
}
}
}
copy(temp, array);
}
bool compare(int array1[24][79], int array2[24][79])
{
int count = 0;
for(int j = 0; j < 24; j++)
{
for(int i = 0; i < 79; i++)
{
if(array1[j][i]==array2[j][i])
count++;
}
}
if(count == 24*79)
return true;
else
return false;
}
void print(int array[24][79])
{
for(int j = 1; j < 24; j++)
{
for(int i = 1; i < 79; i++)
{
if(array[j][i] == 1)
cout << 'Q';
else
cout << ' ';
}
cout << endl;
}
}
int main()
{
int gen0[24][79];
int todo[24][79];
int backup[24][79];
char neighborhood;
char again;
char cont;
bool comparison;
string decoration;
do
{
do
{
cout << "Which neighborhood would you like to use (m or v): ";
cin >> neighborhood;
}while(neighborhood != 'm' && neighborhood != 'v');
system("clear");
int i = 0;
do
{
srand(time(NULL));
for(int j = 1; j < 24; j++)
{
for (int i = 1; i < 79; i++)
gen0[j][i] = rand() % 2;
}
if(i < 10)
decoration = "#############";
else if(i >= 10 && i < 100)
decoration = "##############";
else if(i >= 100 && i < 1000)
decoration = "###############";
else if(i >= 1000 && i < 10000)
decoration = "################";
else
decoration = "#################";
cout << decoration << endl << "Generation " << i
<< ":" << endl << decoration << endl << endl;
if(i == 0)
copy(gen0, todo);
copy(todo, backup);
print(todo);
life(todo, neighborhood);
i++;if(i % 50 == 1 && i != 1)
{
cout << endl;
do
{
cout << "Would you like to continue this simulation? (y/n): ";
cin >> cont;
}while(cont != 'y' && cont != 'n');
if(cont == 'n')
break;
}
comparison = compare(todo, backup);
if(comparison == false)
system("clear");
if(comparison == true)
cout << endl;
}while(comparison == false);
//Loop to check for proper inputs.
do
{
cout << "Would you like to run another simulation? (y/n): ";
cin >> again;
}while(again != 'y' && again != 'n');
}while(again == 'y');
return 0;
}
Explanation / Answer
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <unistd.h>
//#include
using namespace std;
void copy(int array1[24][79], int array2[24][79])
{
for(int j = 0; j < 24; j++)
{
for(int i = 0; i < 79; i++)
array2[j][i] = array1[j][i];
}
}
void life(int array[24][79], char choice)
{
int temp[24][79];
copy(array, temp);
for(int j = 1; j < 24; j++)
{
for(int i = 1; i < 79; i++)
{
if(choice == 'm')
{
int count = 0;
count = array[j-1][i] +
array[j-1][i-1] +
array[j][i-1] +
array[j+1][i-1] +
array[j+1][i] +
array[j+1][i+1] +
array[j][i+1] +
array[j-1][i+1];
if(count < 2 || count > 3)
temp[j][i] = 0;
else if(count == 2)
temp[j][i] = array[j][i];
else if(count == 3)
temp[j][i] = 1;
}
else if(choice == 'v')
{
int count = 0;
count = array[j-1][i] +
array[j][i-1] +
array[j+1][i] +
array[j][i+1];
if(count < 2 || count > 3)
temp[j][i] = 0;
else if(count == 2)
temp[j][i] = array[j][i];
else if(count == 3)
temp[j][i] = 1;
}
}
}
copy(temp, array);
}
bool compare(int array1[24][79], int array2[24][79])
{
int count = 0;
for(int j = 0; j < 24; j++)
{
for(int i = 0; i < 79; i++)
{
if(array1[j][i]==array2[j][i])
count++;
}
}
if(count == 24*79)
return true;
else
return false;
}
int print(int array[24][79])
{
int cnt=0;
for(int j = 1; j < 24; j++)
{
for(int i = 1; i < 79; i++)
{
if(array[j][i] == 1){
cout << 'Q';
cnt++;
}
else
cout << ' ';
}
cout << endl;
}
return cnt;
}
int main()
{
int gen0[24][79];
int todo[24][79];
int backup[24][79];
char neighborhood;
char again;
char cont;
bool comparison;
string decoration;
do
{
do
{
cout << "Which neighborhood would you like to use (m or v): ";
cin >> neighborhood;
}while(neighborhood != 'm' && neighborhood != 'v');
system("clear");
int i = 0;
do
{
srand(time(NULL));
for(int j = 1; j < 24; j++)
{
for (int i = 1; i < 79; i++)
gen0[j][i] = rand() % 2;
}
if(i < 10)
decoration = "#############";
else if(i >= 10 && i < 100)
decoration = "##############";
else if(i >= 100 && i < 1000)
decoration = "###############";
else if(i >= 1000 && i < 10000)
decoration = "################";
else
decoration = "#################";
cout << decoration << endl << "Generation " << i << ":" << endl << decoration << endl << endl;
if(i == 0)
copy(gen0, todo);
copy(todo, backup);
int count=print(todo);
cout<<endl;
cout<<"Fill Percentage is: "<< (double)count/(double)(23*78)<<endl<<endl;
life(todo, neighborhood);
i++;
if(i % 50 == 1 && i != 1)
{
cout << endl;
do
{
cout << "Would you like to continue this simulation? (y/n): ";
cin >> cont;
}while(cont != 'y' && cont != 'n');
if(cont == 'n')
break;
}
comparison = compare(todo, backup);
//if(comparison == false)
//system("clear");
if(comparison == true)
cout << endl;
sleep(1);
}while(comparison == false);
//Loop to check for proper inputs.
do
{
cout << "Would you like to run another simulation? (y/n): ";
cin >> again;
}while(again != 'y' && again != 'n');
}while(again == 'y');
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.