Class Creature

java.lang.Object
  extended by Creature
All Implemented Interfaces:
Entity, java.lang.Runnable

public abstract class Creature
extends java.lang.Object
implements Entity, java.lang.Runnable

Extend this class to create your own Creature.

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):

Example:


public class Rover extends Creature {
    public void run() {
           while (isAlive()) {

                if (! moveForward()) {
                    attack();
                    turnLeft();
                }

            }
    }
 }

Each creature executes in its own thread. This means that you must be very careful when using static fields to always used threadsafe classes or to access them through synchronized accessor methods.

Morgan McGuire
morgan@cs.williams.edu


Constructor Summary
protected Creature()
          Subclass constructors must not invoke any of the parent class methods from their constructor.
 
Method Summary
protected  boolean attack()
          Attack the creature right in front of you.
protected  void delay(int n)
          Called by creature operations to enforce the time cost of those operations.
protected  int distance(java.awt.Point p2)
          Returns the manhattan distance from current position to p2.
static int distance(java.awt.Point p1, java.awt.Point p2)
          Returns the manhattan distance between p1 and p2
 boolean equals(java.lang.Object ob)
          Uses the pointer comparision from Object.
 java.lang.String getAuthorName()
          Allows GUI browsers to display your name as author of this creature.
 java.lang.String getClassName()
          Name of this species of creature.
 java.lang.String getDescription()
          Allows GUI browsers to display information and credits about your creature.
 Direction getDirection()
          Direction this creature is facing.
 int getId()
          Each creature has a unique number that it can use to distinguish itself from others.
 char getLabel()
           
 java.awt.Dimension getMapDimensions()
          Returns the size of the map.
protected  java.awt.Point getMovePosition()
          Same as getMovePosition(int) with the argument n = 1
protected  java.awt.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.
 java.awt.Point getPosition()
          Returns the position of this Creature.
 int getTime()
          Returns the number of time steps since the simulation started.
 Type getType()
           
 int hashCode()
          Uses the pointer hashCode from Object.
 boolean isAlive()
          Returns true if this creature is alive.
protected  boolean isEnemy(Observation obs)
          Returns true if this observation describes a Creature that is not of this species
protected  Observation look()
          Look forward.
protected  boolean moveBackward()
          Call to move your creature backward 1 square without changing its facing direction.
protected  boolean moveForward()
          Call to move your creature forward 1 square.
 Observation observeSelf()
          Create an observation describing this creature
 void run()
          Override this method to make your creature think and move.
static java.lang.String toString(java.awt.Point point)
          Prints a point to a string concisely.
protected  void turnLeft()
          Rotate counter-clockwise 90 degrees.
protected  void turnRight()
          Rotate clockwise 90 degrees.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Creature

protected Creature()
Subclass constructors must not invoke any of the parent class methods from their constructor. Instead, perform initialization at the beginning of the run() method.

Method Detail

delay

protected final void delay(int n)
Called by creature operations to enforce the time cost of those operations. Can also be called to make a creature sit still for "n time steps".


getType

public final Type getType()
Specified by:
getType in interface Entity

getTime

public final int getTime()
Returns the number of time steps since the simulation started. Creatures can call this even after they have been converted.


getClassName

public final java.lang.String getClassName()
Name of this species of creature.


getAuthorName

public java.lang.String getAuthorName()
Allows GUI browsers to display your name as author of this creature.


getDescription

public java.lang.String getDescription()
Allows GUI browsers to display information and credits about your creature.


getMapDimensions

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


getId

public final int getId()
Each creature has a unique number that it can use to distinguish itself from others. The id is not valid until run() is called on the creature; do not reference it from the constructor.


observeSelf

public Observation observeSelf()
Create an observation describing this creature


getMovePosition

protected java.awt.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. (fast)


getMovePosition

protected java.awt.Point getMovePosition()
Same as getMovePosition(int) with the argument n = 1


isEnemy

protected boolean isEnemy(Observation obs)
Returns true if this observation describes a Creature that is not of this species


distance

protected int distance(java.awt.Point p2)
Returns the manhattan distance from current position to p2.


distance

public static int distance(java.awt.Point p1,
                           java.awt.Point p2)
Returns the manhattan distance between p1 and p2


hashCode

public final int hashCode()
Uses the pointer hashCode from Object.

Overrides:
hashCode in class java.lang.Object

equals

public final boolean equals(java.lang.Object ob)
Uses the pointer comparision from Object.

Overrides:
equals in class java.lang.Object

getLabel

public final char getLabel()
Specified by:
getLabel in interface Entity

run

public void run()
Override this method to make your creature think and move. Executes as soon as the creature is injected into the world. Your creature will stop moving when the run method ends (but stay alive), so most implementations will be an intentional infinite loop. If you want to know when your creature has been converted, test isAlive or attempt an action and catch the ConvertedError.

Specified by:
run in interface java.lang.Runnable

getPosition

public final java.awt.Point getPosition()
Returns the position of this Creature. This is fast.


getDirection

public final Direction getDirection()
Direction this creature is facing. This is fast.


isAlive

public final boolean isAlive()
Returns true if this creature is alive. A creature that is not alive should allow its run method to exit.


toString

public static java.lang.String toString(java.awt.Point point)
Prints a point to a string concisely.


moveForward

protected boolean moveForward()
Call to move your creature forward 1 square. If the creature is blocked, it will not move. Moving takes about Simulator.MOVE_FORWARD_COST time steps.

Returns:
true if the creature successfully moved forward.

moveBackward

protected boolean moveBackward()
Call to move your creature backward 1 square without changing its facing direction. If the creature is blocked, it will not move. Moving takes about Simulator.MOVE_BACKWARD_COST time steps.

Returns:
true if the creature successfully moved forward.

look

protected Observation look()
Look forward. Returns a description of the first non-empty square observed, which may be any distance away.

Takes about Simulator.LOOK_COST time steps. The result of the look is accurate at the end of the delay, but of course whatever is seen might have moved by the time the creature actually makes its response.


turnLeft

protected void turnLeft()
Rotate counter-clockwise 90 degrees. This takes Simulator.TURN_COST time steps.


turnRight

protected void turnRight()
Rotate clockwise 90 degrees. This takes Simulator.TURN_COST time steps.


attack

protected boolean attack()
Attack the creature right in front of you. If there is a creature of a different species present in that spot, that creature will be destroyed and a new creature of the same type as this one will be created in its place. The new creature will face in the opposite direction as this one; i.e., they will be face to face.

Whether there is a creature present or not, this takes about Simulator.ATTACK_COST time steps. The actual attack occurs at the beginning the delay.

Returns:
true if the attack succeeded.