Question
Programming Assignment 1: Game of Life The objective of this programming assignment is to design and implement what is known as the Game of Life,
Programming Assignment 1: Game of Life
The objective of this programming assignment is to design and implement what is known as the Game of Life, conceptualized by the British mathematician John Horton Conway in 1970 to simulate the evolution patterns in a population of living organisms.
The game board is seeded with an initial population pattern, and then evolves based on the set of rules defining when a cell dies or is born into life. A cells life cycle depends on the state of its neighboring cells.
The game has no players - a user interacts with the Game of Life by creating an initial configuration and then observes how it unfolds. Some already known initial patterns have interesting predictable properties you might discover a new one!
The implementation will only utilize the C++ programming concepts covered by pre-requisite courses. We will take an iterative approach; walk through the phases of specification, design and implementation.
Task #1 - Preparation
Please familiarize yourself with the topic by watching the following video:
https://www.youtube.com/watch?v=CgOcEZinQ2I (Links to an external site.)
Task #2 - Specification
We will discuss the game specification in class. What functionality (encapsulated in the member functions/methods) will be needed to accomplish the goal? Which of those methods will be publicly available (API) to invoke and operate the game, and which methods will be private to carry out the internal implementation details?
The specification will then be documented as part of the GameOfLife.h file the comment section of the file, including the Pre-Conditions and Post-Conditions, followed by methods prototypes.
A skeleton .h file will be provided.
Task #3 - Object Oriented Design
Object Oriented design asks to model all significant entities in a problem domain as classes, with appropriate structural relationships between them. We would then have two classes - one corresponding to the game itself (example: GameOfLife_T), and another one corresponding to a cell (example: Cell_T). The GameOfLife class will contain a private structure(s) of Cell_T. The entities will each have a set of responsibilities, reflected in the corresponding member functions/methods.
The GameOfLife_T would have to be able to:
Seed/initialize the game board with
a predefined pattern (from a file)
a randomly generated pattern
Run the game
Execute the rules for every cell in order to generate the next generation of our cell population
Display the next generation matrix
Stop the game (ask a user up front how many generations to display, or ask a user whether to continue every step of the way)
The Cell_T would have to be able to:
Know, if it is alive or dead, and communicate so
Die
Come alive
Task #4 - Implementation
There are many ways to implement the generation of the next step/generation game board, and it will be up to a student to decide. We will discuss the options in class.
The display of the board will be character based, simply accomplished with console output.
--------------------
-----*--------------
----*-*-------------
--------*-----------
Please put your game in Namespace CS
Deliverables
A zip/archive file named containing:
h header file
cxx implementation file
cxx with main() to instantiate and run the game
txt configuration file to seed the initial population pattern
**********GameOfLife.h have to use this in the program*********
#include // Provides ostream
#include // String operations
#include // Randomizer
#include
#include
class Cell
{
public:
static const char dead = '-'; //Dead cell
static const char alive = 'o'; //Alive cell
Cell();
Cell(bool state);
~Cell();
// Accessors have no intention to modify the object, so it is a good practice to make them 'const' functions
bool getState() const;
void setState(bool newState);
private:
bool state;
char face;
};
class GameOfLife
{
public:
static const unsigned int MAX_BOARD = 30;
GameOfLife();
GameOfLife(size_t boardSize);
~GameOfLife();
void displayBoard();
int seedBoard(std::string fileName);
void seedBoard(size_t seeds);
void run();
void run(unsigned int numberOfIterations);
// 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] { return currentLife; };
// friend operator can access private members of GameOfLife
friend std::ostream& operator << (std::ostream& out, const GameOfLife& board);
private:
Cell currentLife[MAX_BOARD][MAX_BOARD];
Cell nextLife[MAX_BOARD][MAX_BOARD];
// Example how to declare variable c as a pointer/handle to our array of Cells of size MAX_BOARD
// The accessor method getCurrentLife() above uses the same syntax for the return type
const Cell (*c)[MAX_BOARD] = currentLife;
size_t boardSize; // Board size requested in the constructor
};
// NON-MEMBER OUTPUT FUNCTIONS
// Display cell's state with alive/dead face
std::ostream& operator << (std::ostream& out, const Cell& cell);
// Display current board. Implementation needs to be moved to the .cpp file, only here temporarily to demo
std::ostream& operator << (std::ostream& out, const GameOfLife& board)
{
// Example of how to invoke the accessor method getCurrentLife() and access an element of the board
// If we have an accessor, then the operator does not have to be a friend
out << board.getCurrentLife()[0][0];
// If the operator is a friend, then any private data member (like currentLife) can be accessed directly.
out << board.currentLife[0][0];
// Example how to access a cell's method.
// The board comes in to this operator function with a 'const' qualifier in front of the board, which means that
// the function cannot alter the board. Therefore only 'const' functions are allowed to operate on the baord.
// So getState() accessor must be a 'const' function - please see its declaration above
bool a = board.currentLife[1][2].getState();
return out;
}
Step 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