-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocation.java
More file actions
126 lines (118 loc) · 3.24 KB
/
Copy pathLocation.java
File metadata and controls
126 lines (118 loc) · 3.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
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
package model;
/**
* Model a location in a city.
*
* @author David J. Barnes and Michael Kolling. Modified ZHENG yujie, SU yu
* @version 2017.03.25
*/
public class Location
{
// (0,0)-----> x (width)
// |
// |
// y (height)
private int x;
private int y;
/**
* Model a location in the city.
* @param x The x coordinate. Must be positive.
* @param y The y coordinate. Must be positive.
* @throws IllegalArgumentException If a coordinate is negative.
*/
public Location(int x, int y)
{
if(x < 0) {
throw new IllegalArgumentException(
"Negative x-coordinate: " + x);
}
if(y < 0) {
throw new IllegalArgumentException(
"Negative y-coordinate: " + y);
}
this.x = x;
this.y = y;
}
/**
* Generate the next location to visit in order to
* reach the destination.
* @param destination Where we want to get to.
* @return The next location in a direct line from this to
* destination; or destination if already arrived.
*/
public Location nextLocation(Location destination)
{
int destX = destination.getX();
int destY = destination.getY();
int offsetX = x < destX ? 1 : x > destX ? -1 : 0;
int offsetY = y < destY ? 1 : y > destY ? -1 : 0;
if(offsetX != 0 || offsetY != 0) {
return new Location(x + offsetX, y + offsetY);
}
else {
return destination;
}
}
/**
* Determine the number of movements required to get
* from here to the destination.
* @param destination The required destination.
* @return The number of movement steps.
*/
public int distance(Location destination)
{
int xDist = Math.abs(destination.getX() - x);
int yDist = Math.abs(destination.getY() - y);
return Math.max(xDist, yDist);
}
/**
* Implement content equality for locations.
* @return true if this location matches the other,
* false otherwise.
*/
@Override
public boolean equals(Object other)
{
if(other instanceof Location) {
Location otherLocation = (Location) other;
return x == otherLocation.getX() &&
y == otherLocation.getY();
}
else {
return false;
}
}
/**
* Implement a hashcode coherent with the equals method
* @return A hashcode of this location
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int)(x ^ (x >>> 8));
result = prime * result + (int)(y ^ (y >>> 16));
return result;
}
/**
* @return A representation of the location.
*/
@Override
public String toString()
{
return "location " + x + "," + y;
}
/**
* @return The x coordinate.
*/
public int getX()
{
return x;
}
/**
* @return The y coordinate.
*/
public int getY()
{
return y;
}
}