John H. Conway, a Cambridge mathematician, invented the Game of Life. The simulation runs on a square, two-dimensional array. Each cell in the array is in one of two states: occupied by a creature or empty.
Initially, the grid is populated randomly with occupied and empty cells. Once the initial grid has been created, the program loops. Each iteration of the loop represents a tick or time step in the environment.
Cells interact with their eight neighbors. Neighbors are the cells that are horizontally, vertically, or diagonally adjacent.
Creatures are created and die based on the following rules:
-
Any living creature (occupied cell) with fewer than two live neighbors dies of loneliness.
-
Any living creature with two or three live neighbors lives on to the next generation.
-
Any living creature with more than three live neighbors dies of overcrowding.
-
Any empty cell with exactly three live neighbors becomes an occupied cell. This represents a birth.
Your project should have three classes: Cell, Environment, and EnvironmentDriver. The Cell class will represent a single cell. It should contain the following:
Instance variables:
• A private boolean variable occupied that is true if the cell is occupied and false if
the cell is not occupied.
Methods:
-
A constructor that takes one boolean argument. This constructor should use its
parameter to set the instance variable occupied.
-
An accessor (get) method for the occupied instance variable.
The Environment class be responsible for running the simulation: Instance variables:
- A private two-dimensional array of cells.
Methods:
-
A constructor that takes one argument, n. The constructor should create the n x n
array of cells. It should then randomly fill the array with occupied and empty cells.
-
A public runSimulation method is responsible for displaying the grid graphically
and creating the next generation based on the rules given above.
In addition, you should create private methods that divide up the work and make your code easier to read. These might include: a method that that is responsible for creating a grid that represents the next generation of creatures; a method that determines how many neighbors a single cell has; and a method that displays the grid graphically.
You should use the class StdDraw to create your graphical representation. StdDraw will be demonstrated in class.
The EnvironmentDriver class should contain the main method that starts the simulation. Here is some sample code for this method:
public static void main(String[] args) {
Environment e = new Environment(20);
e.runSimulation(); }
This code creates a 20 x 20 grid and starts the simulation.