CS-140 Lab 8 -- The Game of Life

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:

cells[i-1][j-1]cells[i-1][j] cells[i-1][j+1]
cells[i][j-1] cells[i][j] cells[i][j+1]
cells[i+1][j-1] cells[i+1][j] cells[i+1][j+1]

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[0][j-1] cells[0][j] cells[0][j+1]
cells[1][j-1] cells[1][j] cells[1][j+1]

Cells in the corners have only three neighbors. For instance, the cells[0][0] is in the upper left corner of the grid, and has the following neighbors:

cells[0][0] cells[0][1]
cells[1][0] cells[1][1]

In each turn of the game of life, whether a specific cell in the grid lives or dies depends on the following three rules:

  1. If the cell is dead, but has exactly three neighbors who are alive, it comes to life (birth)
  2. If the cell is alive, but has more than three live neighbors, it dies (death from starvation)
  3. If the cell is alive, but has less than two live neighbors, it dies (death from loneliness)

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.

Getting Started

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:

  1. Create a 2-dimensional int neighbors array that is the same size as the cells array.
  2. For each cell, calculate the number of neighbors of that cell which are true. This can be accomplished with nested for loops, where the outside for loop loops through the rows, and the inside for loop loops through the columns. If you use i as the row index, and j as the column index, then you should calculate neighbors[i][j] inside the inner loop. Warning - watch out for border cases. If you try to look at something like cells[-1][4] when you are working on row zero, then you will get an array bounds error, which causes an exception in Java.
  3. After you have calculated the neighbors array, you need to update the cells array, based on the rules of the game outlined above. You will need another pair of nested for loops to determine the truth value of every cell in the cells array to do this.

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.

Extra Credit

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.

Grading Criteria

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.