|
dLife Home Page | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectdlife.models.gridmap.GridMap
public class GridMap
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.
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.
3 3 Row Col N E S W 0 0 0 3 1 0 0 1 0 2 0 1 0 2 0 0 3 2 1 0 3 0 2 0 1 1 0 1 3 0 1 2 1 0 0 2 2 0 5 1 0 0 2 1 2 2 0 7 2 2 0 0 0 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).
| 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 |
|---|
public static final int NORTH
public static final int EAST
public static final int SOUTH
public static final int WEST
| Constructor Detail |
|---|
public GridMap(String filename)
throws IOException
filename - the name of the file containing the map data.
IOException - if the file cannot be read or has an invalid format.| Method Detail |
|---|
public int getWidth()
public int getHeight()
public int getCost(int row,
int col,
int dir)
row - the row of the location.col - the column of the location.dir - the direction to enter from (see class constants);
public int getTravelCost(int startRow,
int startCol,
int dir,
int spaces)
startRow - the starting row.startCol - the starting column.dir - the direction to travel (see class constants).spaces - the number of spaces to travel.
|
dLife Home Page | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||