becker.robots
Class City

java.lang.Object
  extended by becker.robots.City
Direct Known Subclasses:
MazeCity

public class City
extends Object

A city contains intersections joined by streets and avenues. Intersections may contain Things such as walls, streetlights, flashers, and robots.

Author:
Byron Weber Becker

Constructor Summary
City()
          Construct a new city using the defaults stored in the becker.robots.ini initialization file.
City(int numVisibleStreets, int numVisibleAvenues)
          Construct a new City which displays streets 0 through numVisibleStreets-1 and avenues 0 through numVisibleAvenues-1.
City(int firstVisibleStreet, int firstVisibleAvenue, int numVisibleStreets, int numVisibleAvenues)
          Construct a new City which displays streets firstVisibleStreet through numVisibleStreets-1 and avenues firstVisibleAvenue through numVisibleAvenues-1.
City(int firstVisibleStreet, int firstVisibleAvenue, int numVisibleStreets, int numVisibleAvenues, int intersectionSize)
          Construct a new City.
City(Scanner in)
          Construct a new city by reading information to construct it from a file.
City(String fileName)
          Construct a new city by reading information to construct it from a file.
 
Method Summary
protected  void customizeIntersection(Intersection intersection)
          Customize an intersection, perhaps by adding Things to it.
 IIterate<Light> examineLights()
          Examine all the Light objects in this city, one at a time.
 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 in this City, one at a time.
 IIterate<Thing> examineThings(IPredicate aPredicate)
          Examine all the Things, one at a time, that are in this city and match the provided predicate.
protected  Intersection getIntersection(int street, int avenue)
          Obtain a reference to a specified intersection within this city.
 IPredicate getThingCountPredicate()
          Get the predicate used to show counts on the city's intersections.
 boolean isShowingThingCounts()
          Is the city showing the number of Things and Robots on each intersection?
protected  void keyTyped(char key)
          This method is called when the city's display has the focus and a key is typed.
protected  Intersection makeIntersection(int street, int avenue)
          Make an intersection which will appear at the specified avenue and street.
 void save(String indent, PrintWriter out)
          Save a representation of this city to a file for later use.
 void setFrameTitle(String title)
          Sets the title of the implicitly created frame, if there is one.
 void setSize(int width, int height)
          Set the size of the implicitly created frame, if there is one.
 void setThingCountPredicate(IPredicate pred)
          Set the predicate for what kinds of things to count when showing the number of things on each intersection.
static void showFrame(boolean show)
          Should the city be shown in a frame? The default is to show it.
 void showThingCounts(boolean show)
          Show the number of Things and Robots on each intersection, counted according to the predicate set with setThingCountPredicate(becker.robots.IPredicate).
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

City

public City()
Construct a new city using the defaults stored in the becker.robots.ini initialization file.


City

public City(int numVisibleStreets,
            int numVisibleAvenues)
Construct a new City which displays streets 0 through numVisibleStreets-1 and avenues 0 through numVisibleAvenues-1.

Parameters:
numVisibleStreets - The number of streets to display.
numVisibleAvenues - The number of avenues to display.

City

public City(int firstVisibleStreet,
            int firstVisibleAvenue,
            int numVisibleStreets,
            int numVisibleAvenues)
Construct a new City which displays streets firstVisibleStreet through numVisibleStreets-1 and avenues firstVisibleAvenue through numVisibleAvenues-1.

Parameters:
firstVisibleStreet - The street of the upper left intersection.
firstVisibleAvenue - The avenue of the upper left intersection.
numVisibleStreets - The number of streets to display
numVisibleAvenues - The number of avenues to display

City

public City(int firstVisibleStreet,
            int firstVisibleAvenue,
            int numVisibleStreets,
            int numVisibleAvenues,
            int intersectionSize)
Construct a new City. By default, an implicit frame is created which displays the intersection at firstVisibleStreet and firstVisibleAvenue in the upper left corner of the City. numVisibleStreet streets and numVisibleAvenues avenues are displayed below and to the right. Use showFrame(boolean) to avoid creating the implicit frame.

Parameters:
firstVisibleStreet - The street of the upper left intersection.
firstVisibleAvenue - The avenue of the upper left intersection.
numVisibleStreets - The number of streets to display
numVisibleAvenues - The number of avenues to display
intersectionSize - The number of pixels each intersection takes up

City

public City(String fileName)
Construct a new city by reading information to construct it from a file. The file format is documented in City(Scanner). If fileName does not exist, display a file chooser to allow the user to choose a file.

Parameters:
fileName - the name of the file containing the information to build this city.

City

public City(Scanner in)
Construct a new city by reading information to construct it from a file. The file format is the same as that produced by using the "Save" command in a running program. The first lines in the file must contain the following information in the order given. Comments and blank lines (see below) may be interspersed between these three lines.
  1. A string, to be used as the window's title.
  2. Four integers: first Street, first Avenue, number of visible streets, number of visible avenues
  3. One integer: the size of the intersections, in pixels
Each of the remaining lines in the file must have one of three formats:

For example,

# A city with one robot, one thing and one wall.
# Window title
Clear snow
# first street, first avenue, num streets, num avenues
0 0 5 9
# intersection size
38

becker.robots.Robot 1 2 Direction.EAST 
becker.robots.Thing 2 2
becker.robots.Wall 2 2 Direction.EAST
    

Parameters:
in - the open file containing the information to build this city.
Method Detail

showFrame

public static void showFrame(boolean show)
Should the city be shown in a frame? The default is to show it. Not showing it is useful if for automated testing and if you want to provide your own controls for the window. See RobotUIComponents. This method, if used, must be called before the City object is constructed.

Parameters:
show - True to show the city inside a frame; false otherwise.

showThingCounts

public void showThingCounts(boolean show)
Show the number of Things and Robots on each intersection, counted according to the predicate set with setThingCountPredicate(becker.robots.IPredicate). The default predicate counts things that can be moved by a robot.

Parameters:
show - true if the number of things should be shown; false otherwise

isShowingThingCounts

public boolean isShowingThingCounts()
Is the city showing the number of Things and Robots on each intersection?

Returns:
true if the city is showing counts; false otherwise

setThingCountPredicate

public void setThingCountPredicate(IPredicate pred)
Set the predicate for what kinds of things to count when showing the number of things on each intersection. The default counts the number of things that can be moved by a robot.

Parameters:
pred - A predicate that returns true if the Thing/Robot should be counted, false otherwise.

getThingCountPredicate

public IPredicate getThingCountPredicate()
Get the predicate used to show counts on the city's intersections.

Returns:
The current predicate; null if none is in use.

setFrameTitle

public void setFrameTitle(String title)
Sets the title of the implicitly created frame, if there is one.

Parameters:
title - The frame's title

setSize

public void setSize(int width,
                    int height)
Set the size of the implicitly created frame, if there is one.

Parameters:
width - the width of the frame, in pixels
height - the height of the frame, in pixels

getIntersection

protected Intersection getIntersection(int street,
                                       int avenue)
Obtain a reference to a specified intersection within this city.

Parameters:
street - the street of the desired intersection
avenue - the avenue of the desired intersection
Returns:
the specified intersection.

makeIntersection

protected Intersection makeIntersection(int street,
                                        int avenue)
Make an intersection which will appear at the specified avenue and street. This method simply instantiates an Intersection object by default. Cities which use a subclass of Intersection should override the method to instantiate the desired subclass.

Caution: Do not add Things such as Walls or Flashers to the intersection before returning it; doing so results in infinite recursion. Instead, override customizeIntersection.

Parameters:
avenue - the avenue where the intersection will appear within the city.
street - the street where the intersection will appear within the city.
Returns:
an Intersection.

customizeIntersection

protected void customizeIntersection(Intersection intersection)
Customize an intersection, perhaps by adding Things to it. For instance, a city which has a wall on the north edge can be implemented by extending City and overriding customizeIntersection as follows:
  protected void customizeIntersection(Intersection intersection)
  {  if (intersection.getStreet() == 0)
     {  new Wall(this, intersection.getAvenue(), 0, Direction.NORTH);
     }
  }

Parameters:
intersection - the intersection to be customized.

keyTyped

protected void keyTyped(char key)
This method is called when the city's display has the focus and a key is typed.

Parameters:
key - The character typed at the keyboard.

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 in this city, one at a time. 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 in this City, one at a time. 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 in this city 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()

save

public void save(String indent,
                 PrintWriter out)
Save a representation of this city to a file for later use.

Parameters:
indent - A string of spaces prepended to each line of output.
out - an open output stream.

toString

public String toString()
Overrides:
toString in class Object