Game of Life

The "Game of Life" (not to be confused with the Parker Brothers edition) is a well-known mathematical simulation that can mimic some very complex behavior. The games tries to mimic the life cycle of cells.

The rules for the game are really very simple:

Neighbors are defined as any adjacent square left, right, up, down or diagonally. The diagram below depicts all the neighbors of a center cell.

+---+---+---+

| 1 | 2 | 3 |

+---+---+---+

| 4 | 5 | 6 | So a cell at 5, would need to check its neighbors at 1,2,3,4,6,7,8,9.

+---+---+---+

| 7 | 8 | 9 |

+---+---+---+

If a cell is at a border, then treat all locations off-board as empty.

Here are a few sample illustrations to help demonstrate the rules. A C means the square is occupied by a cell.

+---+---+---+

| C |   |   |

+---+---+---+

|   | C |   | This center cell would die (become an empty square) in the next generation

+---+---+---+ since it only has 1 neighbor.

|   |   |   |

+---+---+---+

 

+---+---+---+

|   |   |   |

+---+---+---+

| C | C | C |  This center cell would die (become an empty square) in the next generation

+---+---+---+  since it has 4 neighbors.

| C |   | C |

+---+---+---+

 

+---+---+---+

| C |   |   |

+---+---+---+

|   |   |   | This center cell would be born (become an occupied square) in the next generation

+---+---+---+ since it has exactly 3 neighbors.

| C |   | C |

+---+---+---+

 

Program Requirements

Your assignment is to program the Game of Life subject to the rules described above. Your program should run in two modes: single step and animate. In single step mode, the game advances one turn per button press. In animate mode the game advances each turn automatically based upon the timer delay.

User Interface

Your program should run as a GUI application in a JFrame. You should have a JPanel-derived class containing 4 buttons, a JLabel, a text entry field and a drawing area.

The buttons consist of:

You should display the current generation number (turn number) in a JLabel.

You should allow the user to enter a delay to use for the animate mode. The default delay if no other entry has been made is 100ms.

In the drawing area, you should display the grid representing the squares that hold the cells. The default grid size should be 20 x 20. (For extra credit, you may allow user specified grid sizes to be entered.)

When starting the game, the user should be allowed to click with the mouse inside each cell to make that cell occupied or empty. Clicking on an empty cell makes it occupied. Clicking on an occupied cell makes it empty. Once the user presses the Step or Run button, mouse clicks in the grid area are ignored until the clear button is pressed.

When a square is occupied it should be filled in with the color of your choice. All the squares should be the same color. (For extra credit, newly born cells in each generation can be colored with a different color from the older cells in a generation).

Deliverables

Extra Credit Opportunities

Some special start cell sequences

Here are some interesting starting sequences. You can also use these to test your application. The glider. The glider is cool because it repeats itself every few generations and seems to move across the screen. It will move down and right every 4 generations.

Generation 0 (Start)

+---+---+---+---+---+

|   | C |   |   |   |

+---+---+---+---+---+

|   |   | C |   |   |

+---+---+---+---+---+

| C | C | C |   |   |

+---+---+---+---+---+

|   |   |   |   |   |

+---+---+---+---+---+

Generation 1

+---+---+---+---+---+

|   |   |   |   |   |

+---+---+---+---+---+

| C |   | C |   |   |

+---+---+---+---+---+

|   | C | C |   |   |

+---+---+---+---+---+

|   | C |   |   |   |

+---+---+---+---+---+

Generation 2

+---+---+---+---+---+

|   |   |   |   |   |

+---+---+---+---+---+

|   |   | C |   |   |

+---+---+---+---+---+

| C |   | C |   |   |

+---+---+---+---+---+

|   | C | C |   |   |

+---+---+---+---+---+

Generation 3

+---+---+---+---+---+

|   |   |   |   |   |

+---+---+---+---+---+

|   | C |   |   |   |

+---+---+---+---+---+

|   |   | C | C |   |

+---+---+---+---+---+

|   | C | C |   |   |

+---+---+---+---+---+

Generation 4

+---+---+---+---+---+

|   |   |   |   |   |

+---+---+---+---+---+

|   |   | C |   |   |

+---+---+---+---+---+

|   |   |   | C |   |

+---+---+---+---+---+

|   | C | C | C |   |

+---+---+---+---+---+