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 M squares in the horizontal direction and N squares in the vertical direction (0<M<50, 0<N<50). 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
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. Thus, border squares will always be empty.
The inputs for your problem are as follows
· The names of the input data file and output file are supplied by the user as keyboard input.
· The initial configuration of organisms is provided in an input data file.
o The first line of the input data file contains five integers. These five integers represent
§ The number of rows in the game board
§ The number of columns in the game board
§ The number of generations to be calculated (the initial board is generation 0, the first new generation calculated is generation 1)
§ The increment between generations printed to the output file (If this increment is 3, generations 1, 4, 7, 10… 3*increment+1 are printed to the output file)
§ The increment between generations printed to the screen
o The remainder of the game file contains an image of the game board (see sample input file). This image may look like a game board or may contain a continuous list of single characters separated by white space (blank, tab, or newline)
o The last character in the input file should be a newline (If you move to the end of your input file the cursor should be at the beginning of the line following the last piece of data)
Your main program should do each of the following
1. Request the name of the input file
2. Open the input file and check that it was opened correctly
· If it did not open correctly print an error message and terminate the program
3. Request the name of the output file
4. Open the output file and check that it was opened correctly
· If it did not open correctly print an error message and terminate the program
5. Read the dimensions of the game board from the input file
6. Read the number of generations to be calculated from the input file
7. Read the printing increment for output file and for the screen
8. Read the initial configuration of the game board from the input file. Place the .s or Xs, one character into each element of the 2-D array representing the life game board.
9. Use the PrintGen function to print the title and the initial configuration of the game board into the output file and to the screen
10. For each calculated generation requested your main program should:
a. Use the NextGen function to calculate the locations of the organisms for the next generation.
b. Print the results for the next generation (the one you just calculated using NextGen) into the output file using the PrintGen Function.
c. Print the results for the next generation to the screen using the PrintGen Function.
11. Finally your program should print the title and Life game board for the final generation calculated to both the screen and the output file
Your PrintGen function should
1. Have the following prototype
void PrintGen(char lifeBoard[ ][MAX_ARRAY_SIZE], ostream& outStream, int numRowsInBoard, int numColsInBoard, int generationNum, int increment);
2. Determine if this iteration is the initial board (generationNum==0)
a. If it is print (both to the screen and to the output file)
i. The header
LIFE initial game board
ii. Print one blank line
iii. Print the game board.
iv. Print three blank lines
3. Determine if the game board for this iteration is to be printed to the screen
4. step removed
5. If the game board for this iteration is NOT to be printed to the screen
a. Print the message
LIFE game board generation 44 has been determined
to the screen. The number will indicate the generation number of the generation being printed
6. step removed
7. step removed
8. If the game board for this iteration (generationNum != 0) is to be printed to the output file
a. Print a header to label the game board into your output file. The header should look like
i. LIFE game board generation 123
ii. The number will indicate which generation’s game board is illustrated below the title. The example title would be for generation 123
b. Print the game board into the output file. 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 game board per line. You should print one space between each successive X or .l (this will enable any screen in the output file to be used later as part of an input file)
c. Print three blank lines to the screen
Your NexttGen function should
9. Have the following prototype
void NextGen (char lifeBoard [][MAX_ARRAY_SIZE], int numRowsInBoard, int numColsInBoard);
Within the function, NextGen, 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. Once the entire next generation is determined copy the contents of nextGenBoard into the array lifeBoard and then return the next generation to the calling program.
Remember (or anticipate what we will discuss) when passing an array into a function we can change the contents of the array within the function and expect those changes to be reflected in the values of the array in the main program. Do remember that you cannot change the contents of a variable that is not an array within your function and expect the changes to be reflected in the main program.
After you have developed and tested your program electronically submit the life.cpp file containing the main program and both functions. Your hard copy should include a copy of the code in the life.cpp file and a test plan,
the sample input file is as follows
18 40 5 5 8
........................................
.........................XXX.......X....
..........................X........X....
.........................XXX.......X....
...............XXX.................X....
................X..................X....
...............X.X......................
...............XXX......................
........................................
........X................XX.............
........X................XX.............
........X................XX.............
........XX...............XX.............
........XX..............................
........X...............XX..............
........X...............XX..............
........X...............XX..............
........................................
Thanks
Sibghat
This post has been edited by sibghat: 28 October 2007 - 11:51 AM

New Topic/Question



MultiQuote



|