dLife Home Page

dlife.models.gridmap
Class GridMap

java.lang.Object
  extended by dlife.models.gridmap.GridMap

public class GridMap
extends Object

Representation of a grid based map. The map represents the world as a uniform grid of locations. The locations are indexed in row major order from (0,0) in the upper left corner to (height-1,width-1) in the lower right corner. Each location has 4 cost values that indicate the cost of entering that cell from the north, east, south and west. A cost of 0 indicates that the location cannot be entered from the corresponding direction (typically due to the presence of an obstacle). As graphical representation of a 3x3 map is shown in figure 1.


Figure 1: A 3x3 GridMap.

Data for a GridMap can be read from a text file. The text file begins with an ordered pair indicating the height and width of the map. The ordered pair is followed by a header line that is ignored. The remainder of the file is a list of white space (space or tab) delimited lines as illustrated below for the map in Figure 1.

Note that the map entries may be listed in any order. Any locations not listed in the map will be inaccessible (i.e. 0 cost in all directions).

Version:
Sep 27, 2008
Author:
Grant Braught, Dickinson College

Field Summary
static int EAST
          Constant to indicate travel to the east.
static int NORTH
          Constant to indicate travel to the north.
static int SOUTH
          Constant to indicate travel to the south.
static int WEST
          Constant to indicate travel to the west.
 
Constructor Summary
GridMap(String filename)
          Construct a new GridMap by reading data from the specified file.
 
Method Summary
 int getCost(int row, int col, int dir)
          Get the cost of entering the specified row and column from the indicated direction.
 int getHeight()
          Get the height (number of row) of this GridMap.
 int getTravelCost(int startRow, int startCol, int dir, int spaces)
          Get the total cost for traveling the specified number of spaces in the specified direction the from the position (startRow, startCol).
 int getWidth()
          Get the width (number of columns) of this GridMap.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NORTH

public static final int NORTH
Constant to indicate travel to the north.

See Also:
Constant Field Values

EAST

public static final int EAST
Constant to indicate travel to the east.

See Also:
Constant Field Values

SOUTH

public static final int SOUTH
Constant to indicate travel to the south.

See Also:
Constant Field Values

WEST

public static final int WEST
Constant to indicate travel to the west.

See Also:
Constant Field Values
Constructor Detail

GridMap

public GridMap(String filename)
        throws IOException
Construct a new GridMap by reading data from the specified file.

Parameters:
filename - the name of the file containing the map data.
Throws:
IOException - if the file cannot be read or has an invalid format.
Method Detail

getWidth

public int getWidth()
Get the width (number of columns) of this GridMap.

Returns:
the width of the map.

getHeight

public int getHeight()
Get the height (number of row) of this GridMap.

Returns:
the height of the map.

getCost

public int getCost(int row,
                   int col,
                   int dir)
Get the cost of entering the specified row and column from the indicated direction.

Parameters:
row - the row of the location.
col - the column of the location.
dir - the direction to enter from (see class constants);
Returns:
the cost of entering row,col from dir or -1 if location row,col cannot be entered from dir.

getTravelCost

public int getTravelCost(int startRow,
                         int startCol,
                         int dir,
                         int spaces)
Get the total cost for traveling the specified number of spaces in the specified direction the from the position (startRow, startCol). If it is not possible to travel the specified number of spaces in the specified direction (i.e. a wall exists) then this method returns -1.

Parameters:
startRow - the starting row.
startCol - the starting column.
dir - the direction to travel (see class constants).
spaces - the number of spaces to travel.
Returns:
the cost of the travel or -1 if the travel is not possible.

dLife Home Page