public abstract class Creature extends Object implements Entity, Runnable
Override the run()
method to implement the Creature's
AI. Within that method the following actions are available (each of
which takes some time steps to execute):
moveForward(int)
moveBackward(int)
turnLeft()
turnRight()
attack()
observe()
delay()
emitPheromone(java.lang.String)
Example:
public class Rover extends Creature { public void run() { while (true) { if (! moveForward()) { attack(); turnLeft(); } } } }
Morgan McGuire
morgan@cs.williams.edu
Modifier and Type | Field and Description |
---|---|
static int |
APPLE_CLASS_ID
The class ID for an Apple.
|
static int |
EMPTY_CLASS_ID
The class ID for an empty space on the map.
|
static int |
FLYTRAP_CLASS_ID
The class ID for a Flytrap.
|
static int |
HAZARD_CLASS_ID |
static Type |
THORN
Deprecated.
Old name for HAZARD
|
static int |
THORN_CLASS_ID
Deprecated.
Old name for HAZARD_CLASS_ID
|
static int |
TREASURE_CLASS_ID
The class ID for a Treasure.
|
static int |
UNINITIALIZED_CLASS_ID
An illegal, uninitialized value distinct from an empty square.
|
static int |
UNKNOWN_CREATURE_CLASS_ID
A class ID reserved for use by Creature subclasses.
|
static int |
WALL_CLASS_ID |
Modifier | Constructor and Description |
---|---|
protected |
Creature()
Subclass constructors must not invoke any of the parent class
methods from their constructor.
|
Modifier and Type | Method and Description |
---|---|
protected boolean |
attack()
Attack the creature right in front of you.
|
protected void |
delay()
Execute the delay action.
|
protected void |
delay(int n)
Execute the delay action n times in a row
|
protected int |
distance(Point p2)
Returns the Manhattan distance from current position to p2.
|
static int |
distance(Point p1,
Point p2)
Returns the Manhattan distance between p1 and p2
|
protected void |
emitPheromone(String s)
Emit a pheromone onto this creature's current location.
|
boolean |
equals(Object ob)
Uses the pointer comparision from Object.
|
abstract String |
getAuthorName()
Allows GUI browsers to display your name as author of this creature.
|
int |
getClassId()
Each creature species is assigned a unique class ID for each trial,
except Treasure, Apple, and Flytrap, which always have the same values.
|
String |
getClassName()
Name of this species of creature.
|
abstract String |
getDescription()
Allows GUI browsers to display information and credits about
your creature.
|
Direction |
getDirection()
Direction this creature is facing.
|
long |
getGameTime()
Returns the time in virtual nanoseconds since the simulation started.
|
int |
getId()
Each creature has a unique number that it can use to
distinguish itself from others.
|
char |
getLabel()
Used by Simulator for labeling the icons in 2D render mode.
|
Dimension |
getMapDimensions()
Returns the size of the map.
|
protected Point |
getMovePosition()
Same as
getMovePosition(int) with the argument n = 1 |
protected Point |
getMovePosition(int n)
The coordinates of the next position this Creature will enter
if it moves n times, regardless of whether that position is
currently empty.
|
long |
getMyTimeSinceSpawn()
The time in virtual nanoseconds that this Creature has spent
in actions and computing since it spawned.
|
protected String |
getPheromone()
Returns the pheromone at this creature's current
location.
|
Point |
getPosition()
Returns the position of this Creature.
|
long |
getTime()
Deprecated.
Use getGameTime instead
|
long |
getTurnTime()
Returns the time in virtual nanoseconds since this creature's current
turn started.
|
Type |
getType() |
int |
hashCode()
Uses the pointer hashCode from Object.
|
protected boolean |
inFog()
Returns true if this creature's current location is in
fog.
|
protected boolean |
inMud()
Returns true if this creature's current location is in mud.
|
protected boolean |
isEnchanted()
Returns true if this creature is enchanted.
|
protected boolean |
isEnemy(Observation obs)
Returns true if this observation describes a Creature that is
not of this species, including Flytrap, Apple, and Treasure.
|
protected Observation |
look()
Deprecated.
returns the first element of observe().
Provided for backwards compatibility.
|
protected boolean |
moveBackward()
Call to move your creature backward 1 square without changing
its facing direction.
|
protected boolean |
moveBackward(int n)
Execute the moveBackward action n times in a row.
|
protected boolean |
moveForward()
Call to move your creature forward 1 square.
|
protected boolean |
moveForward(int n)
Execute the moveForward action n times in a row.
|
protected int |
myClassId()
Synonym for
getClassId() to make Creature subclasses more readable. |
protected int |
myId()
Synonym for
getId() to make Creature subclasses more readable. |
protected Observation[] |
observe()
Observe all locations that are visible to this creature.
|
Observation |
observeSelf()
Create an observation describing this creature.
|
void |
onDeath()
Called by the simulator on a creature that has is about to
die, to give it an opportunity to communicate with other
members of its species before it is removed from the game.
|
abstract void |
run()
Override this method to make your creature think and move.
|
protected int |
shrineClassId()
If this creature is on a shrine, returns the classID of the
creatures that own the shrine.
|
static String |
toString(Point point)
Prints a point to a string concisely.
|
protected void |
turnLeft()
Rotate counter-clockwise 90 degrees.
|
protected void |
turnRight()
Rotate clockwise 90 degrees.
|
public static final Type THORN
public static final int UNINITIALIZED_CLASS_ID
Observation.classId
,
getClassId()
,
Constant Field Valuespublic static final int EMPTY_CLASS_ID
Observation.classId
,
getClassId()
,
Constant Field Valuespublic static final int WALL_CLASS_ID
Observation.classId
,
getClassId()
,
Constant Field Valuespublic static final int HAZARD_CLASS_ID
Observation.classId
,
getClassId()
,
Constant Field Valuespublic static final int THORN_CLASS_ID
public static final int TREASURE_CLASS_ID
Observation.classId
,
getClassId()
,
Constant Field Valuespublic static final int APPLE_CLASS_ID
Observation.classId
,
getClassId()
,
Constant Field Valuespublic static final int FLYTRAP_CLASS_ID
Observation.classId
,
getClassId()
,
Constant Field Valuespublic static final int UNKNOWN_CREATURE_CLASS_ID
Observation.classId
,
getClassId()
,
Constant Field Valuesprotected Creature()
run()
method.public final int getClassId()
Observation obs; ... if (obs.classId == getClassId()) { ... }
getClassId
in interface Entity
UNINITIALIZED_CLASS_ID
,
EMPTY_CLASS_ID
,
TREASURE_CLASS_ID
,
APPLE_CLASS_ID
,
FLYTRAP_CLASS_ID
,
UNKNOWN_CREATURE_CLASS_ID
protected final int myClassId()
getClassId()
to make Creature subclasses more readable.protected final int myId()
getId()
to make Creature subclasses more readable.public final Type getType()
getType
in interface Entity
public final long getTime()
public final long getGameTime()
public final long getMyTimeSinceSpawn()
getTime()
,
getTurnTime()
public final long getTurnTime()
getTime()
,
getMyTimeSinceSpawn()
public final String getClassName()
public abstract String getAuthorName()
public abstract String getDescription()
public void onDeath()
Called by the simulator on a creature that has is about to die, to give it an opportunity to communicate with other members of its species before it is removed from the game. The creature is still in the map and may observe itself or ask for its position or direction before it is removed. Taking any action will throw an exception and cause the method to terminate.
This is far more reliable than a finalizer, which Java is not required to call and has no explicit time at which it runs. This is run on a separate thread than the creature's usual thread, so if the creature was holding a lock this method may block. The normal time limit is enforced for the onDeath method.
public Dimension getMapDimensions()
public final int getId()
public Observation observeSelf()
protected String getPheromone()
protected boolean inFog()
protected boolean inMud()
protected boolean isEnchanted()
protected int shrineClassId()
UNINITIALIZED_CLASS_ID
protected Point getMovePosition(int n)
protected Point getMovePosition()
getMovePosition(int)
with the argument n = 1
protected boolean isEnemy(Observation obs)
Observation.className
protected int distance(Point p2)
public static int distance(Point p1, Point p2)
public final int hashCode()
public final boolean equals(Object ob)
public final char getLabel()
getLabel
in interface Entity
public abstract void run()
public final Point getPosition()
public final Direction getDirection()
protected void delay()
protected void delay(int n)
protected boolean moveForward(int n)
protected boolean moveBackward(int n)
protected boolean moveForward()
Simulator.MOVE_FORWARD_COST
nanoseconds, even if it fails.protected boolean moveBackward()
Simulator.MOVE_BACKWARD_COST
time steps, even if it fails.protected void emitPheromone(String s)
Simulator.EMIT_PHEROMONE_COST
nanoseconds.
Pheromones can be sensed with getPheromone().protected Observation look()
protected Observation[] observe()
The returned array is ordered so that the first element is
always the first non-Type.EMPTY element along the facing
direction. Note that a square with a shrine on it has Type.EMPTY
, so a shrine will never appear first in the array.
The other elements are not in any guaranteed order.
Costs Simulator.OBSERVE_COST
nanoseconds. The
result of the observe action is accurate at the
beginning of the creature's next turn, but of course
whatever is seen might have moved by the time the creature
actually makes its response.
The return value always contains at least one element.
protected void turnLeft()
Simulator.TURN_COST
nanoseconds.protected void turnRight()
Simulator.TURN_COST
nanoseconds.protected boolean attack()
Whether there is a creature present or not, this costs
Simulator.ATTACK_COST
nanoseconds.