The Darwin World

This assignment is based heavily on an assignment given in the Stanford University course CS106B and originally developed by Nick Parlante.

The Darwin world is a two-dimensional grid populated by a number of creatures. Each creature inhabits a square in the grid, and faces north, south, east or west. Each creature belongs to a species, which determines how the creature behaves.

In the picture below from the Darwin simulator, there are six rover creatures indicated by a black R, four hopper creatures indicated by a red H, three flytraps indicated by a green F, and one food indicated by a blue f.

In a Darwin-world simulation, each creature acts according to its species program. In some sense the creatures are like robots that obey their program. For example, all Rovers behave the same way as do all Flytraps, but each species behaves differently from other species.

In a simulation, each creature gets a turn. During a turn, a creature executes part of its program in which it can see directly in front of itself and take an action depending on what's there. Creatures can turn left or right, move forward, and infect other creatures. Infecting a creature turns it into the same species doing the infecting. The ultimate goal is to be the fittest creature and survive by taking over the world. When a turn ends, another creature gets a turn. When every creature has had a turn the process begins again with each creature getting another turn. This continues indefinitely until there is only one species left or until some predetermined time limit is reached.

Species Programming

A creature executes a program specific to the species to which it currently belongs. Programs are written in DULL, Darwin Unstructured Lattice Language. The program for a Flytrap is shown below.
Flytrap Program
step instruction comment
1 ifenemy 4 if there is an enemy ahead, goto step 4
2 left turn left
3 go 1 go (back) to step 1
4 infect infect the creature adjacent/in front
5 go 1 go (back) to step 1

The step numbers are not part of the actual program, but make it easier to understand the program. A Flytrap first checks to see if there is a creature immediately in front. If there is a creature in front, the program jumps to step 4 and infects the creature there. If there is no creature in front, the program continues to execute with step 2 which makes the creature turn left. In either case the next statement is go 1 so that program execution continues with step 1 and the Flytrap starts over at the beginning of the program.

Programs begin execution with step 1 and continue with each step in sequence although this order can change based on if statements. A list of legal program instructions is provided below.

List of Darwin Commands

hop | left | right | infect | ifempty | ifwall | ifsame | ifenemy | ifrandom | go

Executing a Program

A creature can execute any number of if or go instructions without relinquishing its turn. The turn ends only when the program executes one of the instructions hop, left, right, or infect.

The program for a species is stored in a file with a suffix of .spc, i.e., a flytrap species program would be stored in a file flytrap.spc. The first line of a file is the name of the species, and the first letter is used when displayed in the graphical Darwin-world. The program ends with a blank line, or when no other instruction follows. Comments may appear after the blank line. For example, a program for a flytrap might be written as follows:

Flytrap ifenemy 4 left go 1 infect go 1 The flytrap sits in one place and spins. It infects anything which comes in front. Flytraps do well when they clump.

There are four supplied species:

Running Darwin

To run the Darwin simulator you should type darwin with arguments following the program name (see below).

You can run the program with different arguments as described below.

Designing Species

You are to write programs for several new species. After you've written the corner species, we'll use the computer to test your implementation and the other species as well. You're also to design a creature that will compete against other creatures to see which creature is the fittest. In this survival contest, two creatures will be placed in a 15 X 15 world with 10 food creatures.

Write programs for each of the following species


Because it's painful to write a Darwin program using numbers, you will be able to use the Zap assembler. This will allow you to use symbolic labels. For example, the Rover program can be written in Zap assembler as shown below. start: ifenemy doinfect ifwall dorandom ifsame dorandom hop go start dorandom: ifrandom doright left go start doright: right go start doinfect: infect go start If this is stored in a file rover.d, and the zap compiler invoked using
     zap rover.d
then the rover program will be written to a file named rover.spc. You'll need to edit this file using emacs to add the name of the species at the top.