Class RobotWorld
java.lang.Object
|
+----RobotWorld
- public class RobotWorld
- extends Object
- implements Runnable
This is the "overlord" of the engine. It contains the main symbol
tables and a two-dimensional array of Cells. It has routines for
all of the top-level statements as well as the main tree-traversal/
execution routine. It also evaluates all expressions, inside and
outside of a robot. It has a pointer to an applet which handles
all the display.
- Author:
- Peter Sagerson, petersag@wpi.edu
- See Also:
- Cell, Robot
-
applet
- The applet that this object will report to.
-
delay
- The time (in milliseconds) to delay between robot actions.
-
EAST
- This value must NOT change (see
Robot.turnLeft())
-
execute
- When the user just wants to parse the program, this is set to true
before executing.
-
fDebug
- When true, the node being executed will be displayed during normal
running conditions.
-
firstExecution
- A cheesy boolean so that we only set the world size the first time through.
-
interrupted
- When true, the execution thread will pause between three nodes
until it is notified.
-
kennyFrame
-
-
lastCode
- The last string to be parsed.
-
lastRoot
- The AST associated with
lastCode.
-
mapSize
- The length of one side of the map in grid squares.
-
NORTH
- This value must NOT change (see
Robot.turnLeft())
-
parser
- A reference to the parser created by
RobotlApplet.
-
robots
- The list of existing
Robots.
-
robotTypes
- The list of existing
RobotTypes.
-
SOUTH
- This value must NOT change (see
Robot.turnLeft())
-
variables
- The list of existing global variables.
-
WEST
- This value must NOT change (see
Robot.turnLeft())
-
world
- Keeps track of the contents of each individual grid square.
-
worldEntities
- The list of existing WorldEntities.
-
RobotWorld(RobotlApplet)
- The one and only constructor.
-
addRobot(Robot, int, int)
- Adds an existing robot to the world.
-
beepersAt(int, int)
- Returns the number of beepers at the given location.
-
debug(SimpleNode)
- This is called before the execution of every significant node.
-
error(String, int)
- Posts a message to the user (with postMessage) and then halts
execution.
-
eval(Node, Robot)
- Evaluates all expressions, inside and outside of a Tell.
-
execAssignment(Node, Robot)
- Handles an assignment, inside or outside of a Tell.
-
execDoWhile(Node, Robot)
- Handles a Do-While loop, inside or outside of a Tell.
-
execIf(Node, Robot)
- Handles an If conditional, inside or outside of a Tell.
-
execIterate(Node, Robot)
- Handles an Iterate loop, inside or outside of a Tell.
-
execute(Node)
- Executes a branch of the AST.
-
execWhile(Node, Robot)
- Handles a While loop, inside or outside of a Tell.
-
getCell(int, int)
- Returns the cell at the given coordinates.
-
getGlobals()
- Returns an enumeration containing all of the global variables.
-
getMapSize()
- Returns length of one side of the world, in grid squares.
-
getRobot(String)
- Returns a
Robot reference for the given name.
-
getWorldArray()
- Returns the array of
Cells.
-
interrupt()
- Pauses or resumes execution, depending on the current state.
-
moveRobot(Robot, int, int)
- Moves the given robot the given number of spaces in the given direction.
-
paintCell(int, int)
- Asks the applet to paint a single cell, specified in grid
coordinates.
-
parse()
- Initializes the parser and parses the code.
-
pause()
- Pauses the execution thread for the amount of time specified
in the
delay field.
-
pickBeeper(int, int)
- Tells the world that a robot is trying to pick up a beeper at the given
location.
-
placeWorldEntity(WorldEntity, int, int)
- Called for the RoBOTL "place" command.
-
postMessage(String, int)
- An interface to the applet's postMessage method.
-
putBeeper(int, int)
- Tells the world that a robot is putting down a beeper at the given
location.
-
reInit(boolean, boolean)
- Sets up to run a new program.
-
removeRobot(String)
- Removes a robot from the world.
-
run()
- The required run method that parses and executes and handles the
exceptions.
-
setDebug(boolean)
- Sets the
fDebug field.
-
setDelay(int)
- Sets the delay field.
-
spaceIsClear(int, int)
- Returns 1 if the given space does not have a robot or a wall.
-
step()
- Pauses execution (if it's already paused, there's no change) and signals
the execution thread to proceed.
-
updateRobotFeedback(Robot)
- A wrapper for
RobotlApplet.updateRobotFeedback().
-
warning(String, int)
- Posts a message to the user (with postMessage) and then continues
with execution.
world
Cell world[][]
- Keeps track of the contents of each individual grid square.
applet
RobotlApplet applet
- The applet that this object will report to.
robots
SymbolTable robots
- The list of existing
Robots.
robotTypes
SymbolTable robotTypes
- The list of existing
RobotTypes.
worldEntities
SymbolTable worldEntities
- The list of existing WorldEntities.
variables
SymbolTable variables
- The list of existing global variables.
parser
RoBOTL parser
- A reference to the parser created by
RobotlApplet.
mapSize
int mapSize
- The length of one side of the map in grid squares.
delay
int delay
- The time (in milliseconds) to delay between robot actions.
lastCode
String lastCode
- The last string to be parsed.
lastRoot
ASTStart lastRoot
- The AST associated with
lastCode.
execute
boolean execute
- When the user just wants to parse the program, this is set to true
before executing. It is set in
reInit().
interrupted
boolean interrupted
- When true, the execution thread will pause between three nodes
until it is notified.
fDebug
public boolean fDebug
- When true, the node being executed will be displayed during normal
running conditions. The applicable line will also be highlighted.
firstExecution
private boolean firstExecution
- A cheesy boolean so that we only set the world size the first time through.
NORTH
public static final int NORTH
- This value must NOT change (see
Robot.turnLeft())
EAST
public static final int EAST
- This value must NOT change (see
Robot.turnLeft())
SOUTH
public static final int SOUTH
- This value must NOT change (see
Robot.turnLeft())
WEST
public static final int WEST
- This value must NOT change (see
Robot.turnLeft())
kennyFrame
ImageFrame kennyFrame
RobotWorld
public RobotWorld(RobotlApplet inApplet)
- The one and only constructor. Calls
reInit().
- Parameters:
- inApplet - the applet that this
RobotWorld reports to
reInit
public void reInit(boolean inExecute,
boolean inStepping)
- Sets up to run a new program. Creates blank symbol tables, defines
basic_bot and a few other miscellaneous housekeeping tasks. This must
be called between calls to
execute().
- Parameters:
- inStepping - true if execution should be immediately paused.
getWorldArray
public Cell[][] getWorldArray()
- Returns the array of
Cells. RobotlCanvas
calls this in order to update the screen.
getCell
public Cell getCell(int x,
int y) throws ArrayIndexOutOfBoundsException
- Returns the cell at the given coordinates.
- Returns:
- null if the array of cells hasn't been created yet.
- Throws: ArrayIndexOutOfBoundsException
- if the coordinates are out of range.
getMapSize
public int getMapSize()
- Returns length of one side of the world, in grid squares.
The applet uses this to calculate the size of a grid square
in pixels.
getGlobals
public Enumeration getGlobals()
- Returns an enumeration containing all of the global variables.
beepersAt
public int beepersAt(int x,
int y)
- Returns the number of beepers at the given location.
spaceIsClear
public int spaceIsClear(int x,
int y)
- Returns 1 if the given space does not have a robot or a wall.
Ultimately called from
eval().
setDelay
public void setDelay(int d)
- Sets the delay field. The applet calls this at each execution
and sets it based on the value in the "delay" TextField.
- Parameters:
- d - the delay in milliseconds
setDebug
public void setDebug(boolean b)
- Sets the
fDebug field.
paintCell
public void paintCell(int x,
int y)
- Asks the applet to paint a single cell, specified in grid
coordinates.
step
public synchronized void step()
- Pauses execution (if it's already paused, there's no change) and signals
the execution thread to proceed. It will execute one node and
then pause again in
debug().
interrupt
public synchronized boolean interrupt()
- Pauses or resumes execution, depending on the current state.
Returns true if execution is paused after the function exits.
debug
public synchronized void debug(SimpleNode node)
- This is called before the execution of every significant node.
If debug output is enabled or execution is paused, it prints out
the next node to be executed. If
interrupt is true,
this function pauses the current thread indefinitely (i.e. until notified).
- Parameters:
- node - the node that is about to be executed.
pause
public synchronized void pause()
- Pauses the execution thread for the amount of time specified
in the
delay field.
- See Also:
- delay
getRobot
public Robot getRobot(String name) throws NameNotFoundException
- Returns a
Robot reference for the given name.
- Parameters:
- name - the name of the robot.
- Throws: s
- NameNotFoundException if the robot does not exist.
placeWorldEntity
public void placeWorldEntity(WorldEntity we,
int x,
int y) throws BadCoordException
- Called for the RoBOTL "place" command. Adds the contents of
the WorldEntity to the world at the given coordinates, with
the following first-come, first-serve rules:
1. Nothing will be placed in an occupied cell.
2. A block will not replace a pile of beepers.
An occupied cell is one that contains a wall or a robot.
Beepers placed on existing beepers will simply increase the
number of beepers in that cell. Anything that is off the
map will be ignored.
- Parameters:
- we - the WorldEntity to place
- Throws: BadCoordException
- if
x or y
are out of bounds.
addRobot
public void addRobot(Robot bot,
int x,
int y) throws ObstacleException, DuplicateNameException, BadCoordException
- Adds an existing robot to the world.
- Parameters:
- bot - the robot to be placed
- Throws: ObstacleException
- if the robot collides with another object.
removeRobot
public Robot removeRobot(String name) throws NameNotFoundException
- Removes a robot from the world.
- Parameters:
- name - the name of the robot to be removed.
moveRobot
public Point moveRobot(Robot r,
int direction,
int n) throws BadCountException
- Moves the given robot the given number of spaces in the given direction.
- Parameters:
- r - the robot to be moved.
- direction - the direction to move it
- n - the number of spaces to move it
- Throws: ObstacleException
- if it runs into a wall or another robot.
pickBeeper
void pickBeeper(int x,
int y) throws BadCountException
- Tells the world that a robot is trying to pick up a beeper at the given
location.
- Throws: BadCountException
- Thrown if there is no beeper to pick up at (x, y).
putBeeper
void putBeeper(int x,
int y)
- Tells the world that a robot is putting down a beeper at the given
location. Can't fail.
updateRobotFeedback
void updateRobotFeedback(Robot r)
- A wrapper for
RobotlApplet.updateRobotFeedback(). Called from
Robot.tell().
execAssignment
void execAssignment(Node node,
Robot robot) throws NameNotFoundException
- Handles an assignment, inside or outside of a Tell.
- Parameters:
- node - the root of the assignment branch
- robot - null if this assignment is outside of a Tell
execIf
void execIf(Node node,
Robot robot)
- Handles an If conditional, inside or outside of a Tell.
- Parameters:
- node - the root of the If branch
- robot - null if this If is outside of a Tell.
execIterate
void execIterate(Node node,
Robot robot)
- Handles an Iterate loop, inside or outside of a Tell.
- Parameters:
- node - the root of the Iterate branch
- robot - null if this Iterate is outside of a Tell.
execWhile
void execWhile(Node node,
Robot robot)
- Handles a While loop, inside or outside of a Tell.
- Parameters:
- node - the root of the While branch
- robot - null if this While is outside of a Tell.
execDoWhile
void execDoWhile(Node node,
Robot robot)
- Handles a Do-While loop, inside or outside of a Tell.
- Parameters:
- node - the root of the Do-While branch
- robot - null if this Do-While is outside of a Tell.
execute
public void execute(Node root)
- Executes a branch of the AST. This is the main execution loop, but
it is called recursively.
- Parameters:
- root - the root of the branch to execute
eval
public int eval(Node root,
Robot robot)
- Evaluates all expressions, inside and outside of a Tell.
- Parameters:
- root - the root of the expression branch
- robot - null if the expression is outside of a Tell.
parse
private ASTStart parse() throws ParseException, TokenMgrError
- Initializes the parser and parses the code. Returns the root of the
resulting abstract syntax tree.
run
public void run()
- The required run method that parses and executes and handles the
exceptions.
postMessage
public void postMessage(String msg,
int line)
- An interface to the applet's postMessage method. Adds the line
number and a newline to the end and tells the applet to select
the given line in the code.
- Parameters:
- msg - The basic message
- line - The line of code associated with the message
warning
public void warning(String msg,
int line)
- Posts a message to the user (with postMessage) and then continues
with execution. Used to display non-fatal errors.
- Parameters:
- msg - the basic message to display
- line - the line number associated with the message.
-1 indicates that there is no associated line.
error
public void error(String msg,
int line)
- Posts a message to the user (with postMessage) and then halts
execution. Used to display fatal errors.
- Parameters:
- msg - the basic message to display
- line - the line number associated with the message.
-1 indicates that there is no associated line.