Class SquareEnvironment

java.lang.Object
  |
  +--SquareEnvironment
All Implemented Interfaces:
Environment
Direct Known Subclasses:
BoundedEnv, UnboundedEnv

public abstract class SquareEnvironment
extends java.lang.Object
implements Environment

Marine Biology Simulation: SquareEnvironment is an abstract class that implements only the navigational methods in the Environment interface. It considers the cells in the environment to be square, with sides to the north, south, east, and west, and navigates accordingly.

See Also:
Direction, Location

Constructor Summary
SquareEnvironment()
           
 
Method Summary
 Direction getDirection(Location fromLoc, Location toLoc)
          Returns the direction from one location to another.
 Location getNeighbor(Location fromLoc, Direction compassDir)
          Returns the nearest neighbor of a location in the specified direction.
 boolean isValid(Direction dir)
          Verifies whether a direction is valid in this environment.
 java.util.ArrayList neighborsOf(Location ofLoc)
          Returns the immediately adjacent neighbors of a specified location.
 int numCellSides()
          Returns the number of sides around each cell (will not be tested on the Advanced Placement exam).
 Direction randomDirection()
          Generates a valid random direction.
 int turningAngle()
          Returns the turning angle for turning in this environment (e.g., 45, 60, 90, 120, 180) (will not be tested on the Advanced Placement exam).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface Environment
add, allObjects, isEmpty, isValid, numCols, numObjects, numRows, objectAt, recordMove, remove
 

Constructor Detail

SquareEnvironment

public SquareEnvironment()
Method Detail

numCellSides

public int numCellSides()
Returns the number of sides around each cell (will not be tested on the Advanced Placement exam).
Specified by:
numCellSides in interface Environment
Returns:
the number of cell sides in this environment

turningAngle

public int turningAngle()
Returns the turning angle for turning in this environment (e.g., 45, 60, 90, 120, 180) (will not be tested on the Advanced Placement exam). The turning angle is related to the number of sides to each cell; namely, turningAngle() * numCellSides() == 360.
Specified by:
turningAngle in interface Environment
Returns:
the turning angle in degrees

isValid

public boolean isValid(Direction dir)
Verifies whether a direction is valid in this environment.
Specified by:
isValid in interface Environment
Parameters:
dir - direction to check
Returns:
true if dir is valid; false otherwise

randomDirection

public Direction randomDirection()
Generates a valid random direction.
Specified by:
randomDirection in interface Environment
Returns:
a valid direction

getNeighbor

public Location getNeighbor(Location fromLoc,
                            Direction compassDir)
Returns the nearest neighbor of a location in the specified direction. (Precondition: isValid(compassDir).)
Specified by:
getNeighbor in interface Environment
Parameters:
fromLoc - starting location for search
compassDir - direction in which to look for nearest neighbor
Returns:
neighbor of fromLoc in given direction

getDirection

public Direction getDirection(Location fromLoc,
                              Location toLoc)
Returns the direction from one location to another. If toLoc is not a "straight shot" from fromLoc, then the direction will be rounded to a valid direction in this environment. If fromLoc and toLoc are the same, getDirection arbitrarily returns Direction.NORTH. The table below shows examples.
 
    fromLoc    toLoc    value returned by getDirection(fromLoc, toLoc)
    -------    -----    ----------------------------------------------
     (1, 1)    (0, 1)   Direction.NORTH
     (1, 1)    (1, 2)   Direction.EAST
     (1, 1)    (2, 6)   Direction.EAST (slightly south, mostly east)
     (0, 0)    (1, 1)   Direction.SOUTH (southeast, but rounds to south)
     (1, 1)    (1, 1)   Direction.NORTH
  

One can use directions returned by getDirection to move from one location to another in an environment, even if the direction is not a "straight shot." For example, getDirection from (0, 0) to (1, 1) returns Direction.SOUTH. Moving one cell south means going to location (1, 0). A second call to getDirection, from (1, 0) to (1, 1) returns Direction.EAST. Moving one cell east leads to the destination, (1, 1). Similarly, the path from (1, 1) to (2, 6), following repeated calls to getDirection from the intermediate cells, is [(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 5), (2, 6)].

Specified by:
getDirection in interface Environment
Parameters:
fromLoc - starting location for search
toLoc - destination location
Returns:
direction from fromLoc to toLoc, rounded if necessary

neighborsOf

public java.util.ArrayList neighborsOf(Location ofLoc)
Returns the immediately adjacent neighbors of a specified location. Only neighbors that are valid locations in the environment will be included.
Specified by:
neighborsOf in interface Environment
Parameters:
ofLoc - location whose neighbors to get
Returns:
a list of locations that are neighbors of ofLoc