Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Language C++ // FILE GameOfLife2.h #pragma once #include // Provides ostream #include // String operations #include // Randomizer namespace csci2312 { using std::string; using std::ostream;

Language C++

image text in transcribed

image text in transcribed

// FILE GameOfLife2.h

#pragma once #include // Provides ostream #include // String operations #include // Randomizer

namespace csci2312 { using std::string; using std::ostream; using std::istream; // PA2: standard exception if memory allcation failed using std::bad_alloc;

// Class Cell stays the same for PA1 and PA2 class Cell { public: static const char alive ='o'; // alive image static const char dead = '-'; // dead image Cell(); Cell(bool cellState); ~Cell();

// Accessors have no intention to modify the object, so it is a good practice to make them 'const' functions bool getState() const; char getFace() const;

// Mutators void setState(bool newState); private: bool state=false; char face; };

// PA2: File IO Error custom exception class class FileIOException { // Nothing inside, just a class type to handle exception };

// PA2: Class GameOfLife will have some new and some changed features class GameOfLife { public: // PA1: maximum display board size // PA2: default display board size static const unsigned int MAX_BOARD = 30;

// PA2: New type (pointer to Cell type) handy with dynamic arrays typedef Cell* CellPtr;

// PA1: Default constructor construct a board with all dark cells of MAX_BOARD size // GameOfLife(); // PA1: Constructs the board of the requested size // GameOfLife(size_t boardSize); // PA2: Constructs the board of the requested size, can throw exception. // If parameter is omitted, it becomes the default constructor, and the board will have MAX_BOARD size GameOfLife(size_t boardSize = MAX_BOARD) throw (bad_alloc);

// PA2: Copy-constructor creates a new instance as a deep copy of the referenced object GameOfLife(const GameOfLife& game);

// PA2: destructor must release memory taken by dynamic arrays ~GameOfLife(); // Returns board size size_t getBoardSize() const;

// PA1: seeds the board from a file // int seedBoard(string fileName);

// PA2: seeds the board from a file and throws an exception if there was trouble with file void seedBoard(string fileName) throw (FileIOException);

// PA1 and PA2: seeds the board with a given number of seeds at randomized locations void seedBoard(size_t seeds);

// Executes Conway set of rules. Returns next state // Needed for TASK #4 to develop a test harness (the files test harness will be provided separately) bool executeRules(unsigned int countAlive, bool currentState); // PA1 and PA2: void run(); void run(unsigned int numberOfIterations);

// Just an example of an possible accessor which reaches to a private array. Not needed to satisfy PAs // 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. // PA1: const Cell(*getCurrentLife() const )[MAX_BOARD+2] { return currentLife; }; // PA2: const CellPtr* getCurrentLIfe() const { return currentLife; };

// PA1 and PA2: overloaded output operator to displsy the board // friend operator can access private members of GameOfLife friend ostream& operator

// PA2: overloaded input operator to input the board (from file of keyboard) friend istream& operator >> (istream& in, GameOfLife& board); private: // PA2: Encapsulate next generation calculation in a method. void calculateNextGen(CellPtr* current, CellPtr* next);

// PA1: static arrays of Cells. With "Halo" approach we need a bigger board // Cell currentLife[MAX_BOARD + 2][MAX_BOARD + 2]; // Cell nextLife[MAX_BOARD + 2][MAX_BOARD + 2];

// PA2: dynamic arrays of Cells. New type CellPtr defined (typedef Cell* CellPtr) // currentLife and and nextLife are handles to the dynamic arrays CellPtr *currentLife; CellPtr *nextLife; // Just an example how to declare variable cl as a handle to our array of Cells. Not needed to satisfy PAs // The accessor method getCurrentLife() above uses the same syntax for the return type // PA1 with static arrays: const Cell(*cl)[MAX_BOARD + 2] = currentLife; // PA2 with dynamic arrays: const CellPtr* cl = currentLife; // PA1 and PA2, keeps track of the actual board size, set in the constructor size_t boardSize;

// PA2: A handle to the array that needs to be displyed next. CellPtr *displayBoard;

}; }

----------------------------------------------------------------------------------------------------------------------------------------------------------------

// GameOfLife2_TestApp.cpp #include

#include "ErrorContext.h" #include "GameOfLife2_Tests.h"

using std::cout; using std::endl;

using namespace Testing; using namespace csci2312;

int main() {

ErrorContext ec(cout);

cout

test_cell_setget(ec);

// GameOfLife tests test_game_smoketest(ec);

test_game_rules(ec);

system("pause");

return 0; }

-------------------------------------------------------------------------------------------------------------------------------------

//ErrorContext.h

#pragma once

/** * Acknowledgement: Donnie Pinkston, CALTECH */

#include #include #include

namespace Testing {

using std::set; using std::ostream; using std::string;

class ErrorContext // displays test results { public: ErrorContext(ostream &os); // write header to stream void desc(const char *msg, int line); // write line/description void desc(string msg, int line);

void result(bool good); // write test result ~ErrorContext(); // write summary info bool ok() const; // true iff all tests passed

private: ostream &os; // output stream to use int passed; // # of tests which passed int total; // total # of tests int lastline; // line # of most recent test set badlines; // line #'s of failed tests bool skip; // skip a line before title? };

}

---------------------------------------------------------------------------------------------------------------------------

//GameOfLife2_Tests.cpp

#include #include #include #include

#include "ErrorContext.h" #include "GameOfLife2_Tests.h"

using std::cout; using std::endl; using std::setprecision;

using Testing::ErrorContext; using csci2312::GameOfLife;

#define DESC(x) desc(x, __LINE__) // ugly hack, but saves some time

///////// STUDENT TASK: Fill in implementatin for Cell tests

// Test Cell constructor and destructor void test_cell_smoketest(ErrorContext &ec) { // to do }

// Test Cell setters and getters void test_cell_setget(ErrorContext &ec) { // to do }

///////// END TASK

// Test GameOfLife constructor and destructor void test_game_smoketest(ErrorContext &ec) { bool pass; GameOfLife* myGame;

ec.DESC("--- Test - GameOfLife - Smoketest ---");

ec.DESC("Default Constructor"); pass = true; // Construct a Default GameOfLife - boardSize should be set to MAX_BOARD myGame = new GameOfLife; pass = (myGame->getBoardSize() == myGame->MAX_BOARD); ec.result(pass); ec.DESC("Destruct Defualt Test Game"); // Cleans up after previous test, destruct the object delete myGame;

ec.DESC("Custom Constructor requesting a specific boad size"); pass = true; // Construct a Custom GameOfLife - boardSize should be set to what was requested myGame = new GameOfLife(50); pass = (myGame->getBoardSize() == 50); ec.result(pass);

ec.DESC("Destruct Custom Test Game"); // Cleans up after previous test, destruct the object delete myGame; }

// Game rules test void test_game_rules(ErrorContext &ec) { bool pass; GameOfLife *myGame;

// Run at least once!! // assert(numRuns > 0);

ec.DESC("--- Test - Game Rule 110 ---"); pass = true; // Construct a Default GameOfLife - boardSize should be set to MAX_BOARD myGame = new GameOfLife; ec.DESC("--------- Alive cell with 2 alive neighbors ---"); pass = (myGame->executeRules(2, true) == true); ec.result(pass);

ec.DESC("--------- Dead cell with 2 alive neighbors ---"); pass = (myGame->executeRules(2, false) == false); ec.result(pass);

///////// STUDENT TASK: add test for the remaining rule outcomes

///////// END TASK

// Destruct the object at the end of test ec.DESC("Destruct Test Game"); // Cleans up after previous test, destruct the object delete myGame; }

Programming Assignment 2: Game of Life 2 The objective of this programming assignment is to improve several aspects of PA1 implementation, applying concepts learned so far in this course. As before, the GameofLife2-csci2312.h on file has been provided, and should be treated as project requirements. The game's existing API should remain the same only the board implementation will change, as we are replacing static arrays with dynamic arrays, and employing pointers to enhance the board display management. This time, also the matching GameofLife2-csci2312.cpp has also been provided, with placeholders for method implementation to be filled in As before, the GameofLifeApp2-csci2312.cpp (where main is) would have to be written as The PA2 package also includes several files com the test framework. Please see prising section Task #4 below. Task #0 Finish PA1 Please complete all that was missing in PAl, according to the requirements in the new .h file, and as per feedback received. Task #1 Dynamic Arrays Instead of having static arrays of Cells (currentLife and nextLife), where we had to reserve space for a maximum board, regardless of the actually requested size, we will now employ dynamic arrays and size them just as needed (as per boardsize passed into the GameofLife The only change needed will be in the class definitions and the constructors, where the array is built, but the API implementation (seed board, calculate next state, etc) will not have to change. Task #2 Exception Handling Now let us handle exceptions! Two error-prone areas come to mind: seeding a board from a file, where a file may not open successfully allocating dynamic memory, while constructing the GameofLife (more difficult) Please select at least one of the areas, and have a method throw an Exception upon encountering an error. Then invoke that method in a try/catch block and gracefully exit the program, after displaying a meaningful message

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions