The game of life is hard to play,
I'm gonna lose it anyway.
The losing card I'll someday lay,
So this is all I have to say.

(Suicide is Painless Theme from MASH)

CSCI 152: Introduction to Computer Science II

Assignment II

Due Date: Monday, March 28, 2005, by e-mail

In this Assignment you will implement the “Game of Life”.

Background:

The “Game of Life” was invented by John H. Conway to simulate the birth and death of cells in a society. It really isn't a "game", but a simulation of population growth that is displayed over time.

Game Rules:

The game is played on the rectangular board. Each square at time T could be empty or could contain X that indicating the presence of the organism.

The following rules govern the birth and/or death of these organisms between two consecutive time periods. At time T:

1.Birth: An organism is born if there was none at time T-1 and exactly three of its neighbors were alive.

2.Survival: An existing organism remains alive if at time T-1 there were either two or three neighbors.

3.DeathAn organism dies from isolation if at time T-1 there were fewer than two neighbors.

4.Death: An organism dies from overcrowding if at time T-1 there were more than three neighbors.

At time T the situation on the board is called configuration.

The Game is over when there is no change in configuration between two consecutive time periods

Assignment:

Write a program that reads an input configuration into the rectangular board of 10 rows and 20 columns. The program calculates the next configurations according to the rules of the Game that were defined above. Your program will be ended if the Game is over or the program calculated 10 configurations (even if the Game is not over yet). The program has to print the configuration after each step.

Top-Down Programming:

  1. Initial Message to the User
  2. Input Data – set up the first configuration
  3. Print the input data
  4. Play the Game - Print configuration of the board after each step
  5. Print the Final Statement

Example:

Configuration 1 (input)

- / - / -
X / - / X
- / X / -
- / X / -
X / - / X

Configuration 2

- / - / -
- / X / -
X / X / X
X / X / X
- / X / -

Configuration 3

- / - / -
X / X / X
- / - / -
- / - / -
X / X / X

And so on….

Data Structures:

  1. Use the two –dimensional character array to store the configuration at time T
  2. You have to choose two different characters to denote the cell with organism and the cell without the organism. Write a specific comment statement that indicates the character for the empty cell and for the organism cell.

Hints:

  1. Since every square will be altered by its neighbors, and you must use the original values of each square for the calculations, you cannot just move through the array making changes as you go. This would give erroneous results. A second two-dimensional array needs to be made that will hold either the presence or absence of an organism that is being calculated for each square, while the original array is still intact to provide the values needed for the calculations.
  2. Each square, except for the border squares, has eight neighbors; North, South, East, West, NorthEast, SouthEast, SouthWest, and NorthWest.

List of Functions that you have to write:

  1. void initialMessage (void) – this function prints the short explanation about the Game and user’s prompt
  2. void inputBoard (char board [ROW] [COL]) – this function fills the board with initial data ( you can choose the way to do it, it could be interactive, could be random)
  3. void printBoard (char board [ROW] [COL] ) – prints the board
  4. int playGame (char board [ROW] [COL] ) – this function plays a game (until the Game is over but not more than 10 generations). The function returns an integer that indicates the amount of organisms that survived at the end of the Game or -1 if the Game was ended because there are no changes in configuration.
  5. void copyBoard (oldBoard [ROW] [COL] , newBoard [ROW] [COL] )
  6. int buildNewBoard (oldBoard [ROW] [COL] , newBoard [ROW] [COL] ) the function constructs the new board for the next generation based on the rules of the Game. The function returns 1 if it was a change in configuration and in this case you would need to use function copyBoard to copy the board and to continue. The function returns 0 if there is no change in configuration, in this case the Game is over.
  7. int neighbors (board [ROW] [COL], int x, int y) – this function counts the amount of live organisms that are neighbors of the cell (x, y)
  8. void endMessage (int result) – the function pints the final message according to the value of the parameter result. The value of the parameter result will be the return value of function playGame.

What to submit and how:

  1. You would need to send me by e-mail to the source file and input file, if you would decide to use UNIX redirection option (will be explained in lab on March 14)
  2. Please, make sure that your program is working properly before you send me your e-mail.
  3. I should get your e-mail on March 28, 2005 by 11:59 pm