Darwin's World
Darwin's World is a two dimensional grid filled with creatures.
Each creature lives in a single square of the grid, and faces in one of
the 4 major compas directions, North, South, East, or West, and belongs
to a Species, which determines how the creature behaves.
This world has 20 creatures, 10 Flytraps (the blue F's) and 10
Rovers (the Light purple R's). The orientation of each
creature is shown by the arrow which points in the direction that the
creature is facing. At each step of the simulation, each creature
performs one of 4 actions:
- LEFT: Rotate left 90 degrees
- RIGHT: Rotate right 90 degrees
- HOP: Move forward one square, if the square in front of the
creature is empty
- INFECT: If the square in front of the creature is of a
different species, convert that creature to my species
The goal of each creature is to infect as many different other
creatures as possible, to increase the population of its species
Species Programming
In order to know what to do at each simulation step, each creature
executes a number of instructions of the program for its species.
For example, the Flytrap code is:
Step
|
Instruction
|
Comment
|
0
|
ifenemy 3
|
If there is an enemy ahead, go
to step 3
|
1
|
left
|
Turn left
|
2
|
go 0
|
Go to step 0
|
3
|
infect
|
Infect the adjacant creature
|
4
|
go 0
|
Go to step 0
|
The step numbers are not part of the actual program (step numbers are
implicit), but are included here to make the code easier to
follow. On each turn, the flytrap checks to see if there is an
enemy immediately ahead. If there is an enemy, it jumps to step
3, where it infects the enemy, and then contines to step 0. If there is
no enemy, it continues on to step 1, where it turns left before jumping
back to step 0.
The instructions that can appear in a creature's program are:
hop
|
Move forward one square.
If the next space is either outside the world, or contains another
creature, then the hop command does nothing
|
left
|
Rotate left 90 degrees
|
right
|
Rotate right 90 degrees
|
infect
n
|
If the space immediately in
front of the creature is occupied by an enemy, that creature is
infected. The infected creature maintains its orientation and
position, but changes its species to the species of the infecting
creature. On its next turn, the infected creature will start
executing code starting at step n. If n is not given, then the
infected creature starts at step 0. That is, infect is equivalent to infect 0. If there is not
an enemy immediately in front of the creature, then infect does nothing.
|
ifempty n
|
If the next square is empty, the
next instruction to be executed is at step n. If the next square
is not empty, continue on to the next step
|
ifwall n
|
If the creature is facing the
border of the world, go to step n. If not, go on to the next step
|
ifsame n
|
If the creature is facing
another creature of the same species, go to step n. If not, go on
to the next step
|
ifenemy
n
|
If the creature is facing
another creature of a different species, go to step n. If not, go
on to the next step
|
ifrandom
n
|
Go to step n with probability
0.5, and go on to the next step with probability 0.5
|
go n
|
Go to step n
|
A creature can exeucte any number of if or go instructions in a single
turn. The turn ends when the program exectutes a
hop, left, right, or
infect. On subequent
turns, the program starts up from the point in the program at which it
ended its previous turn. Note that even an
unsucessful hop or infect will end the creature's turn
Species File Format
Each species is described in a file which consists of the species name,
followed by the steps in the species program, in order. The
program ends with a blank line or end of file. Comments may
appear after the blank line, or at the end of each instruction
line. For example, the program file for the Flytrap creature is:
Flytrap
ifenemy 4
left
go 1
infect
go 1
The flytrap sits in one place and
spins.
It infects anything that comes in
font.
Flytraps do well when they clump.
Some basic creatures are provided:
Food
|
This creature does nothing but
spin around waiting to be infeced by another creature.
|
Hop
|
This creature hops forward until
it hits a wall. Not very smart.
|
Flytrap
|
This creature spins in place,
infecting any enemy that it sees. Works well against
Rovers, not so well against food, since it never goes looking for other
creatures to infect.
|
Rover
|
This creature walks in a
straight line until it is blocked, infecting any creature that it
sees. If it can move forward, it turns in a random
direction. Does very well in the presence of food.
|
Landmine
|
This creature is much like the
flytrap: it sits in one place, looking to infect. Unlike
the flytrap, it only turns if it is facing a wall or another
landmine. When landmines clump together, they can be
formitable. Like the flytrap, it is not really helped by food.
|
You are also encouraged to create your own creatures. Can you
combine the food-finding ability of the Rover, with the clumping
ability of the Flytrap or Landmine?
Assignment
Your assignment is to program Darwin's world. This is a long,
complicated program, split up into several classes, 4 that we
will provide, and 4 that you need to write yourself.
Provided files:
- Instruction.java
Simple class that encapsulates a single instruction, which consists of
an OpCode (hop, left, right, go, etc) and an address. The address
is only used by ifempty, ifenemy, ifwall, ifsame, infect, and go
- Point.java Simple class
that encapsulates a point with integer coordinates and some utility
functions
- Direction.java Enumerated type that represents the 4
compass directions (North/South/East/West), and some utility functions
- WorldDisplay.java Class that handles all of the GUI
elements for Darwin's World
You are allowed (but not required) to modify the provided files if you
would like. For instance, you could add to the WorldDisplay class
so that it shows a count of the number of individuals of each species,
so you can see who is winning. The WorldDisplay class is pretty
similar to the other Swing GUI stuff we've seen, see me if you have
questions.
- Creature.java A class that represents a single
creature. The main interpreter loop is handled here
- Species.java A class that represents a species.
Programs are stored in the Species class
- World.java A class that represents the positions of
the creatures in the world. Note that while the world is
logically a 2D grid, you probably don't want to represent it that way
...
- Darwin.java Main program, which sets up the initial
state of the word, and then repeatedly calls the takeOneTurn of each
creature and updates the display. Your main should take the
following command line parameters (found in args[0], args[1], etc):
<width of world> <height of world> <number of creatures
of each species> <list of species files>
For instance, a valid set of command line parameters might be:
20 20 10 flytrap rover
or
30 30 10 flytrap rover food
landmine
Your main program should:
Parse the command line parameters
Set up the World, Creatues, and WorldDisplay
Go into an infinite loop:
take one step for each creature
call the displayWorld method of the WorldDisplay
class
You do not need to start from scratch -- we have provided a jar file
for each of the required classes, so you can get Darwin's world up and
running right away. All you need to do is replace the .jar
files with your own .java files. There are two ways to do this:
- Using the command line: Create a folder that contains all
of the provided jars. Add this folder to your classpath.
You can run the provided main program using java -jar
Darwin.jar. As you write your own versions, remove the .jar files
from the directory in your classpath
- Using Eclipse: Save the .jar files to any folder on your
machine. Create a project that contains all of the provided .java
files. Right-click on your project, go to Build Path ->
Configure Build Path, then select Add External Jars (under the Librarys
tab), and add the provided .jar files. You can run the provided main
program
by finding it in the package explorer (under referenced libraries) and
running it. As you write your own versions of the classes, remove
the approprate jars from your project
Part I: Due Wednesday, April 2
For part I of this assignment, you must complete the main class Darwin,
as well as the World and Species classes.
Part II Due Wednesday, April 9
For part II of this assignment, you must complete the Creature class,
and write your own creature, to compete against the rest of the
class.