-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPosition.java~
More file actions
73 lines (59 loc) · 1.47 KB
/
Position.java~
File metadata and controls
73 lines (59 loc) · 1.47 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import structure5.*;
/**
* A Position is an (x,y) coordinate in the World, much like
* the Positions for the maze program.
*/
public class Position {
/** The North compass point. */
static public final int NORTH = 0;
/** The East compass point. */
static public final int EAST = 1;
/** The South compass point. */
static public final int SOUTH = 2;
/** The West compass point. */
static public final int WEST = 3;
protected int x,y;
/**
* Create a new position for the given x and y coordinates.
*/
public Position(int x, int y) {
this.x = x;
this.y = y;
}
/**
* Return the x coordinate for the position.
*/
public int getX() {
return x;
}
/**
* Return the y coordinate for the position.
*/
public int getY() {
return y;
}
public String toString() {
return "(" + x + ", " + y + ")";
}
/**
* Return a new position that is in one of the four
* compass directions from this.
* @pre direction must be NORTH, SOUTH, EAST, or WEST.
* @post the Position adjecent to this in the given direction.
*/
public Position getAdjacent(int direction) {
switch (direction) {
case NORTH:
return new Position(x, y-1);
case SOUTH:
return new Position(x, y+1);
case EAST:
return new Position(x+1, y);
case WEST:
return new Position(x-1, y);
default:
Assert.fail("bad direction");
}
return null;
}
}