becker.robots
Class Intersection

java.lang.Object
  extended by becker.robots.Sim
      extended by becker.robots.Intersection
All Implemented Interfaces:
ILabel

public class Intersection
extends Sim
implements ILabel

Karel the Robot lives in a city composed of intersections connected by roads. Roads that run north and south (up and down) are called "Avenues" and roads that run east and west are called "Streets".

Intersections may contain Things such as Flashers, Walls or Streetlights. Some kinds of things block robots from entering or exiting an intersection. It is possible to build things which are one-way, blocking robots from entering but not exiting (or visa versa) an intersection.

Author:
Byron Weber Becker

Constructor Summary
Intersection(City city, int street, int avenue)
          Construct a new intersection.
 
Method Summary
protected  void addSim(Sim theSim)
          Add a sim to this intersection.
 int countSims(IPredicate pred)
          Determine the number of sims currently on this intersection that match the given predicate.
 int countThings()
          Determine the number of Things currently on this intersection.
protected  boolean entryIsBlocked(Direction dir)
          Determine whether something on this intersection blocks robots from entering the intersection from the given direction.
 IIterate<Light> examineLights(IPredicate aPredicate)
          Examine all the lights on this intersection that match the given predicate, one at a time.
 IIterate<Robot> examineRobots(IPredicate aPredicate)
          Examine all the robots on this intersection that match the given predicate, one at a time.
 IIterate<Thing> examineThings()
          Examine all the Things that are on this intersection, one at a time.
 IIterate<Thing> examineThings(IPredicate aPredicate)
          Examine all the things on this intersection that match the given predicate, one at a time.
protected  boolean exitIsBlocked(Direction dir)
          Determine whether something on this intersection blocks robots from exiting the intersection.
 int getAvenue()
          The avenue intersecting this intersection.
protected  Intersection getIntersection()
          Return this intersection.
 String getLabel()
          Get the label for this intersection.
 Intersection getNeighbor(Direction dir)
          The intersection neighboring this one in the given direction.
 int getStreet()
          The street intersecting this intersection.
 int hashCode()
          Used internally.
protected  void removeSim(Sim s)
          Remove the given Sim (robot, flasher, streetlight, wall, and so on) from this intersection.
protected  void save(String indent, PrintWriter out)
          Save a representation of this intersection to an output stream.
 void setLabel(String aLabel)
          Set a label for this intersection.
 String toString()
          Report the internal state of the intersection.
 
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, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Intersection

public Intersection(City city,
                    int street,
                    int avenue)
Construct a new intersection.

Parameters:
city - The city in which this intersection exists.
avenue - The intersection's avenue within the city.
street - The intersection's street within the city.
Method Detail

getNeighbor

public Intersection getNeighbor(Direction dir)
The intersection neighboring this one in the given direction. This always succeeds, even if the intersection is not displayed.

Parameters:
dir - the direction of the desired neighboring intersection.
Returns:
the neighboring intersection.

setLabel

public void setLabel(String aLabel)
Set a label for this intersection.

Specified by:
setLabel in interface ILabel
Parameters:
aLabel - the new label

getLabel

public String getLabel()
Get the label for this intersection.

Specified by:
getLabel in interface ILabel
Returns:
this intersection's label

toString

public String toString()
Report the internal state of the intersection.

Overrides:
toString in class Sim
Returns:
The internal state of the intersection.

getAvenue

public final int getAvenue()
The avenue intersecting this intersection.

Returns:
The avenue intersecting this intersection.

getStreet

public final int getStreet()
The street intersecting this intersection.

Returns:
The street intersecting this intersection.

hashCode

public int hashCode()
Used internally.

Overrides:
hashCode in class Object
Returns:
a perfect hash (for all reasonable cases) for the intersection.

getIntersection

protected Intersection getIntersection()
Return this intersection.

Specified by:
getIntersection in class Sim
Returns:
This intersection.

exitIsBlocked

protected boolean exitIsBlocked(Direction dir)
Determine whether something on this intersection blocks robots from exiting the intersection.

Parameters:
dir - the direction to check for a blockage. One of {Direction.NORTH, SOUTH, EAST, WEST}.
Returns:
True if a robot facing direction is blocked from exiting the intersection; false otherwise.

entryIsBlocked

protected boolean entryIsBlocked(Direction dir)
Determine whether something on this intersection blocks robots from entering the intersection from the given direction.

Parameters:
dir - the direction to check for a blockage. One of {Direction.NORTH, SOUTH, EAST, WEST}.
Returns:
true if a robot coming from the direction side is blocked from entering the intersection; false otherwise.

addSim

protected void addSim(Sim theSim)
Add a sim to this intersection. It is called when a robot enters the intersection, when a robot puts a thing in the intersection, or a Sim (robot, streetlight, flasher, wall...) is constructed on this intersection.

Parameters:
theSim - The sim to add.
See Also:
removeSim(becker.robots.Sim)

removeSim

protected void removeSim(Sim s)
Remove the given Sim (robot, flasher, streetlight, wall, and so on) from this intersection.

Parameters:
s - The Sim to remove. It is an error to attempt to remove a Sim which isn't on this intersection.
See Also:
addSim(becker.robots.Sim)

countThings

public int countThings()
Determine the number of Things currently on this intersection.

Returns:
The number of Things on this intersection.

countSims

public int countSims(IPredicate pred)
Determine the number of sims currently on this intersection that match the given predicate.

Parameters:
pred - the predicate that determines which sims to count
Returns:
The number of sims on this intersection.

save

protected void save(String indent,
                    PrintWriter out)
Save a representation of this intersection to an output stream. The default intersection actually does nothing. Override for intersections which need to save state. Sims on the intersection are saved separately.

Parameters:
indent - the indentation, for formatting purposes
out - the output stream

examineThings

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

Returns:
an interator of all the things on this intersection.

examineThings

public IIterate<Thing> examineThings(IPredicate aPredicate)
Examine all the things on this intersection that match the given predicate, one at a time. One Thing can be obtained with
Thing t = anIntersection.examineThings(aPredicate).next();
If there are no things matching the predicate on the intersection, an exception is thrown. All of the things on the intersection that match the predicate can be obtained, one at a time, with
for(Thing t : anIntersection.examineThings(aPredicate))
 {  // do something with t
 } 

Parameters:
aPredicate - A predicate used to test whether a thing should be included in the iteration.
Returns:
an interator of all the things on this intersection.

examineRobots

public IIterate<Robot> examineRobots(IPredicate aPredicate)
Examine all the robots on this intersection that match the given predicate, one at a time. Usage is similar to that documented in examineThings(IPredicate aPredicate).

Parameters:
aPredicate - A predicate used to test whether a robot should be included in the iteration.
Returns:
An iterator over all the robots on this intersection that match the given predicate.

examineLights

public IIterate<Light> examineLights(IPredicate aPredicate)
Examine all the lights on this intersection that match the given predicate, one at a time. Usage is similar to that documented in examineThings(IPredicate aPredicate).

Parameters:
aPredicate - A predicate used to test whether a light should be included in the iteration.
Returns:
An iterator over all the lights on this intersection that match the given predicate.