Question
Can any one help me fix the code as these requirements. Thank you in advance. I am using c++ The requirements are: 1. When you
Can any one help me fix the code as these requirements. Thank you in advance. I am using c++
The requirements are:
1. When you start the program, there will be the parttern as the picture, and when you press P (you just press P, you don't need to press enter), the program will start to generate patterns.( This is a game of life program)
2. You can reset when you press any button
And this is my code:
#include #include #include #include #include using namespace std;
const int MAX_ROW = 30; const int MAX_COL = 60; const int PATTERN_HEIGHT = 6; const int PATTERN_WIDTH = 7;
void displayMenu(); void displayArray(const int array[MAX_ROW][MAX_COL]); void copyArray(const int fromArray[MAX_ROW][MAX_COL], int toArray[MAX_ROW][MAX_COL]); void setZeroArray(int array[MAX_ROW][MAX_COL]); void setInitialPatternArray(int array[MAX_ROW][MAX_COL]); void setNextGenArray(const int fromArray[MAX_ROW][MAX_COL], int toArray[MAX_ROW][MAX_COL]);
int main() { char option; int currentArray[MAX_ROW][MAX_COL]; int tempArray[MAX_ROW][MAX_COL];
srand(time(NULL));
do { setZeroArray(tempArray); setInitialPatternArray(tempArray); copyArray(tempArray, currentArray); displayArray(currentArray); while(true) { system("cls"); displayMenu(); if(kbhit()) { break; } else { setNextGenArray(currentArray, tempArray); copyArray(tempArray, currentArray); displayArray(currentArray); Sleep(800); } } option = getch(); }while(option != 'q');
return 0; }
void displayMenu() { cout
void displayArray(const int array[MAX_ROW][MAX_COL]) { for(int coordX = 0; coordX
void copyArray(const int fromArray[MAX_ROW][MAX_COL], int toArray[MAX_ROW][MAX_COL]) { for(int coordX = 0; coordX
void setZeroArray(int array[MAX_ROW][MAX_COL]) { for(int coordX = 0; coordX
void setInitialPatternArray(int array[MAX_ROW][MAX_COL]) { int startRow = rand() % (MAX_ROW - PATTERN_HEIGHT); int startCol = rand() % (MAX_COL - PATTERN_WIDTH);
for(int len = 0; len
for(int len = 0; len
void setNextGenArray(const int fromArray[MAX_ROW][MAX_COL], int toArray[MAX_ROW][MAX_COL]) { for(int coordX = 0; coordX
if((coordX == 0) && (coordY == 0)) { if(fromArray[0][1] == 1) alive++; if(fromArray[1][1] == 1) alive++; if(fromArray[1][0] == 1) alive++; } else if((coordX == 0) && (coordY == (MAX_COL - 1))) { if(fromArray[0][MAX_COL - 2] == 1) alive++; if(fromArray[1][MAX_COL - 2] == 1) alive++; if(fromArray[1][MAX_COL - 1] == 1) alive++; } else if((coordX == (MAX_ROW - 1)) && (coordY == 0)) { if(fromArray[MAX_ROW - 1][1] == 1) alive++; if(fromArray[MAX_ROW - 2][1] == 1) alive++; if(fromArray[MAX_ROW - 2][0] == 1) alive++; } else if((coordX == (MAX_ROW - 1)) && (coordY == (MAX_COL - 1))) { if(fromArray[MAX_ROW - 1][MAX_COL - 2] == 1) alive++; if(fromArray[MAX_ROW - 2][MAX_COL - 2] == 1) alive++; if(fromArray[MAX_ROW - 2][MAX_COL - 1] == 1) alive++; } else if(coordX == 0) { if(fromArray[0][coordY - 1] == 1) alive++; if(fromArray[1][coordY - 1] == 1) alive++; if(fromArray[1][coordY] == 1) alive++; if(fromArray[1][coordY + 1] == 1) alive++; if(fromArray[0][coordY + 1] == 1) alive++; } else if(coordX == (MAX_ROW - 1)) { if(fromArray[MAX_ROW - 1][coordY - 1] == 1) alive++; if(fromArray[MAX_ROW - 2][coordY - 1] == 1) alive++; if(fromArray[MAX_ROW - 2][coordY] == 1) alive++; if(fromArray[MAX_ROW - 2][coordY + 1] == 1) alive++; if(fromArray[MAX_ROW - 1][coordY + 1] == 1) alive++; } else if(coordY == 0) { if(fromArray[coordX - 1][0] == 1) alive++; if(fromArray[coordX - 1][1] == 1) alive++; if(fromArray[coordX][1] == 1) alive++; if(fromArray[coordX + 1][1] == 1) alive++; if(fromArray[coordX + 1][0] == 1) alive++; } else if(coordY == (MAX_COL - 1)) { if(fromArray[coordX - 1][MAX_COL - 1] == 1) alive++; if(fromArray[coordX - 1][MAX_COL - 2] == 1) alive++; if(fromArray[coordX][MAX_COL - 2] == 1) alive++; if(fromArray[coordX + 1][MAX_COL - 2] == 1) alive++; if(fromArray[coordX + 1][MAX_COL - 1] == 1) alive++; } else { if(fromArray[coordX - 1][coordY - 1] == 1) alive++; if(fromArray[coordX - 1][coordY] == 1) alive++; if(fromArray[coordX - 1][coordY + 1] == 1) alive++; if(fromArray[coordX][coordY + 1] == 1) alive++; if(fromArray[coordX + 1][coordY + 1] == 1) alive++; if(fromArray[coordX + 1][coordY] == 1) alive++; if(fromArray[coordX + 1][coordY - 1] == 1) alive++; if(fromArray[coordX][coordY - 1] == 1) alive++; }
if(fromArray[coordX][coordY] == 1) { if((alive == 2) || (alive == 3)) { toArray[coordX][coordY] = 1; } else { toArray[coordX][coordY] = 0; } } else { if(alive == 3) { toArray[coordX][coordY] = 1; } else { toArray[coordX][coordY] = 0; } } } } }
This is initial screen:
2 When you press P, it will automatic generate pattern continuously
3. When you press any button, it will be like in the initial screen, but the U pattern is on random position
D:AHOC\CIS121LAb\GoL_Project-Part2.exe P]lay Press p' to play. [Qluit Press qto quit 00000000000000000000000001000001000000000000000000000000000 00000000000000000000000001000001000000000000000000000000000 00000000000000000000000001000001000000000000000000000000000 00000000000000000000000001000001000000000000000000000000000 00000000000000000000000001000001000000000000000000000000000 00000000000000000000000001111111000000000000000000000000000Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started