Due electronically: Thursday 25 September, 6:00 pm
This assignment is worth 2 marks towards your grade in this course.
Part marks will count.
This is a warm-up assignment
to get everyone going in Java.
While the language is probably new to most of you,
coming up with the algorithms required for this
assignment should be a straightforward task for all CSC148 students.
If you don't find it so, then you may need to get more programming
experience before taking 148.
The game of life
The game of Life was developed in 1970 by John Conway at the University
of Cambridge. It is meant to demonstrate that some simple local
rules can lead to interesting large scale behaviour.
The game is played on a two-dimensional grid of cells. Each cell can either be occupied by an organism or unoccupied. The game begins with a selection of occupied cells. From this initial generation, the next generation is calculated using the following rules:
For example, on the left below
is an initial grid of cells, with the organisms represented by
black squares.
Given this grid, the next generation would be as shown on the right:
This process of evolution continues until either of two things happens:
Notice that this is not a ``game'' as we usually think of it -- a set of rules specifying a competition between active, human players.
Your task
Your task is to write a program that plays the Game of Life.
Because we don't have until the heat death of the universe to wait
for the next generation to be calculated, your program will use
a finite grid of cells (rather than an infinite one, as the game
was originally meant to be played).
With a finite grid, however,
organisms at the edge will tend to die since they don't have as many
potential neighbours to keep them alive.
To remedy this, we will act as though the grid is wrapped around a torus
(a donut shape). If we think of the grid this way,
there are no more edges, and each cell will have 8 neighbours.
Make sure that your program behaves this way.
For example, for a cell at the left hand edge, the neighbour to its ``left''
is on the right hand edge of the grid.
This will actually make your code simpler, if you do it sensibly.
Consider using the % operator, which computes integer remainders.
(Some languages call this ``mod''.)
For example, 28 % 5 has the value 3.
Input and output specifications
The input to your program must be in a file called LifeInput
and must consist of the following:
9
6 6
1 1
1 2
1 3
2 1
The output from your program will be a representation
of each generation, from the initial configuration specified in
the input to the final generation computed.
Two kinds of output are simultaneously produced: text-based
and graphical.
In the text-based output,
empty cells are indicated by `-' and
live cells by `X'.
Each generation's output is preceded by a message indicating
which generation it is.
For example, below is possible output for one generation:
Generation number: 12
------
-XX---
XX---X
-----X
-X----
-X----
In the graphical output, empty cells are white and live cells are
coloured.
The age of an organism is indicated by its colour: organisms are
born white, and their colour becomes darker as they age.
Choosing output mode
Sometimes you may not want both kinds of output.
If you wish the program
to produce only text-based output, run the program
with the argument `text'; if you wish only graphical output,
run it with the argument `pretty'.
The default is that both types of output are produced.
This occurs when no argument is given.
How to write your program
We have already written most of the program for you, and
it is available online through the course web page
(You will also find there an example of an input file
and the output that it should produce.)
You must use our code as your starting point.
Make your own copy of the starter code using your web browser.
(With Netscape, you can use the
``Save As'' command under the ``File'' pull-down menu.)
Then edit the program and insert the missing piece,
the body of a single method,
in the place marked by >> and <<.
You must use our starter code, and
you must not make any alterations to it.
In particular, do not change the existing (or add new)
input or output statements.
Do not add prompts for the input.
You may find it convenient to break down the missing method
into smaller subtasks.
Feel free to add additional methods.
They should be labelled ``private'', since they are just helper
methods and will never be called from outside the class.
For those of you programming in Java for the first time,
the starter code may be a little overwhelming at first.
It's not huge, but everything looks new.
Don't panic. You can ignore large parts of the program at first,
and the code you are going to write is very small.
To help you get started, and to help you later figure out the parts of the start code that you can initially ignore, we've written a small guide to the starter code. It is available on the course web site.
How to hand in your assignment
For this assignment,
your solution will be marked by a computer program,
which will put it through a thorough set of
test cases.
We will not tell you what these tests are.
Because the assignment is simple, the marking scheme
will be tough.
You should therefore test your code very thoroughly
before handing it in.
Because this assignment is to be marked by a program,
you will hand it in electronically rather than
on paper.
When you are convinced of the correctness of your code,
export it into a single text file called Life.java and
hand it in electronically using the ``Hand in (submit)'' command,
found under the ``File Utilities'' icon on the PCs in our lab.
If you hand in a solution more than once, we will have only a copy of your
most recent version; our copy of the older ones will
be erased.
The program that will mark your assignment is not terribly
clever,
and can only compare your program's output character for character
with what we tell it is the correct output.
If your output is different in any
way from the expected output, it will be considered
wrong, and your mark will be zero.
For this reason, we are providing, among other things,
the statements that perform input and output.
If you modify these in any way, or add other input or output
statements
(for example, if you add prompts)
your mark will be zero.
In addition, you must use the file name Life.java
when you hand in your assignment, or your mark will be zero.
Some useful advice
The most effective way to get this assignment done quickly is to write
your code out on paper and simulate it by hand before you
go near a computer, in order to get rid of as many errors as possible.
Even if you plan to develop your program at home, you will have to use one
of our PCs
to hand in your final program.
Make sure you log in and get comfortable with our computers well before
the due date.
Try to get your assignment finished early. The computers are likely to
be very crowded near the due date.
This assignment is unusual in comparison to the rest of the assignments
in CSC148.
First,
it is far less difficult
than the assignments to come.
Second, you will hand it in electronically, whereas
future assignments will generally be handed in on paper.
Finally, only the
correctness of your code will be evaluated.
Future programs will be evaluated also for programming style,
documentation, and testing, and will require a report.
These aspects may be worth as much as 75% of future
programming assignments.
Check the 148 web site
Remember that it is your responsibility to regularly check the
course web site for announcements.
There we often post answers to common questions concerning assignments,
and sometimes corrections to assignments.
Because this is the first time we are teaching 148 in
Java, there will undoubtedly be a few glitches.
Some cool things
You may notice some very interesting patterns emerging when you run
your program. Over the course of the years, many of these
have been named by intrepid Life programmers.
For example, a glider is a series of five cells that
seems to move diagonally across the grid.
If you'd like to read more about Life, check out:
This document was generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
The command line arguments were:
latex2html -split 0 -no_navigation a0.tex.
The translation was initiated by Diane Horton on Fri Sep 5 18:13:59 EDT 1997