Class Field

java.lang.Object
  extended by Field

public class Field
extends Object

Represent a rectangular grid of field positions. Each position is able to store a single animal.

Version:
2011.07.31
Author:
David J. Barnes and Michael Kölling

Constructor Summary
Field(int depth, int width)
          Represent a field of the given dimensions.
 
Method Summary
 List<Location> adjacentLocations(Location location)
          Return a shuffled list of locations adjacent to the given one.
 void clear()
          Empty the field.
 void clear(Location location)
          Clear the given location.
 Location freeAdjacentLocation(Location location)
          Try to find a free location that is adjacent to the given location.
 int getDepth()
          Return the depth of the field.
 List<Location> getFreeAdjacentLocations(Location location)
          Get a shuffled list of the free adjacent locations.
 Object getObjectAt(int row, int col)
          Return the animal at the given location, if any.
 Object getObjectAt(Location location)
          Return the animal at the given location, if any.
 int getWidth()
          Return the width of the field.
 void place(Object animal, int row, int col)
          Place an animal at the given location.
 void place(Object animal, Location location)
          Place an animal at the given location.
 Location randomAdjacentLocation(Location location)
          Generate a random location that is adjacent to the given location, or is the same location.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Field

public Field(int depth,
             int width)
Represent a field of the given dimensions.

Parameters:
depth - The depth of the field.
width - The width of the field.
Method Detail

adjacentLocations

public List<Location> adjacentLocations(Location location)
Return a shuffled list of locations adjacent to the given one. The list will not include the location itself. All locations will lie within the grid.

Parameters:
location - The location from which to generate adjacencies.
Returns:
A list of locations adjacent to that given.

clear

public void clear()
Empty the field.


clear

public void clear(Location location)
Clear the given location.

Parameters:
location - The location to clear.

freeAdjacentLocation

public Location freeAdjacentLocation(Location location)
Try to find a free location that is adjacent to the given location. If there is none, return null. The returned location will be within the valid bounds of the field.

Parameters:
location - The location from which to generate an adjacency.
Returns:
A valid location within the grid area.

getDepth

public int getDepth()
Return the depth of the field.

Returns:
The depth of the field.

getFreeAdjacentLocations

public List<Location> getFreeAdjacentLocations(Location location)
Get a shuffled list of the free adjacent locations.

Parameters:
location - Get locations adjacent to this.
Returns:
A list of free adjacent locations.

getObjectAt

public Object getObjectAt(int row,
                          int col)
Return the animal at the given location, if any.

Parameters:
row - The desired row.
col - The desired column.
Returns:
The animal at the given location, or null if there is none.

getObjectAt

public Object getObjectAt(Location location)
Return the animal at the given location, if any.

Parameters:
location - Where in the field.
Returns:
The animal at the given location, or null if there is none.

getWidth

public int getWidth()
Return the width of the field.

Returns:
The width of the field.

place

public void place(Object animal,
                  int row,
                  int col)
Place an animal at the given location. If there is already an animal at the location it will be lost.

Parameters:
animal - The animal to be placed.
row - Row coordinate of the location.
col - Column coordinate of the location.

place

public void place(Object animal,
                  Location location)
Place an animal at the given location. If there is already an animal at the location it will be lost.

Parameters:
animal - The animal to be placed.
location - Where to place the animal.

randomAdjacentLocation

public Location randomAdjacentLocation(Location location)
Generate a random location that is adjacent to the given location, or is the same location. The returned location will be within the valid bounds of the field.

Parameters:
location - The location from which to generate an adjacency.
Returns:
A valid location within the grid area.