In: Computer Science
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 .(using C++ visual studio).
#######################################
           input.txt
#######################################
4 5
1 1
1 2
1 3
2 2
-1
#######################################
            main.cpp
#######################################
#include <iostream>
#include <fstream>
using namespace std;
int r, w;
// to print the grid
void print(int **cells) {
   for (int i = 0; i < r; i++) {
       for (int j = 0; j < w; j++) {
           cout << cells[i][j] << " ";
       }
    cout << endl;
   }
}
// check if i, j is valid indices for grid
bool isValid(int i, int j) {
   if (i >= 0 && i < r && j >= 0 && j < w)
       return true;
   else
       return false;
}
// find no of live neighbors
int findActiveNeighbours(int **cells, int i, int j) {
   int activeNeighbours = 0;
   if (isValid(i - 1, j - 1) && cells[i - 1][j - 1] == 1)
       activeNeighbours++;
   if (isValid(i - 1, j) && cells[i - 1][j] == 1)
       activeNeighbours++;
   if (isValid(i - 1, j + 1) && cells[i - 1][j + 1] == 1)
       activeNeighbours++;
   if (isValid(i, j - 1) && cells[i][j - 1] == 1)
       activeNeighbours++;
   if (isValid(i, j + 1) && cells[i][j + 1] == 1)
       activeNeighbours++;
   if (isValid(i + 1, j - 1) && cells[i + 1][j - 1] == 1)
       activeNeighbours++;
   if (isValid(i + 1, j) && cells[i + 1][j] == 1)
       activeNeighbours++;
   if (isValid(i + 1, j + 1) && cells[i + 1][j + 1] == 1)
       activeNeighbours++;
   return activeNeighbours;
}
// this method simulates a round
// it changes the original array
void simulate(int **cells) {
   int newCells[r][w];
   for (int i = 0; i < r; i++) {
       for (int j = 0; j < w; j++) {
           // find no of live neighbors
           int n = findActiveNeighbours(cells, i, j);
           if (cells[i][j] == 1) { // if cell is live
               if (n == 0 || n == 1) {
                   newCells[i][j] = 0; // mark for deletion
               } else if (n >= 4) {
                   newCells[i][j] = 0; // mark for deletion
               } else {
               newCells[i][j] = cells[i][j];
               }
           } else { // if cell is dead
               if (n == 3) {
                   newCells[i][j] = 1; // mark for generation
               } else {
               newCells[i][j] = cells[i][j];
               }
           }
       }
   }
   // restoring values into original array
   for (int i = 0; i < r; i++) {
       for (int j = 0; j < w; j++) {
       cells[i][j] = newCells[i][j];
       }
   }
}
// driver function
int main()
{
    ifstream myfile;
    myfile.open ("input.txt");
    myfile >> r >> w;
    // read and populate grid
    int **life = new int*[r];
    for(int i=0; i<r; i++) {
        life[i] = new int[w];
    }
    // fill living organism
    while(true) {
        int x, y;
        myfile >> x;
        if(x == -1) {
            break;
        }
        myfile >> y;
        life[x][y] = 1;
    }
    cout << "\t\t\t Game of Life\n\t\t\t--------------" << endl;
    int round = 1;
    cout << "\t\tOriginal configuration" << endl;
    print(life);
    while(round <= 4) {
        cout << "\n\t\t\t\tRound" << round << endl;
        simulate(life);
        print(life);
        round++;
    }
}

Please upvote, as i have given the exact answer as asked in
question. Still in case of any concerns in code, let me know in
comments. Thanks!