Class Robot

java.lang.Object
   |
   +----Symbol
           |
           +----Robot

class Robot
extends Symbol
implements InfoTarget
Robot keeps track of all of the information for one robot. It is created at the time of a New statement and destroyed at a Remove.

Author:
Peter Sagerson, petersag@wpi.edu

Variable Index

 o active
True if the robot is turned on and can receive commands.
 o beepers
The number of beepers the robot is carrying.
 o dead
True if the robot has run into an obstacle.
 o direction
The direction the robot is currently facing.
 o type
A reference to the robot's type.
 o usedVariables
A string containg the names of all of the variables this Robot has requested from it's RobotType.
 o variables
The robot's variables.
 o world
A reference to the world that contains this robot.
 o xPos
The robot's current position.
 o yPos
The robot's current position.

Constructor Index

 o Robot(String, RobotType, RobotWorld, int, int)

Method Index

 o beepersInBag()
Returns the number of beepers that the robot is carrying.
 o beepersOnFloor()
Returns the number of beepers on the grid at the robot's position.
 o buildInfoString()
Builds an info string that lists the robot name, type, location, number of beepers, whether it is turned on or not and the values of all the variables.
 o buildVariableString()
Required by InfoTarget.
 o die()
Sets dead to true.
 o directionIsClear(int)
Returns true if the given direction is clear.
 o facing()
Returns the direction the robot is facing.
 o frontIsClear()
Returns true if there is no obstacle in front of the robot.
 o getLocation()
Returns the robot's current location as a Point.
 o getVariable(String)
Returns a reference to the variable whose name matches the given string.
 o getX()
Returns the x coordinate of the robot's position.
 o getY()
Returns the y coordinate of the robot's position.
 o leftIsClear()
Returns true if there is no obstacle to the left of the robot.
 o move(int)
Calls world.moveRobot, which returns the robot's new location.
 o rightIsClear()
Returns true if there is no obstacle to the right of the robot.
 o tell(Node)
This is called by RobotWorld.execute() when it encounters a Tell statement.
 o turnLeft()
Just changes the direction field to turn the robot left.
 o turnOff()
Sets active to false.
 o turnOn()
Sets active to true if the robot is not dead.

Variables

 o dead
 boolean dead
True if the robot has run into an obstacle. A dead robot can not receive any more commands, including Turnon.

 o active
 boolean active
True if the robot is turned on and can receive commands.

 o xPos
 int xPos
The robot's current position.

 o yPos
 int yPos
The robot's current position.

 o direction
 int direction
The direction the robot is currently facing. The directions are constants in RobotWorld. Robots face east initially.

 o beepers
 int beepers
The number of beepers the robot is carrying.

 o variables
 SymbolTable variables
The robot's variables.

 o usedVariables
 String usedVariables
A string containg the names of all of the variables this Robot has requested from it's RobotType. Used in buildVariableString. Names are separated by '&', which variable symbol names can not contain.

See Also:
buildVariableString, RobotType
 o type
 RobotType type
A reference to the robot's type.

 o world
 RobotWorld world
A reference to the world that contains this robot.

Constructors

 o Robot
 public Robot(String name,
              RobotType rt,
              RobotWorld rw,
              int x,
              int y) throws NameLengthException

Methods

 o beepersInBag
 public int beepersInBag()
Returns the number of beepers that the robot is carrying.

 o beepersOnFloor
 public int beepersOnFloor()
Returns the number of beepers on the grid at the robot's position. Calls RobotWorld.beepersAt().

 o getX
 public int getX()
Returns the x coordinate of the robot's position.

 o getY
 public int getY()
Returns the y coordinate of the robot's position.

 o facing
 public int facing()
Returns the direction the robot is facing.

 o frontIsClear
 public int frontIsClear()
Returns true if there is no obstacle in front of the robot.

 o rightIsClear
 public int rightIsClear()
Returns true if there is no obstacle to the right of the robot.

 o leftIsClear
 public int leftIsClear()
Returns true if there is no obstacle to the left of the robot.

 o die
 public void die()
Sets dead to true.

 o turnOff
 public void turnOff()
Sets active to false.

 o turnOn
 public void turnOn()
Sets active to true if the robot is not dead.

 o getLocation
 public Point getLocation()
Returns the robot's current location as a Point.

 o directionIsClear
 private int directionIsClear(int dir)
Returns true if the given direction is clear.

 o getVariable
 public Variable getVariable(String name)
Returns a reference to the variable whose name matches the given string. Returns null if the variable does not exist.

Robot variables are lazy. When a robot is created, it starts out with a blank list, even though it's supposed to inherit all of the variables from its RobotType. When a variable is referenced, the robot first looks in its own list. If it's not present, it asks its RobotType to look for it. If the RobotType (and its ancestors) don't have it either, it returns null to the caller. If the RobotType does find the variable, the Robot makes a local copy for future use.

 o turnLeft
 public void turnLeft()
Just changes the direction field to turn the robot left.

 o move
 public void move(int n) throws BadCountException
Calls world.moveRobot, which returns the robot's new location. This method also calls world.paintCell to repaint the cell it now occupies.

Parameters:
n - the number of squares to move.
Throws: BadCountException
If the user tried to move the robot a negative number of cells.
Throws: EndTellError
If the robot hit an obstacle while moving.
 o tell
 public void tell(Node root)
This is called by RobotWorld.execute() when it encounters a Tell statement. This method just traverses the branch of the tree that it is given (it should start with the ASTTell node) and executes it. At the beginning, it calls world.debug() to allow the user to stop on any executable node. Conditionals and loops are processed by world.execIf(), execIterate(), etc.

 o buildInfoString
 public String buildInfoString()
Builds an info string that lists the robot name, type, location, number of beepers, whether it is turned on or not and the values of all the variables.

 o buildVariableString
 public String buildVariableString()
Required by InfoTarget. usedVariables is requred to ensure that the RobotType doesn't add any variables to the string that this Robot is going to add.

Returns:
An empty string if there are no variables in this robot or any of its superclasses.
See Also:
InfoTarget