YCP Logo Assignment 2: Conway's Game of Life

Due: Monday, Sept 22nd by 11:59 PM

In this assignment, you will implement Conway's Game of Life. Life is a simulation of simple one-celled organisms.

Cells either live or die according to the Rules of Life:

  • if a cell is alive now, but has fewer than 2 neighbors, it dies of isolation
  • if a cell is alive now, but has more than 3 neighbors, it dies of overcrowding
  • if a cell is dead now, and has exactly 3 neighbors, it is born

A simple GUI is provided so that you can visualize the progress of the successive generations of organisms.

figures/lifescreenshot.png

Your task is to implement the class GameBoard which represents one generation of cells. The board is a two-dimensional grid of cells, where each cell contains an integer value. The value of a cell is either 0, meaning that the cell is dead, or some positive integer representing the number of generations that the cell has been alive.

Methods to implement

You will implement the following methods in the GameBoard class:

  • The constructor public GameBoard(int numColumns, int numRows), which initializes a new game board with the specified number of columns and rows. Each cell in the new game board should be set to 0.
  • public int getNumColumns(), return the number of columns in the game board.
  • public int getNumRows(), return the number of rows in the game board.
  • public void initializeRandom(), randomly set each cell of the board to either 0 or 1.
  • public static GameBoard loadFromFile(String fileName), load game board data from a file and return a new GameBoard object. The format of the file is described in the comment for the method.
  • public int getCell(int column, int row), get the value of the cell at given column and row, returning 0 if either the column or the row is outside the bounds of the game board.
  • public void setCell(int column, int row, int value), set the value of the cell at given column and row. This method does not have to handle out-of-bounds column or row numbers.
  • public GameBoard computeNextGeneration(), return a new GameBoard object with the same dimensions as this object, containing the next generation of organisms, as determined by the Rules of Life.

Before You Do Anything Else

In this course we will be using new features that were introduced in the most recent version of Java. So, we need to tell Eclipse that we want to use these new features.

In Eclipse, choose Window->Preferences...->Java->Compiler. In the dialog, set Compiler compliance level to 5.0. Click Ok and then answer Yes when prompted whether or not you want to do a full rebuild.

After this configuration change, you will be able to use the latest Java features in all of your projects.

Getting started

Start by importing the file CS201_Assign2.zip into Eclipse: File->Import->Existing Projects into Workspace, click Select archive file, select CS201_Assign2.zip from the file selection dialog, and then click the Finish button.

The LifeGUI class has a main method that will start the GUI interface. To execute LifeGUI, right-click LifeGUI.java in the Package Explorer, then choose Run As->Java Application.

The Random button creates a new random game board. The One Step button applies the Rules of Life to the current game board; therefore, it is a good way to test your implementation of the GameBoard class. Once you have GameBoard working, you can use the Animate button to play a sequence of generations, 3 generations per second. The Load button will use your implementation of the loadFromFile method to load a new game board. Some sample files are provided in the handouts folder in your project: for example, glider.life initializes the board to display a single glider pattern, which evolves in an interesting way. (Note that when you run the GUI, the file chooser will start in your home directory. To get to the sample files, look in the handouts directory of the CS201_Assign2 project.)

Hints

The class GameBoardTest contains JUnit test cases for the GameBoard class. When you have finished implementing the GameBoard class, all of the tests it defines should pass. You may wish to add some new test objects or test methods of your own.

Try loading blinkers.life and animating it. A blinker is a 3-cell pattern that oscillates with period 2.

Make sure that when you load the file glider.life and animate it, the glider shape starts at the top left and moves towards the bottom right of the game board.

glidergun.life produces a stream of gliders.

When you are computing the GameBoard representing the next generation, remember that the value of the cells in the new game board records the number of generations that the cell has been alive. The GUI displays the number of generations a cell has been alive using different colors: red cells are new, yellow and green cells have lived a few generations, and blue and purple cells are oldest.

Submitting the project

Export your project as a zip file (File->Export...->Archive File) and upload it to the submission server as assign2. The URL of the server is

https://camel.ycp.edu:8443/