becker.robots
Class Robot

java.lang.Object
  extended by becker.robots.Sim
      extended by becker.robots.Robot
All Implemented Interfaces:
IColor, ILabel
Direct Known Subclasses:
RobotRC, RobotSE

public class Robot
extends Sim
implements ILabel, IColor

Robots exist on a rectangular grid of roads and can move, turn left ninety degrees, pick things up, carry things, and put things down. A robot knows which avenue and street it is on and which direction it is facing. Its speed can be set and queried.

More advanced features include determining if it is safe to move forward, examining things on the same intersection as themselves and determining if they are beside a specific kind of thing. Robots can also pick up and put down specific kinds of things and determine how many things they are carrying.

Author:
Byron Weber Becker

Constructor Summary
Robot(City aCity, int aStreet, int anAvenue, Direction aDirection)
          Construct a new Robot at the given location in the given city with nothing in its backpack.
Robot(City aCity, int aStreet, int anAvenue, Direction aDirection, int numThings)
          Construct a new Robot at the given location in the given city with the given number of things in its backpack.
 
Method Summary
protected  void breakRobot(String msg)
          This method is called when the robot does something illegal such as trying to move through a wall or picking up a non-existant object.
 boolean canPickThing()
          Determine whether this robot is on the same intersection as a thing it can pick up.
 int countThingsInBackpack()
          How many things are in this robot's backpack?
 int countThingsInBackpack(IPredicate kindOfThing)
          How many of a specific kind of thing are in this robot's backpack?
 IIterate<Light> examineLights()
          Examine all the Light objects, one at a time, that are on the same intersection as this robot.
 IIterate<Robot> examineRobots()
          Examine all the other Robots, one at a time, that are on the same intersection as this robot.
 IIterate<Thing> examineThings()
          Examine all the Things, one at a time, that are on the same intersection as this robot.
 IIterate<Thing> examineThings(IPredicate aPredicate)
          Examine all the Things, one at a time, that are on the same intersection as this robot and match the provided predicate.
 boolean frontIsClear()
          Can this robot to move forward to the next intersection safely?
 int getAvenue()
          Which avenue is this robot on?
 Direction getDirection()
          Which direction is this robot facing?
 Intersection getIntersection()
          Return a reference to this robot's intersection.
 String getLabel()
          What is the string labeling this robot?
 double getSpeed()
          How many moves and/or turns does this robot complete in one second?
 int getStreet()
          Which street is this robot on?
 double getTransparency()
          Get this robot's transparency.
 boolean isBesideThing(IPredicate kindOfThing)
          Determine whether this robot is on the same intersection as one or more instances of the specified kind of thing.
protected  Thing makeThing(int n, int total)
          Make a new thing to place in the Robot's backpack.
 void move()
          Move this robot from the intersection it currently occupies to the next intersection in the direction it is currently facing, leaving it facing the same direction.
 void pickThing()
          Attempt to pick up a movable thing from the current intersection.
 void pickThing(IPredicate kindOfThing)
          Attempt to pick up a particular kind of thing from the intersection this robot currently occupies.
 void pickThing(Thing theThing)
          Attempt to pick up a particular thing from the intersection this robot currently occupies.
 void putThing()
          Take something out of the robot's backpack and put it down on the intersection this robot currently occupies.
 void putThing(IPredicate kindOfThing)
          Attempt to take a particular kind of thing out of this robot's backpack and put it on the intersection currently occupied by this robot.
 void putThing(Thing theThing)
          Attempt to put down a particular thing on the intersection this robot currently occupies.
protected  void save(String indent, PrintWriter out)
          Save a representation of this robot to an output stream.
 void setLabel(String theLabel)
          Set a label to identify this robot.
 void setSpeed(double movesPerSecond)
          Set this robot's speed.
 void setTransparency(double trans)
          Set this robot's transparency.
 String toString()
          This robot, represented as a string.
 void turnLeft()
          Turn this robot left by 90 degrees or one quarter turn.
 
Methods inherited from class becker.robots.Sim
getCity, getColor, getIcon, keyTyped, notifyObservers, notifyObservers, setColor, setIcon
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface becker.robots.IColor
getColor, setColor
 

Constructor Detail

Robot

public Robot(City aCity,
             int aStreet,
             int anAvenue,
             Direction aDirection)
Construct a new Robot at the given location in the given city with nothing in its backpack.

Parameters:
aCity - The city in which the robot will exist.
aStreet - The robot's initial street within the city.
anAvenue - The robot's initial avenue within the city.
aDirection - The robot's initial direction. One of {Direction.NORTH, EAST, SOUTH, WEST}.

Robot

public Robot(City aCity,
             int aStreet,
             int anAvenue,
             Direction aDirection,
             int numThings)
Construct a new Robot at the given location in the given city with the given number of things in its backpack. Override makeThing to customize the kind of thing added to the backpack.

Parameters:
aCity - The city in which the robot will exist.
aStreet - The robot's initial street within the city.
anAvenue - The robot's initial avenue within the city.
aDirection - The robot's initial direction. One of {Direction.NORTH, EAST, SOUTH, WEST}.
numThings - The number of things initially in the Robot's backpack.
Method Detail

move

public void move()
Move this robot from the intersection it currently occupies to the next intersection in the direction it is currently facing, leaving it facing the same direction.

It is possible that something blocks the exit of this intersection or blocks entry to the next intersection. If the robot tries to move in that direction anyway, it will break and no longer respond to commands. The robot can detect whether or not the way is clear with the command frontIsClear().


turnLeft

public void turnLeft()
Turn this robot left by 90 degrees or one quarter turn. The robot remains on the same Intersection. A robot cannot encounter any errors while turning left.


pickThing

public void pickThing()
Attempt to pick up a movable thing from the current intersection. If there are no movable things on the current intersection the robot will break and not respond to further commands.

See Also:
pickThing(IPredicate), pickThing(Thing), canPickThing()

putThing

public void putThing()
Take something out of the robot's backpack and put it down on the intersection this robot currently occupies. The robot breaks if the backpack is empty.

See Also:
putThing(IPredicate), putThing(Thing), countThingsInBackpack()

canPickThing

public boolean canPickThing()
Determine whether this robot is on the same intersection as a thing it can pick up.

Returns:
True if there is a movable thing on the same intersection as the robot; false otherwise.

frontIsClear

public boolean frontIsClear()
Can this robot to move forward to the next intersection safely?

Returns:
True if nothing blocks the robot in the direction it is currently facing; false if something blocks the way.

getAvenue

public int getAvenue()
Which avenue is this robot on?

Returns:
The avenue this robot is on.

getStreet

public int getStreet()
Which street is this robot on?

Returns:
The street this robot is on.

getDirection

public Direction getDirection()
Which direction is this robot facing?

Returns:
The direction this robot is facing. One of {Direction.NORTH, SOUTH, EAST, WEST}.

countThingsInBackpack

public int countThingsInBackpack()
How many things are in this robot's backpack?

Returns:
The number of things in this robot's backpack.
See Also:
countThingsInBackpack(IPredicate)

getSpeed

public double getSpeed()
How many moves and/or turns does this robot complete in one second?

Returns:
The number of moves/turns per second.

setSpeed

public void setSpeed(double movesPerSecond)
Set this robot's speed.

Parameters:
movesPerSecond - the approximate number of moves and/or turns a robot will complete in one second. The default is 2.0 moves per second.

setTransparency

public void setTransparency(double trans)
Set this robot's transparency. Has no effect if the robot's current icon is not an instance of IColor.

Specified by:
setTransparency in interface IColor
Parameters:
trans - The degree to which things under the robot will show through. 0.0 is fully opque; 1.0 is fully transparent.

getTransparency

public double getTransparency()
Get this robot's transparency.

Specified by:
getTransparency in interface IColor
Returns:
a number between 0.0 (fully opaque) and 1.0 (fully transparent)

getLabel

public String getLabel()
What is the string labeling this robot?

Specified by:
getLabel in interface ILabel
Returns:
the string labeling this robot

setLabel

public void setLabel(String theLabel)
Set a label to identify this robot.

Specified by:
setLabel in interface ILabel
Parameters:
theLabel - the label identifying this robot

getIntersection

public Intersection getIntersection()
Return a reference to this robot's intersection.

Specified by:
getIntersection in class Sim
Returns:
This robot's intersection.

pickThing

public void pickThing(IPredicate kindOfThing)
Attempt to pick up a particular kind of thing from the intersection this robot currently occupies. If nothing matches the kind of thing specified the robot will break. It is possible to specify things such as walls which cannot be moved. This, too, causes the robot to break. A broken robot will not respond to further commands.

Parameters:
kindOfThing - A predicate indicating what kind of thing should be picked up.
See Also:
pickThing(), pickThing(Thing)

pickThing

public void pickThing(Thing theThing)
Attempt to pick up a particular thing from the intersection this robot currently occupies. If the thing does not exist on the current intersection or if the thing cannot be moved the robot will break, refusing to respond to further commands.

Parameters:
theThing - The thing to pick up.
See Also:
pickThing(), pickThing(IPredicate)

putThing

public void putThing(IPredicate kindOfThing)
Attempt to take a particular kind of thing out of this robot's backpack and put it on the intersection currently occupied by this robot. If the backpack is already empty the robot will break and will no longer respond to commands.

Parameters:
kindOfThing - A predicate indicating what kind of thing should be put down.
See Also:
putThing(), putThing(Thing)

putThing

public void putThing(Thing theThing)
Attempt to put down a particular thing on the intersection this robot currently occupies. If the thing does not exist in the robot's backpact, the robot will break, refusing to respond to further commands.

Parameters:
theThing - The thing to put down.
See Also:
putThing(), putThing(IPredicate)

toString

public String toString()
This robot, represented as a string.

Overrides:
toString in class Sim
Returns:
A string representing this robot.

examineRobots

public final IIterate<Robot> examineRobots()
Examine all the other Robots, one at a time, that are on the same intersection as this robot. examineRobots returns an iterator, which may be used as follows:

Returns:
an interator of all the robots (other than this robot) on this intersection.
See Also:
examineThings(), examineThings(IPredicate), examineLights()

examineLights

public final IIterate<Light> examineLights()
Examine all the Light objects, one at a time, that are on the same intersection as this robot. examineLights returns an iterator, which may be used as follows:

Returns:
an interator of all the lights on this intersection.
See Also:
examineThings(), examineThings(IPredicate), examineRobots()

examineThings

public final IIterate<Thing> examineThings()
Examine all the Things, one at a time, that are on the same intersection as this robot. examineThings returns an iterator, which may be used as follows:

Returns:
an interator of all the things on this intersection.
See Also:
examineThings(IPredicate), examineLights(), examineRobots()

examineThings

public final IIterate<Thing> examineThings(IPredicate aPredicate)
Examine all the Things, one at a time, that are on the same intersection as this robot and match the provided predicate. examineThings returns an iterator, which may be used as follows:

Parameters:
aPredicate - A predicate used to test whether a thing should be included in the iteration. Commonly used predicates are defined in IPredicate.
Returns:
an interator of all the things on this intersection.
See Also:
examineThings(), examineLights(), examineRobots()

isBesideThing

public boolean isBesideThing(IPredicate kindOfThing)
Determine whether this robot is on the same intersection as one or more instances of the specified kind of thing.

Parameters:
kindOfThing - A predicate indicating what kind of thing should be looked for on the current intersection.
Returns:
True if there is a movable thing on the same intersection as the robot; false otherwise.

countThingsInBackpack

public int countThingsInBackpack(IPredicate kindOfThing)
How many of a specific kind of thing are in this robot's backpack?

Parameters:
kindOfThing - The kind of thing to count.
Returns:
The number of things of the given kind.

save

protected void save(String indent,
                    PrintWriter out)
Save a representation of this robot to an output stream.

Parameters:
indent - A string of blanks, for formatting purposes.
out - The output stream.

makeThing

protected Thing makeThing(int n,
                          int total)
Make a new thing to place in the Robot's backpack. Override this method in a subclass to control what kind of Thing is made when a robot is constructed with things in its backpack.

Parameters:
n - this will be the nth thing out of the total number to be made.
total - the total number of Things to be made.

breakRobot

protected void breakRobot(String msg)
This method is called when the robot does something illegal such as trying to move through a wall or picking up a non-existant object. Subclasses may also call this method to indicate that something has gone wrong. After breakRobot has been called, the robot does not respond to any further messages and a RobotException is thrown.

Parameters:
msg - The message for the exception.