The Game of Life is a computer simulation of life, invented by John Conway
and popularized via Martin Gardner's
columns in Scientific American. 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 counter 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 on a canvas
runs generation after generation, allowing the user to watch how the
patterns change
You should create a class called GameOfLife, with a static void method named
main. This is the method that starts your program. 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 in a similar fashion
to the animation graphics lab.
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.
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.
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.
Use InputBoxes and MessageBoxes (covered in your textbook) to communicate
with the user instead of the Terminal Window.
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.