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
Copy your files from last week to a new working directory.
Download the class LandscapeDisplay.java
- 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).
class Life - create a class called Live that extends Simulation. In
the Life class you will need to override the Iterate method and the
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
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.
- 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.
- Change the rules of the game and see what happens.
- 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.
- 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.
- 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.
- Use command line arguments to control some of the simulation parameters (number of rows and columns, for example).
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.
- A brief description of the overall task, in your own words.
- As explanation of your solution, focusing on the interesting bits. The interesting bits here are how you implemented the NumNeighbors function, how you implemented the Iterate method, how you initialized the game, whether you permit command line arguments, and any extensions.
- Printouts, pictures, or results to show what you did. You can do screen captures of your terminal to show the initial and final landscapes. I'll show you how to create animated gifs in lab.
- Other results to demonstrate extensions you undertook. If you tried different update rules, for example, show how those affected the overall simulation results.
- A brief conclusion and description of what you learned.
Once you have written up your assignment, give the page the label:
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.