CS 117: Artificial Life
Overview
Artifical life is the area of study that is characterized by
simulating aspects of living beings with a computer. Perhaps the most
well-known example of artifical life, referred to as "ALife" by some,
is the "Game of Life" by John Conway. A grid, like the one shown
above, contains empty and filled cells. Each filled cell represents an
"organism." Every turn, or "generation," of the game consists of
applying Conway's three "genetic laws," which are:
- Survival: Every live cell with two or three live neighbors survives
for the next generation.
- Death: Every live cell with four or more live neighbors dies (is removed)
from overpopulation. Every organism with one live neighbor or none dies from
loneliness.
- Birth: Every empty cell adjacent to exactly three neighbors is a birth
cell. It becomes a live cell at the next move.
Your mission: Program this in Java.
Details
Create a Java program that does the following:
- asks the user for a grid size
- asks the user to enter (x,y) coordinates for all the starting locations
for live cells
- renders all the cells as circles on a canvas
- runs generation after generation, allowing the user to watch how the
patterns change
You should create a class called GameOfLife
, which
contains your main
method. Beyond this, you are free to
structure your program as you wish, so long as it is reasonable and
conforms with good style. Here are some ideas you may choose to
follow:
- Your game board is represented as an object. The grid is a two-dimensional
array.
- Since the same canvas will be used generation after generation, it
should be an object variable. Erase it at the beginning of each generation.
- Write methods to... (you will need more, but this is a starting point)
- Add a live cell to the board, doing appropriate error checking.
- Draw the current generation on the canvas.
- Calculate what the new generation will be based on the old one.
You will need a second two-dimensional array here, since you need to work
on your new generation while still having your old one to look at.
- Use a loop to to drive the repeated generations.
Hints, Suggestions, Etc.
- Come up with a way to test your code non-interactively. When testing
your program, you don't want to have to enter in the grid locations of your
starting cells over and over again.
- Check out the
wait
method in
Canvas
. You may find this useful for slowing the
simulation down so you can watch it.
- Once you have it working, try lots of starting patterns and see how
the game responds. You can get some ideas from the Reed College Artificial Life
Project. Try blinkers and gliders for starters.
- Interested in more? I cover Genetic Algorithms in my Artificial Intelligence
course (CS 327).
Extras
The following ideas are not required, but are things you can try if you
want to push this further:
- Provide status information at the bottom of the canvas indicating
how many cells are live, and how many generations have passed.
- Detect when nothing is changing, and stop the game.
- Detect when the game is stuck in a cycle, and prompt the user to stop
if desired.
- Make it into a 2 player game. A description
of a 2 player version, called P2life, can be found here.
Note: The Game of Life is lots of fun, and quite popular. There are many
versions of this game already available coded in Java on the web. Don't use
them. See the syllabus for further info on plagiarism.