Problem

Game of Life The Game of Life was invented by John H. Conway to model some natural l...

Game of Life The Game of Life was invented by John H. Conway to model some natural laws for birth, death, and survival. Consider a checkerboard consisting of an n-by-n array of squares. Each square can contain one individual (denoted by 1) or be empty (denoted by -). Figure 7.78(a) shows a 6-by-6 board with four of the squares occupied. The future of each individual depends on the number of his neighbors. After each period of time, called a generation, certain individuals will survive, others will die due to either loneliness or overcrowding, and new individuals will be born. Each non-border square has eight neighboring squares. After each generation, the status of the squares changes as follows:

(a) An individual survives if there are two or three individuals in neighboring squares.

(b) An individual dies if he has more than three individuals or less than two in neighboring squares.

(c) A new individual is born into each empty square that has exactly three individuals as neighbors.

Figure 7.78(b) shows the status after one generation. Write a program to do the following:

1. Declare a two-dimensional array of size n by n, where n is input by the user, to hold the status of each square in the current generation. To specify the initial configuration, have the user input each row as a string of length n, and break the row into 1’s or dashes with the Substring method.

2. Declare a two-dimensional array of size n by n to hold the status of each square in the next generation. Compute the status for each square and produce the display in Figure 7.78(b). Note: The generation changes all at once. Only current cells are used to determine which cells will contain individuals in the next generation.

3. Assign the next-generation values to the current generation and repeat as often as desired.

4. Display the individuals in each generation. (Hint: The hardest part of the program is determining the number of neighbors a cell has. In general, you must check a 3-by-3 square around the cell in question. Exceptions must be made when the cell is on the edge of the array. Don’t forget that a cell is not a neighbor of itself.)

(Test the program with the initial configuration shown in Figure 7.79. It is known as the figure-eight configuration and repeats after eight generations.)

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 7