-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocation
More file actions
135 lines (109 loc) · 2.75 KB
/
Location
File metadata and controls
135 lines (109 loc) · 2.75 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Location is an object that holds a location value, consisting of an x and y
* coordinate, to represent a place on the maze. Locations are immutable; in
* order to change a location you must simply create a new location object at
* the updated position.
*
* @author Joe Bruzek (jbruzek)
* @version 2013.10.03
*/
public class Location
{
private int x;
private int y;
/**
* Location class takes an x and y components to define the postion in the
* maze
*
* @param x
* the x coordinate of the location
* @param y
* the y coordinate of the location
*/
public Location(int x, int y)
{
this.x = x;
this.y = y;
}
/**
* equals returns true if the compared object is both a location object and
* has the same coordinates as the current location
*
* @param other
* the location being compared
* @return whether or not the locations are equal
*/
public boolean equals(Object other)
{
return ((other instanceof Location) &&
((Location)other).x() == x && ((Location)other).y() == y);
}
/**
* toString returns the location object in a neat coordinate notation like
* "(x, y)".
*
* @return the string of the location
*/
public String toString()
{
return ("(" + Integer.toString(x) + ", " + Integer.toString(y) + ")");
}
/**
* East returns a location object that is one block to the east of the
* current location
*
* @return a location object to the East
*/
public Location east()
{
return new Location(x + 1, y);
}
/**
* North returns a location object that is one block to the north of the
* current location
*
* @return a location object to the north
*/
public Location north()
{
return new Location(x, y - 1);
}
/**
* South returns a location object that is one block to the south of the
* current location
*
* @return a location object to the south
*/
public Location south()
{
return new Location(x, y + 1);
}
/**
* West returns a location object that is one block to the west of the
* current location
*
* @return a location object to the west
*/
public Location west()
{
return new Location(x - 1, y);
}
/**
* X returns the x coordinate of the current location
*
* @return the location's x coordinate
*/
public int x()
{
return x;
}
/**
* Y returns the y coordinate of the current location
*
* @return the location's y coordinate
*/
public int y()
{
return y;
}
}