CS 231: Assignment #3

Game of Life

This week you'll implement Conway's Game of Life. You'll use the Entity and Landscape classes from last week, but build an extension of the Simulation class to implement the Game of Life simulation.

Conway's Game of Life is a well known example of cellular automata (probably the most famous example). It is a variation on the spatial simulation of last week, using rules about how many neighbors an entity has to determine its fate.

For more information, see the Wikipedia entry.

Documentation for Java 1.5 is located at: Java 1.5 SE API.

Documentation for Java 1.6 is located at: Java 1.6 SE API

Setup

Copy your files from last week to a new working directory.

Download the class LandscapeDisplay.java

Tasks

  1. class Landscape - add a NumNeighbors(int r, int c) method to your Landscape class that returns the number of Entities next to location (r, c) in an 8-connected sense (diagonals as well as NSEW).
  2. class Life - create a class called Live that extends Simulation. In the Life class you will need to override the Iterate method and the main method.
    • Iterate - This function needs to iterate over each location in the Landscape and determine if there should be an Entity at that location in the next iteration. The rule is that if an existing Entity has 2 or 3 neighbors, it should remain in place. Otherwise, it should be removed from the Landscape. Any empty location in the Landscape that has exactly three Entities around it should get a new Entity.

      Make sure you do not change the Landscape while you are evaluating what should occur at each location. You will probably want to do this in two passes. In the first pass, figure out which locations should have Entities in the next iteration. In some cases Entities will remain, while in others you will need new Entities. You may want to store the list of Entities for the next iteration in an an ArrayList.

      Once the first pass is complete, clear the Landscape and then place the Entites from the array into it.

    • main - the structure of the main function should be almost identical to the one in Simulation. However, you want to allocate a Life object instead of a Simulation object. You should also allocate a LandscapeDisplay object and pass it the Landscape object in the constructor.

      In the main iteration loop, use the update() method of the LandscapeDisplay object to update the visualization. You should also use the Thread.sleep() method to keep the visualization from going too fast. An argument of 500 (ms) works well.

      While debugging, you can also still print your Landscape to the terminal.

Extensions

  1. Change the rules of the game and see what happens.
  2. Combine the rules from this week and last week and use two different types of Entities, where only like entities count as neighbors. The LandscapeDisplay will use up to 10 different colors in the visualization.
  3. Let Entities have one extra turn to live if they are not in a good situation. Let them move randomly by one square, if the square is not occupied.
  4. There are a few places in the code where you loop over an ArrayList. Use an iterator instead of an index in your for loop.
  5. Use command line arguments to control some of the simulation parameters (number of rows and columns, for example).

Handin

Make your writeup for the project a wiki page in your personal space. If you have questions about making a wiki page, stop by my office or ask in lab.

Your writeup should have a simple format.

Once you have written up your assignment, give the page the label:

cs231f09project3

You can give any page a label when you're editing it using the label field at the bottom of the page.

Do not put code on your writeup page or anywhere it can be publicly accessed. To hand in code, attach it to an email and send it to the prof.