Question

In: Computer Science

Write a modular program to simulate the Game of Life and investigate the patterns produced by...

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).

Solutions

Expert Solution

#######################################
           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!


Related Solutions

Overview For this assignment, write a program that uses functions to simulate a game of Craps....
Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the...
For this assignment, write a program that uses functions to simulate a game of Craps. Craps...
For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7 or 11, the player...
Write a program in Python to simulate a Craps game: 1. When you bet on the...
Write a program in Python to simulate a Craps game: 1. When you bet on the Pass Line, you win (double your money) if the FIRST roll (a pair of dice) is a 7 or 11, you lose if it is ”craps” (2, 3, or 12). Otherwise, if it is x ∈ {4, 5, 6, 8, 9, 10}, then your point x is established, and you win when that number is rolled again before ’7’ comes up. The game is...
Write a MATLAB program to simulate the Stuck in the Mud game, The following link contains...
Write a MATLAB program to simulate the Stuck in the Mud game, The following link contains the detail game description: https://www.activityvillage.co.uk/stuck-in-the-mud , with additional features that can: • Use five (5) 6-sided dice to automatically play the Stuck in the Mud game against a player. • Greet the player when the game starts. • Let the player to choose the number of rounds to play. Take care of the user input to ensure the program will not crash with inputs...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
Write a program in C that implements Conway's Game of Life. You will be expected to...
Write a program in C that implements Conway's Game of Life. You will be expected to apply the principles of Design by Contract to your code. The rules for the Game of Life are simple. The universe consists of a two-dimensional matrix of cells with each cell being alive or dead. For each generation every cell determines its next phase of life as follows: If the cell is alive: it dies if it has 0, 1, 4 or more living...
Please write in python Use modular design to write a program that asks the user to...
Please write in python Use modular design to write a program that asks the user to enter his or her weight and the name of a planet. The program then outputs how much the user would weigh on that planet. The following table gives the factor by which the weight must be multiplied for each planet. PLANET CONVERSION FACTOR Mercury 0.4155 Venus 0.8975 Earth 1.0000 Moon 0.1660 Mars 0.3507 Jupiter 2.5374 Saturn 1.0677 Uranus 0.8947 Neptune 1.1794 Pluto 0.0899 The...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT