Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Skills needed to complete this assignment: functions, dynamic arrays. The mathematician John Horton Conway invented the Game of Life. Though not a game in any

Skills needed to complete this assignment: functions, dynamic arrays.

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 to show the continuing behavior of the configuration.

LIFE is an organism that lives in a discrete, two-dimensional world. This world is an array with each cell capable of holding one LIFE cell. Generations mark the passing of time. Each generation brings births and deaths to the LIFE community. The births and deaths follow the following set of rules.

  • We define each cell to have eight neighbor cells. The neighbors of a cell are the cells directly above, below, to the right, to the left, diagonally above to the right and left, and diagonally below to the right and left. Be careful when checking for neighbors on the edges.

  • If an occupied cell has zero or one neighbors, it dies of loneliness. If an occupied cell has more than three neighbors, it dies of overcrowding.

  • If an empty cell has exactly three occupied neighbor cells, there is a birth of a new cell to replace the empty cell.

  • Births and deaths are instantaneous and occur at the changes of generation. A cell dying for whatever reason may help cause birth, but a newborn cell cannot resurrect a cell that is dying, nor will a cells death prevent the death of another, say, by reducing the local population.

Your job is to complete a program that simulates this world. The program will first ask the user for the world's dimensions (rows and columns), the number of initial alive cells, and the initial alive cells' coordinates. After that, the program will display the initial world with spaces for dead cells, and asterisks for alive cells. The user can type in 'G' for the next generation, or type 'Q' to

quit. Three functions prototypes are provided in the template:

void initialization(bool **world, int nrows, int ncols)

This function takes a 2-dimensional Boolean array we call world, and the size of this array in rows and columns. It prompts the user input for the number of initial alive cells and their coordinates, and sets the initial world according to the inputs.

void generation(bool **world, bool **copy, int nrows, int ncols)

This function takes the world array, a copy of the world array (it can be used to help count neighbors while cells are turned on or off in the original vector), and the size of the

Page 1 of 3

world. The function scans the array and modifies the cells, marks the cells with births and deaths in accord with the rules listed earlier. This involves examining each cell in turn, either killing the cell, letting it live, or, if the cell is empty, deciding whether a cell should be born.

void display(bool **world, int nrows, int ncols)

This function accepts the array world and displays the array on the screen. For better visualization, we also display a border of the world.

You are free to add more functions to this program. Please do NOT:

  1. modify the main function except at the places commented with /* your code here */ (for allocating/deallocating dynamic memory).

  2. modify the prototypes of the above three functions.

  3. use C/C++ libraries other than iostream and cstdlib.

....

....

#include #include

using namespace std;

const char ALIVE = '*'; const char DEAD = ' ';

// ATTENTION: comments in this code are hints, they don't follow // the format discussed in lectures i.e. making the code understandable // you should delete them and write your own comments before submitting your code

void initialization(bool **world, int nrows, int ncols); // prompts and reads the alive cells to initialize the world // initializes the world

void generation(bool **world, bool **copy, int nrows, int ncols); // input parameters: original world, an array to make a copy, dimensions of the array // updates the world

void display(bool **world, int nrows, int ncols); // prints the world to the console

// you are free to define more functions

int main(){

// Variable declarations. You can add more if necessary bool **world, **copy; int nrows, ncols; char next;

cout << "Enter world dimensions (rows and columns): "; cin >> nrows >> ncols;

// allocate memory for dynamic 2d-arrays 'world' and 'copy' /* your code here */

// initialize the world and display initialization(world, nrows, ncols); display(world, nrows, ncols);

// prompt user input, Generation/Quit while (true){ cout << "next Generation or Quit (g/q): "; cin >> next; if (next=='g' || next=='G' || next=='q' || next=='Q') break; }

while (next=='g' || next=='G'){ // update the world and display generation(world, copy, nrows, ncols); display(world, nrows, ncols);

// prompt user input while (true){ cout << "next Generation or Quit (g/q): "; cin >> next; if (next=='g' || next=='G' || next=='q' || next=='Q') break; } }

// deallocate memory for dynamic 2d-arrays 'world' and 'copy' /* your code here */

return 0; }

void generation(bool **world, bool **copy, int nrows, int ncols){ /* your code here */ }

void initialization(bool **world, int nrows, int ncols){ /* your code here */ }

void display(bool **world, int nrows, int ncols){ /* your code here */ }

// More function definitions /* your code here */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions