Chapter 15. Arrays of Arrays

The last two chapters of this book use 2D graphics to illustrate more advanced object-oriented concepts. If you haven’t yet read Appendix C, you might want to read it now and become familiar with the Canvas, Color, and Graphics classes from the java.awt package. In this chapter, we use these classes to draw images and animations, and to run graphical simulations.

Conway’s Game of Life

The Game of Life, or GoL for short, was developed by John Conway and popularized in 1970 in Martin Gardner’s column in Scientific American. Conway calls it a zero-player game because no players are needed to choose strategies or make decisions. After you set up the initial conditions, you watch the game play itself. That turns out to be more interesting than it sounds; you can read about it at Wikipedia’s “Conway’s Game of Life” entry.

The game board is a 2D grid of square cells. Each cell is either alive or dead; the color of the cell indicates its state. Figure 15-1 shows an example grid configuration; the five black cells are alive.

Figure 15-1. A Glider in the Game of Life

The game proceeds in time steps, during which each cell interacts with its neighbors in the eight adjacent cells. At each time step, the following rules are applied:

  • A live cell with fewer than two live neighbors dies, as if by underpopulation.

  • A live cell with more than three live neighbors dies, as ...

Get Think Java, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.