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

game of life C++ program (my part for part 1.) #include <iostream> #include <std

ID: 3866243 • Letter: G

Question

game of life

C++ program

(my part for part 1.)

#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." <<endl;
   cout << "[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 (&currentArray)[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")
       {
           break;
       }
       setZeroArray(currentArray);
       setZeroArray(tempArray);
       setInitialPatternArray(tempArray);
       copyArray(tempArray, currentArray);
       displayArray(currentArray);
   }
   return 0;
}

(part 2 problem)

1. Use the global variables properly:


const int MAX_ROW = 30;

const int MAX_COL = 60;

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.

if useful (loop):

#include
#include //kbhit()
#include //Sleep()

using namespace std;

int main()
{
   char ans;
   bool t;
   do
   {
       int i = 0;
       while (1)
       {
           if (t = kbhit())
           {
               break;
           }
           else
           {
               system("cls");
               cout << "Hello" << endl;
               cout << i;
               i++;
               Sleep(1000);  
           }
       }
  
       ans = getch();
         
   }while (ans != 'q');
      
   return 0;
}

OUTPUTS:

**************************************************************************************

After pressing [P]lay - random patterns

[P]lay Press 'p' to play. [Q]uit = Press 'q' to quit. 000000000000000000010000010000000000000000000000000000000000 000000000000000000010000010000000000000000000000000000000000 000000000000000000010000010000000000000000000000000000000000 000000000000000000010000010000000000000000000000000000000000 000000000000000000010000010000000000000000000000000000000000 000000000000000000011111110000000000000000000000000000000000

Explanation / Answer

game.cpp

#include<iostream>
#include <stdlib.h>
using namespace std;
const int MAX_COL = 60;
const int MAX_ROW = 30;
void copyArray(int (&tempArray)[MAX_ROW][MAX_COL], int (&currentArray)[MAX_ROW][MAX_COL]);
void displayMenu();
void setZeroArray(int (&tempArray)[MAX_ROW][MAX_COL]);
void setInitialPatternArray(int (&tempArray)[MAX_ROW][MAX_COL]);
void displayArray(int (&currentArray)[MAX_ROW][MAX_COL]);
void setNextGenArray(int (&tempArray)[MAX_ROW][MAX_COL], int (&currentArray)[MAX_ROW][MAX_COL]);
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;
}
setZeroArray(currentArray);
setZeroArray(tempArray);
setInitialPatternArray(tempArray);
copyArray(tempArray, currentArray);
displayArray(currentArray);
if(choice=="P"||choice=="p")
   {
      //setNextGenArray(tempArray,currentArray);
      copyArray(tempArray, currentArray);
displayArray(currentArray);
   }
   else
   {
      cout<<"wrong Choice: please check"<<endl;
   }
}
return 0;
}

void setNextGenArray(int (&tempArray)[MAX_ROW][MAX_COL], int (&currentArray)[MAX_ROW][MAX_COL])
{
   int cell;
   for(int k=0;k<MAX_ROW;k++)
for(int i=0;i<MAX_COL;i++)
{
if(i != k)
{
cell=currentArray[i][k]/currentArray[k][k];
for(int j=0;j<MAX_COL+1;j++)
{
tempArray[i][j]=currentArray[i][j]-(cell*tempArray[k][j]);
}
}
}
for(int i=0;i<MAX_ROW;i++)
{
cell=tempArray[i][i];
for(int j=0;j<MAX_COL;j++)
{
tempArray[i][j]=tempArray[i][j]/cell;
}
}
}

void displayMenu()
{
cout << "[P]Play Press 'P' to play." <<endl;   
cout << "[Q]Quit 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 (&currentArray)[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<MAX_ROW;i++)
{
tempArray[i][tempcol] = 1;
tempArray[i][tempcol+5] = 1;
}
for(int i=tempcol+1;i<MAX_COL;i++)
{
tempArray[temprow+5][i] = 1;
}
}