The Game of Life, invented by the mathematician John H. Conway, is intended to model life in a society of organisms. Consider a rectangular array of cells, each of which may contain an organism. If the array is viewed as extending indefinitely in both directions, then each cell has eight neighbors, the eight cells surrounding it. In each generation, births and deaths occur according to the following rules:
To illustrate, the following shows the first five generations of a particular configuration of organisms: see image.
Write a modular program to simulate the Game of Life and investigate the patterns produced by various initial configurations. Some configurations die off rather rapidly; others repeat after a certain number of generations; others change shape and size and may move across the array, and still others may produce 'gliders that detach themselves from the society and sail off into space!
Since the game requires an array of cells that continually expands/shrinks, you would want to use multi-dimensional arrays and dynamic memory allocation. Your program should consist of at least two modules. One module will implement a class to represent Game of Life. Following good object-oriented programming practices, keep the data members private. Accessor functions should be declared to set and get the private data if needed. A Game of Life object should be able to initialize itself using input from a file, evolve through the different generations and display each generation! Write a test program that illustrates the use of your Game of life class and representative output (at least 5 generations) for every dataset in the input file that will be provided.
Additionally, the output should be displayed in format that is easy to understand and you have to make use of at least one of the functionalities from the header file < iomanip.h>. Overloading of input and output operators is required. Include all the source code (well documented) and input/output files/screenshots.
Please note that the object creation should not be inside the big loop of the main program.
Sample Input file:
4 5 // indicates number of rows and columns
1 1 // list of all the positions of organisms
1 2
1 3
2 2
-1 //end of current dataset, there can be more datasets in a larger input file
Corresponding Output file:
Generation 1:
#####
#xxx#
##x##
#####
Generation 2:
#####
##x##
#xxx#
#xxx#
#####
Generation 3:
#####
#xxx#
#####
#x#x#
##x##
#####
Generation 4:
#####
##x##
##x##
#x#x#
##x##
##x##
#####
Generation 5:
#####
#xxx#
#x#x#
#xxx#
#####
Initial code:
Main.cpp
#include < iostream>
#include < fstream>
#include < string>
using namespace std;
int main()
{
Matrix ml;
ifstream inputInFile("input.txt");
inputInFile >> ml;
for (int i = 0; i < 5; i++)
{
cout < < ml;
ml.nextgen(); //processing to next gen
}
}
Game.h
#ifndef MATRIX H
#define MATRIX_H
#include < fstream>
using namespace std;
class Matrix {
private:
int c_size, r_size;
int ** p;
public:
Matrix(int c, int r); //CONSTRUCTOR
Matrix(const Matrix&); //
~Matrix();
int & operator(int i, int j) { return p[i][j]; }
Matrix operator = (const Matrix & m);
Matrix operator += (Matrix &m);
friend ostream& operator < < (ostream & os, const Matrix & m);
friend istream& operator>>(istream & is, Matrix & m);
void nextgen();
};
#endif