CSCI 152: Introduction to Computer Science II

Spring 2007

Assignment 3

Due Date: Monday, April 9, 2007, by e-mail

Early Submission Bonus: Submit by Monday, April 2 and get 20 points bonus

Late Submission Penalty: Each delay day is -20 points

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

You are allowed to work in teams of two students.

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 1 that indicating the presence of the organism (life) and 0 the indicating the absence of the organism (death).

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.Death: An 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 5rows and 3 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 array of integers 0 and 1 to store the configuration at time T

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 (int board [ROW] [COL]) – this function fills the board with random numbers 0 and 1. Life organism is represented by 1 and dead organism is represented by 0.
  3. void printBoard (int board [ROW] [COL] ) – prints the board
  4. int playGame (int 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 (int oldBoard [ROW] [COL] , int newBoard [ROW] [COL] ) – this function copies oldBoard elements to the newBoard elements. At the end of this function both boards: oldBoard and newBoard are filled with the same values.
  6. int buildNewBoard (int oldBoard [ROW] [COL] , int 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 (int board [ROW] [COL], int i, int j) – this function counts the amount of live organisms that are neighbors of the cell (i, j)
  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 value that was returned by function playGame.

What to submit and how:

  1. You would need to send source by e-mail to
  2. If you are working in team, send only one source file and indicate the names of the team members in the comment part of the program.
  3. You don’t have to print your program.
  4. Please, make sure that your program is working properly before you send me your e-mail.
  5. I should get your e-mail on or before April 9, 2007 by 11:59 pm or earlier
  6. Each day of delay is -20 points
  7. There is no point to submit the assignment after 5 days of delay.