-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLocation.java
More file actions
35 lines (32 loc) · 1.24 KB
/
Location.java
File metadata and controls
35 lines (32 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* A class representing a location. This class is just a container to store a row and column.
* It has two uses.
*
* First, to return a row and column from a method we need a single object that holds both.
* This object is it.
*
* Second, to provide some general helper methods having to do with direction. For example,
* if we at row 3 and column 2 and we head northeast, where are we now?
*
* This class provides methods to answer those sorts of questions.
* @author David
*/
public class Location {
public static final int NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3;
public int row, col;
public Location(int r, int c) {
row = r;
col = c;
}
public Location locationInDirection(int direction) {
return Location.locationInDirection(this, direction);
}
public static Location locationInDirection(Location loc, int direction) {
Location n = new Location(loc.row, loc.col); // don't change input!
if (direction == NORTH) n.row--;
if (direction == SOUTH) n.row++;
if (direction == EAST) n.col++;
if (direction == WEST) n.col--;
return n;
}
}