Programming Assignment 1 Game of Life The objective of this
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 cell’s 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:
Then read the description of the game – please note the rules of life, as this is what this assignment will attempt to implement:
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life (Links to an external site.)
And here is an interactive demonstration – click the “next” button to watch how a pattern evolves. Your implementation will also need to display a new generation of the game board, but only using a simple character based (console) type of a display
http://www.bitstorm.org/gameoflife (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 publically 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.
At the end of this part, the .h file will be fully defined, and it is expected that it will be implemented as such. Although many other implementations are possible, it is the requirement of this project to follow this model in order to meet the learning objectives.
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 (alive cells at randomly generated indexes)
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:
Hold its state: value of the ‘state’ member set to true (indicates alive) or false (dead)
Communicate its state (whether it is alive or not)
Die (cell’s state set to false, and the face set to ‘dead’)
Come alive (cell’s state set to true, and the face set to ‘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 CSCI2312
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 file ////
#pragma once
#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);
}
What does it mean by the seeding file? Can anyone help? Thanks.
Solution
Seed is a number (or a series of numbers).
Most random functions that are common on personal computers aren\'t random, but deterministic to a degree. The \'seed\' (can also be described as pick a place to start) for these psuedo-random functions are the starting point upon which future values are based. This is useful for debugging purposes: if you keep the seed the same from execution to execution you\'ll get the same numbers.
To get numbers that are more random a different seed is often used from execution to execution. This is often based on the time of the machine. It’s the reason people usually set the seed to the current time of the Computer, which is always different, so that a program will always have different random numbers.




