SlideShare a Scribd company logo
1 of 8
Download to read offline
implemement the game.cpp by the header file given. And create main.cpp to make the program
work.
The game is called the game of life, https://en.wikipedia.org/wiki/Conway's_Game_of_Life
game.h:
#include // Provides ostream
#include // String operations
#include // Randomizer
namespace csci2312
{
using std::string;
using std::ostream;
using std::istream;
class Cell
{
friend class GameOfLife;
public:
static const char alive ='o'; // alive image
static const char dead = '-'; // dead image
// Default constructor sets the cell's state to false
Cell();
// Custom constructor sets the cell's state as per argument
Cell(bool state);
// Empty destructor
~Cell();
// Accessors have no intention to modify the object, so it is a good practice to make them
'const' functions
bool getState() const;
// Mutator to change cell's state
void setState(bool newState);
// Accessor to see the 'face'
char getFace() const;
private:
bool state;
char face;
};
class GameOfLife
{
public:
static const unsigned int MAX_BOARD = 30;
GameOfLife();
GameOfLife(size_t boardSize);
~GameOfLife();
int seedBoard(string fileName);
void seedBoard(size_t seeds);
void run();
void run(unsigned int numberOfIterations);
// ADVANCED
// A const(!) accessor method that returns a handle to the private currentLife array.
// The return type must also be 'const' because we return a pointer to a static array, and
these are fixed
// It is just an example. It is not needed if we have a friend operator.
const Cell(*getCurrentLife() const )[MAX_BOARD+2] { return currentLife; };
///////////////////////////////////////////////////////
// friend operator can access private members of GameOfLife
friend ostream& operator << (ostream& out, const GameOfLife& board);
friend istream& operator >> (istream& in, const GameOfLife& board);
private:
bool executeRules(unsigned int countAlive, bool currentState);
// With "Halo" approach we need a bigger board
Cell currentLife[MAX_BOARD + 2][MAX_BOARD + 2];
Cell nextLife[MAX_BOARD + 2][MAX_BOARD + 2];
// ADVANCED
// Example how to declare variable cl as a pointer/handle to our array of Cells of size
HALO_BOARD
// The accessor method getCurrentLife() above uses the same syntax for the return type
const Cell(*cl)[MAX_BOARD + 2] = currentLife;
////////////////////////////////////////////////////////
size_t boardSize; // Board size requested in the constructor
};
// NON-MEMBER OUTPUT FUNCTIONS
// Display cell's state with alive/dead face
ostream& operator << (ostream& out, const Cell& cell);
}
Solution
Answer:
#include
#include
#include
#include
using namespace std;
void copy(int input1[52][102], int input2[52][102])
{
for(int j = 0; j < 52; j++)
{
for(int i = 0; i < 102; i++)
input2[j][i] = input1[j][i];
}
}
void lifegame(int input[52][102], char option)
{
int support[52][102];
copy(input, support);
for(int j = 1; j < 51; j++)
{
for(int i = 1; i < 101; i++)
{
if(option == 'm')
{
int increment = 0;
increment = input[j-1][i] +
input[j-1][i-1] +
input[j][i-1] +
input[j+1][i-1] +
input[j+1][i] +
input[j+1][i+1] +
input[j][i+1] +
input[j-1][i+1];
if(increment < 2 || increment > 3)
support[j][i] = 0;
if(increment == 2)
support[j][i] = input[j][i];
if(increment == 3)
support[j][i] = 1;
}
else if(option == 'v')
{
int increment = 0;
increment = input[j-1][i] +
input[j][i-1] +
input[j+1][i] +
input[j][i+1];
if(increment < 2 || increment > 3)
support[j][i] = 0;
if(increment == 2)
support[j][i] = input[j][i];
if(increment == 3)
support[j][i] = 1;
}
}
}
copy(support, input);
}
bool differentiate(int input1[52][102], int input2[52][102])
{
int increment = 0;
for(int j = 0; j < 52; j++)
{
for(int i = 0; i < 102; i++)
{
if(input1[j][i]==input2[j][i])
increment++;
}
}
if(increment == 52*102)
return true;
else
return false;
}
void print(int input[52][102])
{
for(int j = 1; j < 51; j++)
{
for(int i = 1; i < 101; i++)
{
if(input[j][i] == 1)
cout << '*';
else
cout << ' ';
}
cout << endl;
}
}
int main()
{
int generate[52][102];
int perform[52][102];
int storage[52][102];
char adjacent;
char repeat;
char cont;
bool difference;
string value;
do
{
do
{
cout << "Which adjacent would you like to use (m or v): ";
cin >> adjacent;
}while(adjacent != 'm' && adjacent != 'v');
system("clear");
int i = 0;
do
{
srand(time(NULL));
for(int j = 1; j < 51; j++)
{
for (int i = 1; i < 101; i++)
generate[j][i] = rand() % 2;
}
if(i < 10)
value = "#############";
else if(i >= 10 && i < 100)
value = "##############";
else if(i >= 100 && i < 1000)
value = "###############";
else if(i >= 1000 && i < 10000)
value = "################";
else
value = "#################";
cout << value << endl << "Generation " << i
<< ":" << endl << value << endl << endl;
if(i == 0)
copy(generate, perform);
copy(perform, storage);
print(perform);
lifegame(perform, adjacent);
i++;
if(i % 100 == 1 && i != 1)
{
cout << endl;
do
{
cout << "If you want to continue the lifegame press y for yes or n for discontinue: ";
cin >> cont;
}while(cont != 'y' && cont != 'n');
if(cont == 'n')
break;
}
difference = differentiate(perform, storage);
if(difference == false)
system("clear");
if(difference == true)
cout << endl;
}while(difference == false);
do
{
cout << "Would you like to run another simulation? (y/n): ";
cin >> repeat;
}while(repeat != 'y' && repeat != 'n');
}while(repeat == 'y');
return 0;
}

More Related Content

Similar to implemement the game.cpp by the header file given. And create main.c.pdf

i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
poblettesedanoree498
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
calderoncasto9163
 
Arduino based keyboard and display interfacing
Arduino based keyboard and display interfacingArduino based keyboard and display interfacing
Arduino based keyboard and display interfacing
Akash1900
 
Tema3_Introduction_to_CUDA_C.pdf
Tema3_Introduction_to_CUDA_C.pdfTema3_Introduction_to_CUDA_C.pdf
Tema3_Introduction_to_CUDA_C.pdf
pepe464163
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
fonecomp
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMP
Miller Lee
 

Similar to implemement the game.cpp by the header file given. And create main.c.pdf (20)

ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
Tugas 2
Tugas 2Tugas 2
Tugas 2
 
Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1Game Design and Development Workshop Day 1
Game Design and Development Workshop Day 1
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
 
Class ‘increment’
Class ‘increment’Class ‘increment’
Class ‘increment’
 
Microkernel Development
Microkernel DevelopmentMicrokernel Development
Microkernel Development
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
 
Arduino based keyboard and display interfacing
Arduino based keyboard and display interfacingArduino based keyboard and display interfacing
Arduino based keyboard and display interfacing
 
ES6 Overview
ES6 OverviewES6 Overview
ES6 Overview
 
Tema3_Introduction_to_CUDA_C.pdf
Tema3_Introduction_to_CUDA_C.pdfTema3_Introduction_to_CUDA_C.pdf
Tema3_Introduction_to_CUDA_C.pdf
 
Php 5.6
Php 5.6Php 5.6
Php 5.6
 
Node.js System: The Landing
Node.js System: The LandingNode.js System: The Landing
Node.js System: The Landing
 
Quiz using C++
Quiz using C++Quiz using C++
Quiz using C++
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
 
ES6: Features + Rails
ES6: Features + RailsES6: Features + Rails
ES6: Features + Rails
 
201913001 khairunnisa progres_harian
201913001 khairunnisa progres_harian201913001 khairunnisa progres_harian
201913001 khairunnisa progres_harian
 
Fia fabila
Fia fabilaFia fabila
Fia fabila
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
C++ L07-Struct
C++ L07-StructC++ L07-Struct
C++ L07-Struct
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMP
 

More from fazilfootsteps

Why are the mass extinctions followed by a burst of speciation in .pdf
Why are the mass extinctions followed by a burst of speciation in .pdfWhy are the mass extinctions followed by a burst of speciation in .pdf
Why are the mass extinctions followed by a burst of speciation in .pdf
fazilfootsteps
 
Why is African American history important What challenges have Afri.pdf
Why is African American history important What challenges have Afri.pdfWhy is African American history important What challenges have Afri.pdf
Why is African American history important What challenges have Afri.pdf
fazilfootsteps
 
What does it mean to say that individuals as a group are net supplier.pdf
What does it mean to say that individuals as a group are net supplier.pdfWhat does it mean to say that individuals as a group are net supplier.pdf
What does it mean to say that individuals as a group are net supplier.pdf
fazilfootsteps
 
Thoughts on this Rule by the people is the main idea behind dem.pdf
Thoughts on this Rule by the people is the main idea behind dem.pdfThoughts on this Rule by the people is the main idea behind dem.pdf
Thoughts on this Rule by the people is the main idea behind dem.pdf
fazilfootsteps
 
potential benefits of sustainability strategySolutionpotential.pdf
potential benefits of sustainability strategySolutionpotential.pdfpotential benefits of sustainability strategySolutionpotential.pdf
potential benefits of sustainability strategySolutionpotential.pdf
fazilfootsteps
 
Please write a code in JAVA to do line editor..Your program will b.pdf
Please write a code in JAVA to do line editor..Your program will b.pdfPlease write a code in JAVA to do line editor..Your program will b.pdf
Please write a code in JAVA to do line editor..Your program will b.pdf
fazilfootsteps
 
Module 01 Discussion - Dominant CultureDefine the .pdf
Module 01 Discussion - Dominant CultureDefine the .pdfModule 01 Discussion - Dominant CultureDefine the .pdf
Module 01 Discussion - Dominant CultureDefine the .pdf
fazilfootsteps
 
List and explain the states of process in a specific operating syste.pdf
List and explain the states of process in a specific operating syste.pdfList and explain the states of process in a specific operating syste.pdf
List and explain the states of process in a specific operating syste.pdf
fazilfootsteps
 
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdfIntroduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
fazilfootsteps
 
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdf
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdfIndian Institute of Management Kashipur Executive Post Graduate Pro.pdf
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdf
fazilfootsteps
 
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdfIfgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
fazilfootsteps
 

More from fazilfootsteps (20)

Why are the mass extinctions followed by a burst of speciation in .pdf
Why are the mass extinctions followed by a burst of speciation in .pdfWhy are the mass extinctions followed by a burst of speciation in .pdf
Why are the mass extinctions followed by a burst of speciation in .pdf
 
Why is African American history important What challenges have Afri.pdf
Why is African American history important What challenges have Afri.pdfWhy is African American history important What challenges have Afri.pdf
Why is African American history important What challenges have Afri.pdf
 
Using the phase diagram of polydstyrene and DOP below, would you ex.pdf
Using the phase diagram of polydstyrene and DOP below, would you ex.pdfUsing the phase diagram of polydstyrene and DOP below, would you ex.pdf
Using the phase diagram of polydstyrene and DOP below, would you ex.pdf
 
What does it mean to say that individuals as a group are net supplier.pdf
What does it mean to say that individuals as a group are net supplier.pdfWhat does it mean to say that individuals as a group are net supplier.pdf
What does it mean to say that individuals as a group are net supplier.pdf
 
True or False Statement The DNS namespace is hierarchical. Protocols.pdf
True or False Statement The DNS namespace is hierarchical. Protocols.pdfTrue or False Statement The DNS namespace is hierarchical. Protocols.pdf
True or False Statement The DNS namespace is hierarchical. Protocols.pdf
 
Thoughts on this Rule by the people is the main idea behind dem.pdf
Thoughts on this Rule by the people is the main idea behind dem.pdfThoughts on this Rule by the people is the main idea behind dem.pdf
Thoughts on this Rule by the people is the main idea behind dem.pdf
 
The symmetry properties of the plane figure formed by the four point.pdf
The symmetry properties of the plane figure formed by the four point.pdfThe symmetry properties of the plane figure formed by the four point.pdf
The symmetry properties of the plane figure formed by the four point.pdf
 
QUESTION 12 (Learning Outcome 3) Which of the following flawed uses o.pdf
QUESTION 12 (Learning Outcome 3) Which of the following flawed uses o.pdfQUESTION 12 (Learning Outcome 3) Which of the following flawed uses o.pdf
QUESTION 12 (Learning Outcome 3) Which of the following flawed uses o.pdf
 
Robbinsdale Hospital is one of two hospitals among the six facilitie.pdf
Robbinsdale Hospital is one of two hospitals among the six facilitie.pdfRobbinsdale Hospital is one of two hospitals among the six facilitie.pdf
Robbinsdale Hospital is one of two hospitals among the six facilitie.pdf
 
Research excise taxes in other states besides North Carolina. Report.pdf
Research excise taxes in other states besides North Carolina. Report.pdfResearch excise taxes in other states besides North Carolina. Report.pdf
Research excise taxes in other states besides North Carolina. Report.pdf
 
Prove that E X is not connected if and only if there exist open sets.pdf
Prove that E  X is not connected if and only if there exist open sets.pdfProve that E  X is not connected if and only if there exist open sets.pdf
Prove that E X is not connected if and only if there exist open sets.pdf
 
potential benefits of sustainability strategySolutionpotential.pdf
potential benefits of sustainability strategySolutionpotential.pdfpotential benefits of sustainability strategySolutionpotential.pdf
potential benefits of sustainability strategySolutionpotential.pdf
 
Please write a code in JAVA to do line editor..Your program will b.pdf
Please write a code in JAVA to do line editor..Your program will b.pdfPlease write a code in JAVA to do line editor..Your program will b.pdf
Please write a code in JAVA to do line editor..Your program will b.pdf
 
Nessus is a network security tool- write a pragraph describe itsto.pdf
Nessus is a network security tool- write a pragraph describe itsto.pdfNessus is a network security tool- write a pragraph describe itsto.pdf
Nessus is a network security tool- write a pragraph describe itsto.pdf
 
Module 01 Discussion - Dominant CultureDefine the .pdf
Module 01 Discussion - Dominant CultureDefine the .pdfModule 01 Discussion - Dominant CultureDefine the .pdf
Module 01 Discussion - Dominant CultureDefine the .pdf
 
List and explain the states of process in a specific operating syste.pdf
List and explain the states of process in a specific operating syste.pdfList and explain the states of process in a specific operating syste.pdf
List and explain the states of process in a specific operating syste.pdf
 
Most integer types are directly supported by hardware, but some are n.pdf
Most integer types are directly supported by hardware, but some are n.pdfMost integer types are directly supported by hardware, but some are n.pdf
Most integer types are directly supported by hardware, but some are n.pdf
 
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdfIntroduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
Introduction to Philosophy 101 HomeworkSection 3 Deductive Argum.pdf
 
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdf
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdfIndian Institute of Management Kashipur Executive Post Graduate Pro.pdf
Indian Institute of Management Kashipur Executive Post Graduate Pro.pdf
 
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdfIfgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
 

Recently uploaded

Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
Elizabeth Walsh
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 

Recently uploaded (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 

implemement the game.cpp by the header file given. And create main.c.pdf

  • 1. implemement the game.cpp by the header file given. And create main.cpp to make the program work. The game is called the game of life, https://en.wikipedia.org/wiki/Conway's_Game_of_Life game.h: #include // Provides ostream #include // String operations #include // Randomizer namespace csci2312 { using std::string; using std::ostream; using std::istream; class Cell { friend class GameOfLife; public: static const char alive ='o'; // alive image static const char dead = '-'; // dead image // Default constructor sets the cell's state to false Cell(); // Custom constructor sets the cell's state as per argument Cell(bool state); // Empty destructor ~Cell(); // Accessors have no intention to modify the object, so it is a good practice to make them 'const' functions bool getState() const; // Mutator to change cell's state void setState(bool newState); // Accessor to see the 'face' char getFace() const; private: bool state;
  • 2. char face; }; class GameOfLife { public: static const unsigned int MAX_BOARD = 30; GameOfLife(); GameOfLife(size_t boardSize); ~GameOfLife(); int seedBoard(string fileName); void seedBoard(size_t seeds); void run(); void run(unsigned int numberOfIterations); // ADVANCED // A const(!) accessor method that returns a handle to the private currentLife array. // The return type must also be 'const' because we return a pointer to a static array, and these are fixed // It is just an example. It is not needed if we have a friend operator. const Cell(*getCurrentLife() const )[MAX_BOARD+2] { return currentLife; }; /////////////////////////////////////////////////////// // friend operator can access private members of GameOfLife friend ostream& operator << (ostream& out, const GameOfLife& board); friend istream& operator >> (istream& in, const GameOfLife& board); private: bool executeRules(unsigned int countAlive, bool currentState); // With "Halo" approach we need a bigger board Cell currentLife[MAX_BOARD + 2][MAX_BOARD + 2]; Cell nextLife[MAX_BOARD + 2][MAX_BOARD + 2]; // ADVANCED // Example how to declare variable cl as a pointer/handle to our array of Cells of size HALO_BOARD
  • 3. // The accessor method getCurrentLife() above uses the same syntax for the return type const Cell(*cl)[MAX_BOARD + 2] = currentLife; //////////////////////////////////////////////////////// size_t boardSize; // Board size requested in the constructor }; // NON-MEMBER OUTPUT FUNCTIONS // Display cell's state with alive/dead face ostream& operator << (ostream& out, const Cell& cell); } Solution Answer: #include #include #include #include using namespace std; void copy(int input1[52][102], int input2[52][102]) { for(int j = 0; j < 52; j++) { for(int i = 0; i < 102; i++) input2[j][i] = input1[j][i]; } } void lifegame(int input[52][102], char option) { int support[52][102]; copy(input, support); for(int j = 1; j < 51; j++)
  • 4. { for(int i = 1; i < 101; i++) { if(option == 'm') { int increment = 0; increment = input[j-1][i] + input[j-1][i-1] + input[j][i-1] + input[j+1][i-1] + input[j+1][i] + input[j+1][i+1] + input[j][i+1] + input[j-1][i+1]; if(increment < 2 || increment > 3) support[j][i] = 0; if(increment == 2) support[j][i] = input[j][i]; if(increment == 3) support[j][i] = 1; } else if(option == 'v') { int increment = 0; increment = input[j-1][i] + input[j][i-1] + input[j+1][i] + input[j][i+1]; if(increment < 2 || increment > 3) support[j][i] = 0; if(increment == 2)
  • 5. support[j][i] = input[j][i]; if(increment == 3) support[j][i] = 1; } } } copy(support, input); } bool differentiate(int input1[52][102], int input2[52][102]) { int increment = 0; for(int j = 0; j < 52; j++) { for(int i = 0; i < 102; i++) { if(input1[j][i]==input2[j][i]) increment++; } } if(increment == 52*102) return true; else return false; } void print(int input[52][102]) { for(int j = 1; j < 51; j++) { for(int i = 1; i < 101; i++) { if(input[j][i] == 1) cout << '*'; else cout << ' ';
  • 6. } cout << endl; } } int main() { int generate[52][102]; int perform[52][102]; int storage[52][102]; char adjacent; char repeat; char cont; bool difference; string value; do { do { cout << "Which adjacent would you like to use (m or v): "; cin >> adjacent; }while(adjacent != 'm' && adjacent != 'v'); system("clear"); int i = 0; do { srand(time(NULL)); for(int j = 1; j < 51; j++) { for (int i = 1; i < 101; i++) generate[j][i] = rand() % 2; }
  • 7. if(i < 10) value = "#############"; else if(i >= 10 && i < 100) value = "##############"; else if(i >= 100 && i < 1000) value = "###############"; else if(i >= 1000 && i < 10000) value = "################"; else value = "#################"; cout << value << endl << "Generation " << i << ":" << endl << value << endl << endl; if(i == 0) copy(generate, perform); copy(perform, storage); print(perform); lifegame(perform, adjacent); i++; if(i % 100 == 1 && i != 1) { cout << endl; do { cout << "If you want to continue the lifegame press y for yes or n for discontinue: "; cin >> cont; }while(cont != 'y' && cont != 'n'); if(cont == 'n') break; } difference = differentiate(perform, storage); if(difference == false) system("clear"); if(difference == true)
  • 8. cout << endl; }while(difference == false); do { cout << "Would you like to run another simulation? (y/n): "; cin >> repeat; }while(repeat != 'y' && repeat != 'n'); }while(repeat == 'y'); return 0; }