CompSci 308
Spring 2025
Advanced Software Design and Implementation

Cell Society: Darwin Simulator

This language was invented by Nick Parlante at Stanford University where it used to be the final assignment for CS106B (their version of CompSci 101).

Create an interactive simulator for a world populated by a number of creatures, showing each creature's current species, location, and orientation, to see how they interact together (and perhaps which species dominates!).

A creature's species is determined by the program it executes on each step of the simulation (so all creatures of the species behave the same way by running the same program although they may each be at a different instruction of their program). A step of the simulation is completed when all non-infected creatures have run one step of their program (i.e., if a creature is infected during a step, then it does not get a chance to run its new program).

During a simulation step, a creature may look in front of itself to see what’s there and then take one action. Thus a program may evaluate any number of IF or GO instructions in a single step. The step ends when the program executes an action instruction (one of MOVE, LEFT, RIGHT, or INFECT). The program starts the subsequent simulation step at the next instruction after the last one executed or starts the program over from the first instruction.

There is one parameter for this model:

Example

This is an example of a typical program:

Program Notes Reference
# Flytrap species
ifenemy 4
left 90
go 1
infect 12
go 1

If a creature is ahead nearby, go to instruction 4
Turn left 90 degrees
Go back to instruction 1
Infect the other creature for 5 simulation steps
Go back to instruction 1

Instruction 1
Instruction 2
Instruction 3
Instruction 4
Instruction 5

More examples are given here. You are expected to make your own example files as well (including ones with syntactically incorrect programs).

Commands

Name Description
MOVE pixels
MV pixels
Move creature forward by the given pixels in the direction it is facing.
If moving forward would put the creature outside the world's boundaries (if any) or cause it to land on another creature, then this instruction does nothing.
LEFT degrees
LT degrees
Turn creature in place left by the given degrees
RIGHT degrees
RT degrees
Turn creature in place right by the given degrees
INFECT steps
INF steps
Change a creature of any different species (an enemy) into your species if it is in the space nearby ahead for the given number of simulation steps
When a creature is infected, it changes its internal program to the same as the infecting creature (i.e., it becomes one of them) and resets its steps count
When the number of simulation steps is reached, the creature reverts back to its most recent previous species and retains that program until it is infected again
If there is no creature in the space nearby ahead, then this instruction does nothing
IFEMPTY instruction
EMP? instruction
Perform the given instruction next only if the space nearby ahead of the creature is empty and inside the world's boundary;
otherwise, go on with the next instruction
IFWALL instruction
WL? instruction
Perform the given instruction next only if the space nearby ahead of the creature is the world's boundary;
otherwise, go on with the next instruction
IFSAME instruction
SM? instruction
Perform the given instruction next only if the space nearby ahead of the creature is occupied by a creature of the same species;
otherwise, go on with the next instruction
IFENEMY instruction
EMY? instruction
Perform the given instruction next only if the space nearby ahead of the creature is occupied by a creature of any different species;
otherwise, go on with the next instruction
IFRANDOM instruction
RND? instruction
Perform the given instruction next half of the time (otherwise continue with the next instruction half of the time)
This allows some creatures to exercise what might be called the rudiments of “free will”
GO instruction Perform the given instruction next, rather than the following instruction (if present)

Notes about a program's syntax:

Resources