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
-
active
- True if the robot is turned on and can receive commands.
-
beepers
- The number of beepers the robot is carrying.
-
dead
- True if the robot has run into an obstacle.
-
direction
- The direction the robot is currently facing.
-
type
- A reference to the robot's type.
-
usedVariables
- A string containg the names of all of the variables this
Robot
has requested from it's RobotType.
-
variables
- The robot's variables.
-
world
- A reference to the world that contains this robot.
-
xPos
- The robot's current position.
-
yPos
- The robot's current position.
-
Robot(String, RobotType, RobotWorld, int, int)
-
-
beepersInBag()
- Returns the number of beepers that the robot is carrying.
-
beepersOnFloor()
- Returns the number of beepers on the grid at the robot's position.
-
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.
-
buildVariableString()
- Required by
InfoTarget.
-
die()
- Sets
dead to true.
-
directionIsClear(int)
- Returns true if the given direction is clear.
-
facing()
- Returns the direction the robot is facing.
-
frontIsClear()
- Returns true if there is no obstacle in front of the robot.
-
getLocation()
- Returns the robot's current location as a
Point.
-
getVariable(String)
- Returns a reference to the variable whose name matches the given
string.
-
getX()
- Returns the x coordinate of the robot's position.
-
getY()
- Returns the y coordinate of the robot's position.
-
leftIsClear()
- Returns true if there is no obstacle to the left of the robot.
-
move(int)
- Calls
world.moveRobot,
which returns the robot's new location.
-
rightIsClear()
- Returns true if there is no obstacle to the right of the robot.
-
tell(Node)
- This is called by RobotWorld.execute() when it encounters a Tell statement.
-
turnLeft()
- Just changes the
direction field to turn the robot left.
-
turnOff()
- Sets
active to false.
-
turnOn()
- Sets
active to true if the robot is not dead.
dead
boolean dead
- True if the robot has run into an obstacle. A
dead robot
can not receive any more commands, including Turnon.
active
boolean active
- True if the robot is turned on and can receive commands.
xPos
int xPos
- The robot's current position.
yPos
int yPos
- The robot's current position.
direction
int direction
- The direction the robot is currently facing. The directions are constants
in
RobotWorld. Robots face east
initially.
beepers
int beepers
- The number of beepers the robot is carrying.
variables
SymbolTable variables
- The robot's variables.
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
type
RobotType type
- A reference to the robot's type.
world
RobotWorld world
- A reference to the world that contains this robot.
Robot
public Robot(String name,
RobotType rt,
RobotWorld rw,
int x,
int y) throws NameLengthException
beepersInBag
public int beepersInBag()
- Returns the number of beepers that the robot is carrying.
beepersOnFloor
public int beepersOnFloor()
- Returns the number of beepers on the grid at the robot's position.
Calls
RobotWorld.beepersAt().
getX
public int getX()
- Returns the x coordinate of the robot's position.
getY
public int getY()
- Returns the y coordinate of the robot's position.
facing
public int facing()
- Returns the direction the robot is facing.
frontIsClear
public int frontIsClear()
- Returns true if there is no obstacle in front of the robot.
rightIsClear
public int rightIsClear()
- Returns true if there is no obstacle to the right of the robot.
leftIsClear
public int leftIsClear()
- Returns true if there is no obstacle to the left of the robot.
die
public void die()
- Sets
dead to true.
turnOff
public void turnOff()
- Sets
active to false.
turnOn
public void turnOn()
- Sets
active to true if the robot is not dead.
getLocation
public Point getLocation()
- Returns the robot's current location as a
Point.
directionIsClear
private int directionIsClear(int dir)
- Returns true if the given direction is clear.
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.
turnLeft
public void turnLeft()
- Just changes the
direction field to turn the robot left.
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.
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.
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.
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