Skip to content

Lab 9: The Game of Life

Note

This lab is based on Chapter 15 of Think Java, a free and open-source textbook. Feel free to read the chapter if you would like a more detailed explanation of the provided files below.

Learning Objectives

After completing this lab, you should be able to:

  • Extend a class that has a private 2D array attribute.
  • Reason about rows and columns including their length.
  • Calculate row/col indexes that "wrap around" the array.

Background

The Game of Life, invented by the mathematician John Conway, is a "zero-player" game that simulates population growth and decline. You set up the initial conditions and then watch the game play itself. This turns out to be a lot more interesting that it sounds!

Go to PlayGameOfLife.com, click the "Explanation" button (on the lower left), and read how the game works. Then try running the game with different starting configurations. Click the "Lexicon" button to see over a thousand different named patterns.

See also ConwayLife.com, which contains a more sophisticated player, a wiki with over 2,500 articles (about various named patterns), and free 500-page textbook about the mathematics behind the game.

This game is so famous that even a Google Search for Conway's Game of Life shows the game running on top of the search results.

Today's Lab

Download and extract lab09.zip, and put the files in your src/labs folder. This is a simple implementation of the Game of Life.

  • The Cell class represents a single cell of the game (on or off) and includes the graphics code for drawing the cell.
  • The GridCanvas class represents the entire grid of cells and extends Java's Canvas class for drawing 2D graphics.
  • The Conway class implements the logic of the game and contains the main() method for running the program.

In the regular Game of Life, the grid has an infinite size. But our Java implementation uses a finite grid instead. As a result, moving objects such as Gliders either crash into the edge of the screen or go out of bounds.

An interesting variation of the Game of Life is a toroidal grid, meaning that the cells "wrap around" on the edges. Your task for today's lab is to extend the Game of Life to use a toroidal grid. Rather than modify the original files, you will use inheritance to add new features.

Instructions

  1. Run the Conway application. Discuss with others at your table:

    • What is the program doing?
    • How does the "game" work?
  2. Briefly review the source code. For each file, discuss with others:

    • What is the class's purpose?
    • How does the constructor work?
  3. Define a new class named ToroidalGrid that extends GridCanvas.

    • Write a default constructor that calls super(10, 8, 20).
    • Define a public int countOn() method; return 0 for now.
    • Override the test() method of GridCanvas; return 0 for now.
  4. Add code to the constructor to initialize the grid cells like this:

    Screenshot of ToroidalGrid.java

  5. Implement the countOn() method to return the total number of cells that are "on". This method can be used, for example, to track the population over time.

  6. Override the test() method so that the coordinates r and c map to the opposite side of the grid if the values are too low or two high.

  7. Modify the constructor of Conway to initialize grid to a ToroidalGrid. Then run the simulation and see if it works! The Glider should "fly" around the grid forever.

  8. Test your solution using ToroidalGridTest.java. Once everything is working (and passes Checkstyle), submit your ToroidalGrid.java to Gradescope.