Introduction

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:

  • An organism is born in any empty cell having exactly three neighbors.
  • An organism dies from isolation if it has fewer than two neighbors.
  • An organism dies from overcrowding if it has more than three neighbors.
  • All other organisms survive.

To illustrate, the following shows the first five generations of a particular configuration of organisms: see image.

Specific Instructions

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
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.