FOR THIS ASSIGNMENT and the next, you will be working with the Game of Life, which we have been discussing in class. The game is described in the textbook in programming project 9 on page 220, but our version of the game will use a class. The assignment uses multidimensional arrays, classes, and files.
The assignment is due on Monday, February 24
The goal of this first part of the assignment is to write an implementation file for a class, LifeBoard, that is defined in the header file LifeBoard.h. For this part of the project, you must write an implementation file that can be used with the main program, cLife.cc. In the next part of the assignment, you will work on a graphical version of Life that will use the same LifeBoard class.
You should copy the files LifeBoard.h and cLife.cc from the directory /home/cs225/life1 into your own account. You will be writing the file LifeBoard.cc. You should also copy the data files, glidergun.life and repeaters.life. These files contain sample life boards that you can use to test your program. This file should start by #including "LifeBoard.h", and it should define all the functions from the LifeBoard class. Note that these functions are commented in the .h file; you do not need any further general comments on the functions that you write (although you will probably need some comments inside the function definitions). The function are described in the comments in the .h file. You should make sure that your function definitions follow these comments. In particular, be sure to use the correct file format. In the nextGeneration function, you must implement the rules of the Game of Life. These were discussed in class and are also given in the project description on page 220 of the text.
The LifeBoard.cc file is not a complete program. To get a complete program, you must compile it together with cLife.cc, which defines the main() routine. You can do this by listing both .cc files on the g++ command line:
g++ -Wall -olife cLife.cc LifeBoard.cc
If you would like to compile LifeBoard.cc separately, to check for syntax errors, you can do this by adding the "-c" option to the g++ command:
g++ -Wall -c LifeBoard.cc
One final requirement on your project: All the functions that you write should still work without modification if the values of the constants ROWS and COLS are changes. This means that you don't ever want to use the specific numbers 75 and 25 in your code. Another good idea would be to use assert to check the preconditions of the functions that you write.
The files that I provided for you use constants to specify the numbers of rows and columns on the life board. It would be nicer if the number of rows and columns could be specified as parameters to the LifeBoard constructor. (Why?) Then the constructor might be defined as:
LifeBoard(int rows = 25, int cols = 75);
Unfortunately, it is impossible to define a two-dimensional array in C++ unless the numbers of rows and columns are given as constants. There is a solution using pointers, but it's not very pretty. The solution is to change the definition of data in the header file from "int data[ROWS+2][COLS+2]" to "int **data;". Then, data will be a pointer to an array of pointers, a kind of doubly dynamic array. We will be looking at dynamic arrays in class soon. Since the number of rows and columns can then vary from one LifeBoard to another, you should also remove the global constants ROWS and COLS and replace them with member variables in the LifeBoard class. With these changes in LifeBoard.h, you can then use the following definition for the constructor:
LifeBoard::LifeBoard(int rows, int cols) { ROWS = rows; COLS = cols; data = new (bool*)[rows+2]; for (int r = 0; r < ROWS+2; r++) { data[r] = new bool[cols+2]; for (int c = 0; c < COLS+2; c++) { data[r][c] = false; } } }
Once this is done, data can be used exactly like an ordinary two-dimensional array. That is, no other changes are necessary to LifeBoard.cc.