We have worked with one dimensional arrays, but arrays can have more than one dimension. One application for two dimensional arrays is in games. Specifically, we are going to apply it to the "Game of Life" - a game invented by British mathematician John Conway. See a complete description of the Game of Life in the Big Java text, problem P7.10. There is also a Wikipedia article about the Game of Life.
The Game of Life consists of a grid of cells which can either be "alive" (black) or "dead" (light gray). Each cell in the grid is referenced by its row index and column index on the grid. For instance, cells[i][j] is the cell at row i and column j. The grid starts at a specific state, where each cell in the grid is either alive or dead. In each turn of the game, each cell looks at all of its neighboring cells. The concept of neighbor is defined as follows. For most cells, cells[i][j] has 8 neighbors, defined by the following table:
Note that cells in the top row, the leftmost column, the rightmost column, or the bottom row don't have as many neighbors. For instance, the neighbors of a cell in the middle of the top row have 5 neighbors, defined by the following table:
Cells in the corners have only three neighbors. For instance, the cells is in the upper left corner of the grid, and has the following neighbors:
In each turn of the game of life, whether a specific cell in the grid lives or dies depends on the following three rules:
In all other situations, the cell stays alive if it was alive, or stays dead if it was dead.
John Conway came up with some results using the Game of Life. There are several initial conditions which result in surprising animated behaviors. By the end of the lab, you should be able to observe some of these behaviors.
Download the lab08.zip file and use it to create a lab08 package in Eclipse. For instructions on how to import a zip file into a package, see the instructions in Using Eclipse web page.
The lab08.zip file has the basic infrastructure for the Game of Life game, including a Graphical User Interface. This infrastructure models the Game of Life with a two-dimensional array of boolean values which are "true" if the cell is alive, and "false" if the cell is dead. This array is the cells field in the CellGrid class.
However, there is one vital missing piece... the code that calculates the next state of the game, given the previous state. That code is in file CellGrid.java, method applyUpdate. Your assignment is to write the code for applyUpdate.
The applyUpdate method needs to do the following:
Once you have coded the applyUpdate method, run the main method in the GraphDialogSetup class. When you run this code, it will create two new windows... a big window that represents the grid, and a "control" window. (On my display, the control window is at the bottom, right hand side of the screen). On the control window, push the Setup button to choose an initial state. Then, press the Step button to advance one turn. Press the Step button several times. If you coded your applyUpdate method correctly, you should see some activity on the screen. If you press the Pause/Run button, it will automatically step the game, and you can see animations on the screen.
There are three .txt files in the package. The brain.txt and ltwgtspaceship.txt files contain the java code for some other initial conditions that are interesting in the Game of Life. You can figure out how to add these to the CellGrid class, but you will also need to read addingbutton2selectonlist.txt to figure out how to add a button to the GUI to enable these initial conditions. You can also write your own methods to create initial conditions and add these to the GUI as well.
This lab is worth 10 points. You will get the full 10 points if your code compiles without warnings, and produces the correct output. The following are reasons for deductions:
If you do the extra credit, you will get up to 3 extra points added to your grade, as long as your grade does not exceed 10 points.