Copy the lab materials to your account from /home/student/Classes/Cs241/Labs/Lab09
In this lab you will complete an implementation of the Game of Life. To keep the game manageable, we will use 40 rows and 60 columns on the board.
Go to a terminal window and
type cd
to go
into the Lab09
directory.
Once there, type the command:
more life1.datThis command lists the contents of the life1.dat data file to the screen. The format of this data file lists just the row and column values of the live cells. Each line contains an (row, column) ordered pair.
What makes this game interesting is that different input files will exhibit very different behavior. That behavior depends on the file's initial pattern of live cells:
Start running BlueJ and open the Lab09 project. There are two classes listed, GameOfLife and GraphicBoard. Most of your work will be done inside GameofLife. GraphicBoard will help us get a more visual display of the data when we reach the last part of the lab exercise.
Edit the GameOfLife class. Look over the code. What kind of actions does it perform? What kind of variables are declared inside main()? Which other methods are included?
In Chapter 8 we learn about class variables and constants. The two integer and two character constants at the beginning of the class are declared at the "class level". This means they are available to all methods in the class without passing their values through parameters.
Just after these declarations you'll see a partially-completed method named makeBoard(). The job of makeBoard() is to create an empty board of characters and return it. The method already contains code that declares and instantiates a 2-dimensional board of characters of the appropriate size.
Add these parts to the method to complete it:
Find the readBoardFromFile() method. It is also stubbed in (partially written). Make these changes so it will work with the data files:
To test this code:
1 Show us your code. Be ready to answer the following questions:
To test this method,
10,16 neighbors: 2Try other row and column values of your own choosing. Look at what displayASCIIBoard() gives you to identify locations of other interesting cells to test.
0,0 neighbors: 0
39,59 neighbors: 0
10,17 neighbors: 3
8,17 neighbors: 1
Be ready to answer the following questions:
The first rule is implemented
in the stubbed method.
Here is how to run the main() method:
3 Show us your corrected code.
Be ready to answer the following questions:
Back in GameOfLife, uncomment the lines in main() that are designated for checkpoint #4. Run the main() method and you will see that the graphics window appears with a box drawn in it.
Open the editor window for GraphicBoard. Find the method named drawBoundingBox(). You will see that the method adds 4 GLines to the window. A GLine is simply a line that extends from (x,y) point to another. This is the code which ran and drew the 4 lines in the window.
The displayBoard() method draws circles using GOvals. A GOval is an oval, with location, height and width. An oval with equal height and width is a circle. As seen in other labs, we had to translate our program's coordinates to pixels. To review:
x = 50 + 10 * colLook for those lines inside of the already-written code. The two 5's in the same line make the circle have both height and width of 5 pixels.
y = 50 + 10 * row
You and your partner should now complete the displayBoard() method. Your job is to enclose the lines which draw the circles inside a nested-for loop. Here is your algorithm:
For each row and column, if the board at that position is ALIVE, draw a circle there. Otherwise, do nothing.
Now go back to the GameOfLife class and revise the main() method one more time. Look for the notes to replace the call to displayASCIIBoard() with a call to displayBoard().
Test displayBoard() by running the main() method. Once it is working, modify the loop in the main() method so that displays 500 generations of the board. Run it on different life__.dat input files and see how they look!
4
Show us your running code. Be prepared to answer the following
question:
Translating from program coordinates to pixel coordinates is a real
pain. How could you revise your program to make this easier?
For this checkpoint you will develop your own data files. Go to a terminal window and cd to the Lab09 directory. Enter the command:
cp life5.dat mylife.datThis copies life5.dat to a new file named mylife.dat.
BlueJ does not allow you to edit non-Java files, so we will have to use a different editor. You can reach a simple text editor named gedit from your main Gnome Applications menu. From the upper left corner of your monitor screen, choose Applications-Accessories-Text Editor to begin running gedit. Navigate to your Lab09 directory and open the mylife.dat file. The editing commands work very similarly to those of the BlueJ editor, so take a moment to try some simple edits. You can move around the file using either the arrow keys or the mouse. You save the file by choosing File-Save (or Ctrl-S).
Edit mylife.dat to add some extra filled positions. Try to draw a particular shape, such as a pinwheel or arrow, somewhere else within the grid. Test your changes by running the main() method.
Now try these other files and be ready to describe how their appearances change as the simulation runs:
5 Show us your modified datafile and describe the running animations.
Don't forget to exit Firefox
before you log out.
6
Show us that you have logged out, cleaned up, turned off your monitor
and pushed in your chairs for this last checkpoint.