|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object becker.robots.Sim becker.robots.Robot
public class Robot
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.
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 Robot s, one at a time, that are on the same
intersection
as this robot. |
IIterate<Thing> |
examineThings()
Examine all the Thing s, one at a time, that are on the same intersection
as this robot. |
IIterate<Thing> |
examineThings(IPredicate aPredicate)
Examine all the Thing s, 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 |
---|
public Robot(City aCity, int aStreet, int anAvenue, Direction aDirection)
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}.public Robot(City aCity, int aStreet, int anAvenue, Direction aDirection, int numThings)
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 |
---|
public void move()
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()
.
public void turnLeft()
Intersection
. A robot cannot
encounter any errors while turning left.
public void pickThing()
pickThing(IPredicate)
,
pickThing(Thing)
,
canPickThing()
public void putThing()
putThing(IPredicate)
,
putThing(Thing)
,
countThingsInBackpack()
public boolean canPickThing()
public boolean frontIsClear()
public int getAvenue()
public int getStreet()
public Direction getDirection()
Direction
.NORTH, SOUTH, EAST, WEST}.public int countThingsInBackpack()
countThingsInBackpack(IPredicate)
public double getSpeed()
public void setSpeed(double movesPerSecond)
movesPerSecond
- the approximate number of moves and/or turns a
robot will complete in one second. The default is 2.0 moves per second.public void setTransparency(double trans)
setTransparency
in interface IColor
trans
- The degree to which things under the robot will show through.
0.0 is fully opque; 1.0 is fully transparent.public double getTransparency()
getTransparency
in interface IColor
public String getLabel()
getLabel
in interface ILabel
public void setLabel(String theLabel)
setLabel
in interface ILabel
theLabel
- the label identifying this robotpublic Intersection getIntersection()
getIntersection
in class Sim
public void pickThing(IPredicate kindOfThing)
kindOfThing
- A predicate indicating what kind of thing should
be picked up.pickThing()
,
pickThing(Thing)
public void pickThing(Thing theThing)
theThing
- The thing to pick up.pickThing()
,
pickThing(IPredicate)
public void putThing(IPredicate kindOfThing)
kindOfThing
- A predicate indicating what kind of thing should
be put down.putThing()
,
putThing(Thing)
public void putThing(Thing theThing)
theThing
- The thing to put down.putThing()
,
putThing(IPredicate)
public String toString()
toString
in class Sim
public final IIterate<Robot> examineRobots()
Robot
s, one at a time, that are on the same
intersection
as this robot. examineRobots
returns an iterator, which may be used as follows:
Robot
can be obtained with
Robot r = this.examineRobots().next();If there are no robots on the intersection, an exception is thrown.
for(Robot r : this.examineRobots()) { // do something with r }
Iteratorrobots = this.examineRobots(); while (robots.hasNext()) { Robot r = robots.next(); // do something with r }
examineThings()
,
examineThings(IPredicate)
,
examineLights()
public final IIterate<Light> examineLights()
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:
Light
can be obtained with
Light l = this.examineLights().next();If there are no lights on the intersection, an exception is thrown.
for(Light l : this.examineLights()) { // do something with l }
Iteratorlights = this.examineLights(); while (lights.hasNext()) { Light l = lights.next(); // do something with l }
examineThings()
,
examineThings(IPredicate)
,
examineRobots()
public final IIterate<Thing> examineThings()
Thing
s, one at a time, that are on the same intersection
as this robot. examineThings
returns an iterator, which may be used as follows:
Thing
can be obtained with
Thing t = this.examineThings().next();If there are no things on the intersection, an exception is thrown.
for(Thing t : this.examineThings()) { // do something with t }
Iteratorthings = this.examineThings(); while (things.hasNext()) { Thing t = things.next(); // do something with t }
examineThings(IPredicate)
,
examineLights()
,
examineRobots()
public final IIterate<Thing> examineThings(IPredicate aPredicate)
Thing
s, 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:
Thing
can be obtained with
Thing t = this.examineThings(aPredicate).next();If there are no things matching the predicate on the intersection, an exception is thrown.
for(Thing t : this.examineThings(aPredicate)) { // do something with t }
Iteratorthings = this.examineThings(aPredicate); while (things.hasNext()) { Thing t = things.next(); // do something with t }
aPredicate
- A predicate used to test whether a thing should be
included in the iteration. Commonly used predicates are defined
in IPredicate
.
examineThings()
,
examineLights()
,
examineRobots()
public boolean isBesideThing(IPredicate kindOfThing)
kindOfThing
- A predicate indicating what kind of thing should
be looked for on the current intersection.
public int countThingsInBackpack(IPredicate kindOfThing)
kindOfThing
- The kind of thing to count.
protected void save(String indent, PrintWriter out)
indent
- A string of blanks, for formatting purposes.out
- The output stream.protected Thing makeThing(int n, int total)
n
- this will be the nth thing out of the total number to be made.total
- the total number of Things to be made.protected void breakRobot(String msg)
RobotException
is thrown.
msg
- The message for the exception.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |