Question

Write a modular program using visual c++ to simulate the Game of Life and investigate the...

Write a modular program using visual c++ 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 . 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: # # # # # # x x x # # # x # # # # # # # Generation 2: # # # # # # # x # # # x x x # # x x x # # # # # # Generation 3: # # # # # # x x x # # # # # # # x # x # # # x # # # # # # # Generation 4: # # # # # # # x # # # # x # # # x # x # # # x # # # # x # # # # # # # Generation 5: # # # # # # x x x # # x # x # # x x x # # # # # #

0 0
Add a comment Improve this question Transcribed image text
Answer #1

main.cpp

#include <iostream>
#include <fstream>

using namespace std;

int r, w;

void print(int **cells) {

for (int i = 0; i < r; i++) {
for (int j = 0; j < w; j++) {
cout << cells[i][j] << " ";
}
cout << endl;
}
}

bool isValid(int i, int j) {
if (i >= 0 && i < r && j >= 0 && j < w)
return true;
else
return false;
}

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;
}

int newCells[r][w];

for (int i = 0; i < r; i++) {
for (int j = 0; j < w; j++) {

int n = findActiveNeighbours(cells, i, j);

if (cells[i][j] == 1) {
if (n == 0 || n == 1) {
newCells[i][j] = 0;
} else if (n >= 4) {
newCells[i][j] = 0;
} else {
newCells[i][j] = cells[i][j];
}
} else {
if (n == 3) {
newCells[i][j] = 1;
} else {
newCells[i][j] = cells[i][j];
}
}
}
}

for (int i = 0; i < r; i++) {
for (int j = 0; j < w; j++) {
cells[i][j] = newCells[i][j];
}
}
}

int main()
{
ifstream myfile;
myfile.open ("input.txt");
myfile >> r >> w;

int **life = new int*[r];
for(int i=0; i<r; i++) {
life[i] = new int[w];
}

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++;
}
}

Input.txt:

4 5
1 1
1 2
1 3
2 2
-1

Sample Output:

Add a comment
Know the answer?
Add Answer to:
Write a modular program using visual c++ to simulate the Game of Life and investigate the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques...

    Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques we have been using in class are allowed. Nothing else. The program should read the initial state of the board by reading in "alive" cells from a user input data file. Meaning your program should ask the user the name of the data file. Assume all the other cells are "dead." Make sure to use modular coding techniques. The main program should be pretty...

  • JAVA. Threading the Game of Life Write a Java program to implement the Game of Life,...

    JAVA. Threading the Game of Life Write a Java program to implement the Game of Life, as defined by John Conway: Any live cell with fewer than two live neighbors dies, as if caused by under-population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by...

  • This is for C++ The mathematician John Horton Conway invented the “Game of Life.” Though not...

    This is for C++ The mathematician John Horton Conway invented the “Game of Life.” Though not a “game” in any traditional sense, it provides interesting behavior that is specified with only a few rules. This project asks you to write a program that allows you to specify an initial configuration. The program follows the rules of Life (listed shortly) to show the continuing behavior of the configuration. LIFE is an organism that lives in a discrete, two-dimensional world. While this...

  • Using c# visual studio Game of Life The Game of Life program assignment follows the material...

    Using c# visual studio Game of Life The Game of Life program assignment follows the material from Chapters 7 and 8 in your book. There are 2 major focus points for this project: 2-dimensional arrays - This is our first data structure that moves beyond a simple linear list of values. We will expand this and also look at some of the built-in collections in the C# FCL. nested for loops (2 loops, an inner loop and an outer loop)...

  • using c++ program the following The Assignment For this assignment, we are asking you to implement...

    using c++ program the following The Assignment For this assignment, we are asking you to implement Conway's Game of Life using a two-dimensional array as a grid in which to store the cells. Live cells are denoted as * characters, dead cells are denoted by the character. Input As input, you will be given a line containing the number of rows, number of columns, and number of generations to simulate in the form mng followed by m lines containing the...

  • Write a program that consists of a 10 by 10 game board, with 10 generations. Project...

    Write a program that consists of a 10 by 10 game board, with 10 generations. Project The Game of Life The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the neighbors of this cell. Each game state is called "generation". The game progresses from one generation to the next according to the following rules: A creature that has...

  • Write a program (C++) that shows Game sales. The program should use a structure to store...

    Write a program (C++) that shows Game sales. The program should use a structure to store the following data about the Game sale: Game company Type of Game (Action, Adventure, Sports etc.) Year of Sale Sale Price The program should use an array of at least 3 structures (3 variables of the same structure). It should let the user enter data into the array, change the contents of any element and display the data stored in the array. The program...

  • Please use C++, thank you! The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the...

    Please use C++, thank you! The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the neighbors of this cell Each game state is called "generation". The game progresses from one generation to the next according to the following rules: A creature that has more than 3 neighbors- dies of crowding. Its cell will be empty in the next...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT