CMPT 102 Lab #9

November 27-30 2006

This lab is designed to give further practice with arrays

Lab Problem 1: Problem 3, p397 of you textbook

The Game of Life, invented by John H. Conway, is supposed to model the genetic laws for birth, survival, and death. We will play the game on a board that consists of 25 squares in the horizontal direction and 25 squares in the vertical direction.  Each square can be empty or can contain an X indicating the presence of an organism. Each square (except for border squares) has eight neighbors. The next generation of organisms is determined according to the following criteria.

a)     Birth – an organism will be born in each empty location that has exactly three neighbors

b)     Death – and organism with four or more organisms as neighbors will die from overcrowding. An organism with less than two neighbors will die from loneliness

c)     Survival – an organism with two or three neighbors will survive to the next generation.

d)     Assume that the borders (where squares have fewer than 8 neighbors) are infertile regions where organisms can neither survive nor be born. You will not have to process the border squares.

The initial configuration of organisms is your input data. From the input data calculate the next generation. You will want one array for the present generation and one for the next generation. Once you have determined the next generation you will want to copy it into the array for the present generation.

Write a function to determine the next generation of organisms and their locations.  The prototype for this function should be

void  nextGen(char lifeBoard [25][25]);

Within the function a second array, nextGenBoard,  will be needed. Based on the locations of the organisms in the array lifeBoard, determine the locations of organisms for the next generation and place those organisms into the array nextGenBoard. One the entire next generation is determined copy the contents of nextGenBoard into the array lifeBoard and return the next generation.

Your main program should first read, from file inputfile, the number of generations to be calculated and then the initial configuration. The initial configuration should be 25 rows containing 25 Xs and .s (periods).  Place the .s or Xs into each element of the 2-D array representing the 25x25 life game board. Your program should then print out the initial configuration, When you print out the array print an X for each array location containing an organism, and a . for any location not containing an organism. Print one row of the array per line. After printing the initial configuration call your function to calculate the locations of the organisms for the next generation. Print three blank lines then print the array for the next generation. Continue until you have printed the results for each generation requested.   A sample data file inputfile is provided.