|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectSimulator
public final class Simulator
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:
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 |
---|
public static final java.lang.Class[] sharedClasses
public static final int LOOK_COST
public static final int ATTACK_COST
public static final int TURN_COST
public static final int MOVE_FORWARD_COST
public static final int MOVE_BACKWARD_COST
public static final int TIME_STEP_LIMIT
public static final int SCALE2D
Constructor Detail |
---|
public Simulator(java.lang.String mapFilename, java.lang.Class[] creatures)
Method Detail |
---|
public static java.lang.Class loadClass(java.lang.String name) throws java.lang.ClassNotFoundException, java.io.IOException
java.lang.ClassNotFoundException
java.io.IOException
public static java.lang.String getVersion()
public void start(java.lang.String mapfile, java.lang.Class[] creatures)
public void stop()
public boolean isRunning()
public java.lang.String getTitle()
public int getTime()
public java.awt.Dimension getDimensions()
public void delay(int n)
public static void nanoSleep(long t) throws java.lang.InterruptedException
java.lang.InterruptedException
public long getDelayTime()
public void setDelayTime(long t)
public void setView3D(boolean b)
public boolean getView3D()
public java.lang.String toString()
toString
in class java.lang.Object
public void turnRight(Creature e)
public void turnLeft(Creature e)
public Observation look(Creature e)
public boolean attack(Creature e)
public int getNumSpeciesLeft()
public int getCreatureCount(java.lang.Class c)
public Simulator.Result getResult()
public boolean isAlive(Creature c)
All creatures are considered dead once the simulator is stopped.
public java.awt.Point getPosition(Creature e)
public Direction getDirection(Creature e)
public boolean move(Creature e, int distance)
Moving into a Thorn causes a Creature to be converted into an Apple. Throws ConvertedError if the Creature is not in the world.
distance
- must be -1 or +1
public void setSelectedCreature(Creature c)
public int getIconHeight()
getIconHeight
in interface javax.swing.Icon
public int getIconWidth()
getIconWidth
in interface javax.swing.Icon
public void paintIcon(java.awt.Component c, java.awt.Graphics _g, int tx, int ty)
paintIcon
in interface javax.swing.Icon
public java.awt.Point getGridDrawXY2D()
public Creature getCreature(int x, int y)
public static java.awt.Image getImage(Creature c, Direction d)
public static java.awt.Image getImage(java.lang.Class cs, Direction d)
public Simulator.PopulationGraph getPopulationGraph()
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |