Please help me. I am having a problem with this problem. I need to add more func
ID: 3844288 • Letter: P
Question
Please help me. I am having a problem with this problem.
I need to add more functions to complete the homework.
this is the code that I have done so far.
#include <iostream>
#include <stdlib.h>
using namespace std;
const int MAX_COL = 60;
const int MAX_ROW = 30;
void displayMenu()
{
cout<<"[P]lay Press 'P' to play. [Q]uit Press 'Q' to exit."<<endl;
}
void setZeroArray (int (&tempArray)[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
tempArray[i][j] = 0;
}
}
}
void copyArray(int tempArray[MAX_ROW][MAX_COL], int (¤tArray)[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
currentArray[i][j] = tempArray[i][j];
}
}
}
void displayArray (int arr[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
cout<<arr[i][j];
}
cout<<endl;
}
}
void setInitialPatternArray(int (&tempArray)[MAX_ROW][MAX_COL])
{
int temprow = rand() % 10 + 1;
int tempcol = rand() % 10 + 1;
for(int i=temprow;i<temprow+5;i++)
{
tempArray[i][tempcol] = 1;
tempArray[i][tempcol+5] = 1;
}
for(int i=tempcol+1;i<tempcol+5;i++)
{
tempArray[temprow+5][i] = 1;
}
}
int main()
{
int currentArray[MAX_ROW][MAX_COL];
int tempArray[MAX_ROW][MAX_COL];
string choice;
while(true)
{
displayMenu();
cin>>choice;
if(choice == "Q" || choice == "q")
{
break;
}
if (choice == "P" || choice == "p")
{
system ("cls");
}
setZeroArray(currentArray);
setZeroArray(tempArray);
setInitialPatternArray(tempArray);
copyArray(tempArray, currentArray);
displayArray(currentArray);
}
return 0;
}
From here, I need to add more function.
1. Use the global variables properly:
const int MAX_COL = 60;
const int MAX_ROW = 30;
for example:
int currentArray[MAX_ROW][MAX_COL];
instead of int currentArray[30][60];
****In your program, replace '60' with MAX_COL and replace '30' with MAX_ROW.
****When declaring variables, array names, use a meaningful name; don't just use x, y, a, b, etc. Don't just use a single character. (-30 points)
2. Write the definition of the function setNextGenArray that creates a pattern of next generation (tempArray) based on the current generation (currentArray); modify the tempArray based on the currentArray by applying the rules of Game of Life.
Conway's Game of Life Rules:
- The neighbors of a given cell are the cells that touch it vertically, horizontally, or diagonally.
- Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overpopulation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
3. Create a loop to generate and next pattern automatically.
When executing your program, the following should happen:
a. Print the menu using the displayMenu function.
b. Initialize the tempArray using the setZeroArray function.
c. Set the ‘U’ pattern in the tempArray using the setInitialPatternArray function.
d. Copy the tempArray to the currentArray using the copyArray function.
e. Print the currentArray using the displayArray function.
f. When the user presses ‘P’, (loop the following three steps)
i. Generate a next pattern using the setNextGenArray function
ii. Copy the tempArray to the currentArray using the copyArray function.
iii. Print the currentArray using the displayArray function.
g. When the user presses ‘Q’, it will terminate the program.
When I press 'p', the program will have to keep generating the specific patterns automatically just like Game of Life.
Also, when I press 'q', the program will have to disappear.
help me.
Explanation / Answer
Hey There! I have heavily commented the code for deeper understanding. When P is pressed the program starts and updates continuously. Unfortunately there is no easy way to make the program terminate from the infinite loop when a key is pressed. That part is implementation dependent (Operating System or compiler). You can press Ctrl + C while executing to terminate the program. That's the best what you can do using the current headers.
Also as of using system("cls"), this only works on windows. If you are using linux then you have to replace it with system("clear")
Hope it helps
#include <iostream>
#include <stdlib.h>
using namespace std;
const int MAX_COL = 60;
const int MAX_ROW = 30;
void displayMenu()
{
cout<<"[P]lay Press 'P' to play. [Q]uit Press 'Q' to exit."<<endl;
}
void setZeroArray (int (&tempArray)[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
tempArray[i][j] = 0;
}
}
}
void copyArray(int tempArray[MAX_ROW][MAX_COL], int (¤tArray)[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
currentArray[i][j] = tempArray[i][j];
}
}
}
void displayArray (int arr[MAX_ROW][MAX_COL])
{
for(int i=0;i<MAX_ROW;i++)
{
for(int j=0;j<MAX_COL;j++)
{
cout<<arr[i][j];
}
cout<<endl;
}
}
void setInitialPatternArray(int (&tempArray)[MAX_ROW][MAX_COL])
{
int temprow = rand() % 10 + 1;
int tempcol = rand() % 10 + 1;
for(int i=temprow;i<temprow+5;i++)
{
tempArray[i][tempcol] = 1;
tempArray[i][tempcol+5] = 1;
}
for(int i=tempcol+1;i<tempcol+5;i++)
{
tempArray[temprow+5][i] = 1;
}
}
void setNextGenArray(int (&tempArray)[MAX_ROW][MAX_COL])
{
int sourroundingLiveCells = 0;
for (int i = 0; i < MAX_ROW; i++) {
for (int j = 0; j < MAX_COL; j++) {
sourroundingLiveCells = 0;
if(i == 0 && j == 0){ // Checking for live cells in top left corner
sourroundingLiveCells = tempArray[0][1] + tempArray[1][1] + tempArray[1][0];
}
else if(i == 0 && j == MAX_COL - 1){ // Checking for live cells in top right corner
sourroundingLiveCells = tempArray[0][MAX_COL-2] + tempArray[1][MAX_COL-2] + tempArray[1][MAX_COL-1];
}
else if(i == MAX_ROW - 1 && j == 0){ // Checking for live cells in bottom left corner
sourroundingLiveCells = tempArray[MAX_ROW - 2][0] + tempArray[MAX_ROW - 2][1] + tempArray[MAX_ROW - 1][1];
}
else if(i == MAX_ROW - 1 && j == MAX_COL - 1){ // Checking for live cells in bottom right corner
sourroundingLiveCells = tempArray[MAX_ROW - 1][MAX_COL - 2] + tempArray[MAX_ROW - 2][MAX_COL - 2] + tempArray[MAX_ROW - 2][MAX_COL - 1];
}
else if(i == 0){ // Checking for live cells in top row
sourroundingLiveCells = tempArray[0][j - 1] + tempArray[0][j + 1] + tempArray[1][j - 1] + tempArray[1][j] + tempArray[1][j+1];
}
else if(i == MAX_ROW - 1){ // Checking for live cells in bottom row
sourroundingLiveCells = tempArray[MAX_ROW - 1][j - 1] + tempArray[MAX_ROW - 1][j + 1] + tempArray[MAX_ROW - 2][j - 1] + tempArray[MAX_ROW - 2][j] + tempArray[MAX_ROW - 2][j+1];
}
else if(j == 0){ //Checking for live cells in left column
sourroundingLiveCells = tempArray[i + 1][j] + tempArray[i - 1][j] + tempArray[i - 1][j + 1] + tempArray[i][j + 1] + tempArray[i + 1][j + 1];
}
else if(j == MAX_COL - 1){ // Checking for live cells in right column
sourroundingLiveCells = tempArray[i - 1][MAX_COL - 1] + tempArray[i + 1][MAX_COL - 1] + tempArray[i - 1][MAX_COL - 2] + tempArray[i][MAX_COL - 2] + tempArray[i + 1][MAX_COL - 2];
}
else{ // Checking for live cells in whole array except outer border, which we have already checked in above cases
sourroundingLiveCells = tempArray[i-1][j] +
tempArray[i+1][j] +
tempArray[i][j-1] +
tempArray[i+1][j-1] +
tempArray[i-1][j-1] +
tempArray[i+1][j+1] +
tempArray[i][j+1] +
tempArray[i-1][j+1];
}
if(tempArray[i][j] == 0) //Dead Cell
{
if(sourroundingLiveCells == 3){ //If 3 live cells surround a dead cell
tempArray[i][j] == 1; //The cell becomes alive
}
}
else //Live Cell
{
if(sourroundingLiveCells < 2 || sourroundingLiveCells > 3){
tempArray[i][j] == 0; //The cell dies
}
//otherwise the cell stays alive i.e. if sourroundingLiveCells equals 2 or 3
}
}
}
}
int main()
{
int currentArray[MAX_ROW][MAX_COL];
int tempArray[MAX_ROW][MAX_COL];
string choice;
int char ch;
displayMenu(); //Display menu for the first time
cin>>choice;
if(choice == "Q" || choice == "q")
{
break;
}
if (choice == "P" || choice == "p")
{
system("cls");
}
setZeroArray(tempArray); //This should not be included in the given infinite loop otherwise tempArray will be setted to zero everytime which we don't want.
setInitialPatternArray(tempArray); //Same for this
copyArray(tempArray, currentArray); // Copying tempArray to currentArray
displayArray(currentArray); // Displaying the currentArray for the first time
while(true)
{
setNextGenArray(tempArray); // Using our setNextGenArray method to modify our tempArray according to the rules of the game.
copyArray(tempArray, currentArray); // Copying the tempArray to currentArray
system ("cls");
displayArray(currentArray); //Displaying the currentArray
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.