Class Simulator

java.lang.Object
  extended by Simulator
All Implemented Interfaces:
javax.swing.Icon

public final class Simulator
extends java.lang.Object
implements javax.swing.Icon

Darwin 2.0 simulator. Executes the simulation on a set of Creatures.

Maps are ASCII files. The first line must contain two integers (width and height of the map) separated by spaces and terminated by a newline (anything else on that line is ignored). The remaining lines are a picture of the map. The elements are:

The array passed to the Simulator constructor indicates which subclass should be loaded for each spawn point. The outer edge of the map is forced to wall blocks regardless of whether it is specified that way or not.

Simulator implements Icon so that it can be rendered. It can also be displayed in text mode in text mode using toString().

To complete the lab assignment, you do not need to read the source code for this class, or understand how it works.

Morgan McGuire
morgan@cs.williams.edu

Inspired by the RoboRally board game, Steve Freund's "Darwin" lab assignment, and Nick Parlante's "Darwin's World" assignment.


Nested Class Summary
 class Simulator.PopulationGraph
          Graph of population over time for each creature type.
static class Simulator.Result
          Return value of getResult()
 
Field Summary
static int ATTACK_COST
           
static int LOOK_COST
           
static int MOVE_BACKWARD_COST
          Note: at moving backwards 3
static int MOVE_FORWARD_COST
           
static int SCALE2D
          2D grid size in pixels.
static java.lang.Class[] sharedClasses
          Shared between ClassLoader (Sandbox) instances
static int TIME_STEP_LIMIT
          The game ends after this many time steps, even if no Creature has won.
static int TURN_COST
           
 
Constructor Summary
Simulator(java.lang.String mapFilename, java.lang.Class[] creatures)
          Loads the specified map, instantiating each of the creatures for the numbered spots in the map.
 
Method Summary
 boolean attack(Creature e)
          Attack the creature in front of this one.
 void delay(int n)
          Wait the delayTime * n.
 Creature getCreature(int x, int y)
          Returns the creature at x, y on the grid.
 int getCreatureCount(java.lang.Class c)
          Returns the number of creatures of this species alive in the map.
 long getDelayTime()
          Returns the time of delay(1) in nanoseconds.
 java.awt.Dimension getDimensions()
          Returns the size of the map.
 Direction getDirection(Creature e)
          Throws an exception if the Creature is not in the world.
 java.awt.Point getGridDrawXY2D()
          Returns the coordinates at which the 2D grid was last drawn
 int getIconHeight()
           
 int getIconWidth()
           
static java.awt.Image getImage(java.lang.Class cs, Direction d)
          Returns the image for this Creature subclass.
static java.awt.Image getImage(Creature c, Direction d)
           
 int getNumSpeciesLeft()
          Returns the number of different species left alive, excluding Flytraps.
 Simulator.PopulationGraph getPopulationGraph()
           
 java.awt.Point getPosition(Creature e)
          Clones the position.
 Simulator.Result getResult()
          Returns a description of the final outcome, or null if the game has not yet ended.
 int getTime()
          Returns the approximate number of time steps that have elapsed since the simulation began.
 java.lang.String getTitle()
          Name of this map
static java.lang.String getVersion()
          Reports the version of this simulator.
 boolean getView3D()
           
 boolean isAlive(Creature c)
          A creature's thread can't be stopped without potentially releasing locks that it is using for synchronization, so this method tells a creature when it should die.
 boolean isRunning()
          True while the simulation is running (not stopped).
static java.lang.Class loadClass(java.lang.String name)
          Loads a class in its own Sandbox.
 Observation look(Creature e)
           
 boolean move(Creature e, int distance)
          Moves this creature forward if the space in front if it is empty, otherwise does not move.
static void nanoSleep(long t)
          On some systems, Thread.sleep(long, int) has a minimum sleep time of 1ms.
 void paintIcon(java.awt.Component c, java.awt.Graphics _g, int tx, int ty)
           
 void setDelayTime(long t)
          Set the delay time in nanoseconds.
 void setSelectedCreature(Creature c)
           
 void setView3D(boolean b)
          If true, the view renders in 3D
 void start(java.lang.String mapfile, java.lang.Class[] creatures)
          Load a new map and start a simulation.
 void stop()
          End simulation, without destroying final state.
 java.lang.String toString()
           
 void turnLeft(Creature e)
           
 void turnRight(Creature e)
          Called by a creature to turn
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

sharedClasses

public static final java.lang.Class[] sharedClasses
Shared between ClassLoader (Sandbox) instances


LOOK_COST

public static final int LOOK_COST
See Also:
Constant Field Values

ATTACK_COST

public static final int ATTACK_COST
See Also:
Constant Field Values

TURN_COST

public static final int TURN_COST
See Also:
Constant Field Values

MOVE_FORWARD_COST

public static final int MOVE_FORWARD_COST
See Also:
Constant Field Values

MOVE_BACKWARD_COST

public static final int MOVE_BACKWARD_COST
Note: at moving backwards 3

See Also:
Constant Field Values

TIME_STEP_LIMIT

public static final int TIME_STEP_LIMIT
The game ends after this many time steps, even if no Creature has won.

See Also:
Constant Field Values

SCALE2D

public static final int SCALE2D
2D grid size in pixels. Must be an odd number

See Also:
Constant Field Values
Constructor Detail

Simulator

public Simulator(java.lang.String mapFilename,
                 java.lang.Class[] creatures)
Loads the specified map, instantiating each of the creatures for the numbered spots in the map.

Method Detail

loadClass

public static java.lang.Class loadClass(java.lang.String name)
                                 throws java.lang.ClassNotFoundException,
                                        java.io.IOException
Loads a class in its own Sandbox.

Throws:
java.lang.ClassNotFoundException
java.io.IOException

getVersion

public static java.lang.String getVersion()
Reports the version of this simulator.


start

public void start(java.lang.String mapfile,
                  java.lang.Class[] creatures)
Load a new map and start a simulation. The simulation is initially paused.


stop

public void stop()
End simulation, without destroying final state.


isRunning

public boolean isRunning()
True while the simulation is running (not stopped). Note that the simulator will never stop itself; it must be externally stopped when the result is interesting.


getTitle

public java.lang.String getTitle()
Name of this map


getTime

public int getTime()
Returns the approximate number of time steps that have elapsed since the simulation began.


getDimensions

public java.awt.Dimension getDimensions()
Returns the size of the map.


delay

public void delay(int n)
Wait the delayTime * n. If delaytime is changed while in delay.


nanoSleep

public static void nanoSleep(long t)
                      throws java.lang.InterruptedException
On some systems, Thread.sleep(long, int) has a minimum sleep time of 1ms. This implements true nano-second sleep intervals even on such systems.

Throws:
java.lang.InterruptedException

getDelayTime

public long getDelayTime()
Returns the time of delay(1) in nanoseconds. Initially huge, so that the simulator is "paused".


setDelayTime

public void setDelayTime(long t)
Set the delay time in nanoseconds. 1 millisecond = 1e6 nanoseconds 1 second = 1e9 nanoseconds Minimum value is 100000.


setView3D

public void setView3D(boolean b)
If true, the view renders in 3D


getView3D

public boolean getView3D()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

turnRight

public void turnRight(Creature e)
Called by a creature to turn


turnLeft

public void turnLeft(Creature e)

look

public Observation look(Creature e)

attack

public boolean attack(Creature e)
Attack the creature in front of this one. If there is a creature there that is not a subclass of this creature, kill it and spawn another instace of this creature facing in the same direction as this creature.


getNumSpeciesLeft

public int getNumSpeciesLeft()
Returns the number of different species left alive, excluding Flytraps.


getCreatureCount

public int getCreatureCount(java.lang.Class c)
Returns the number of creatures of this species alive in the map.


getResult

public Simulator.Result getResult()
Returns a description of the final outcome, or null if the game has not yet ended. A creature has won if:


isAlive

public boolean isAlive(Creature c)
A creature's thread can't be stopped without potentially releasing locks that it is using for synchronization, so this method tells a creature when it should die. Creatures are not allowed to move when dead.

All creatures are considered dead once the simulator is stopped.


getPosition

public java.awt.Point getPosition(Creature e)
Clones the position. Throws an exception if the Creature is not in the world.


getDirection

public Direction getDirection(Creature e)
Throws an exception if the Creature is not in the world.


move

public boolean move(Creature e,
                    int distance)
Moves this creature forward if the space in front if it is empty, otherwise does not move.

Moving into a Thorn causes a Creature to be converted into an Apple. Throws ConvertedError if the Creature is not in the world.

Parameters:
distance - must be -1 or +1
Returns:
true if moved.

setSelectedCreature

public void setSelectedCreature(Creature c)

getIconHeight

public int getIconHeight()
Specified by:
getIconHeight in interface javax.swing.Icon

getIconWidth

public int getIconWidth()
Specified by:
getIconWidth in interface javax.swing.Icon

paintIcon

public void paintIcon(java.awt.Component c,
                      java.awt.Graphics _g,
                      int tx,
                      int ty)
Specified by:
paintIcon in interface javax.swing.Icon

getGridDrawXY2D

public java.awt.Point getGridDrawXY2D()
Returns the coordinates at which the 2D grid was last drawn


getCreature

public Creature getCreature(int x,
                            int y)
Returns the creature at x, y on the grid.


getImage

public static java.awt.Image getImage(Creature c,
                                      Direction d)

getImage

public static java.awt.Image getImage(java.lang.Class cs,
                                      Direction d)
Returns the image for this Creature subclass.


getPopulationGraph

public Simulator.PopulationGraph getPopulationGraph()