-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocation.java
More file actions
173 lines (163 loc) · 4.07 KB
/
Copy pathLocation.java
File metadata and controls
173 lines (163 loc) · 4.07 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Model a location in a city.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
* @version 2024.10.07 DP classes
*/
public class Location
{
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);
}
if(x > 19){
throw new IllegalArgumentException(
"Out of range x-coordinate: " + x);
}
if( y > 19){
throw new IllegalArgumentException(
"Out of range x-coordinate: " + y);
}
this.x = x;
this.y = y;
}
/**
* @return The x coordinate.
*/
public int getX()
{
return x;
}
/**
* @return The y coordinate.
*/
public int getY()
{
return y;
}
/**
* Change the value of the x coordinate.
* @param i The new x coordinate.
*/
public void setX(int i){
x = i;
}
/**
* Change the value of the y coordinate.
* @param i The new y coordinate.
*/
public void setY(int i){
y = i;
}
/**
* Generate the next location to visit in order to
* reach the destination.
* @param destination Where we want to get to.
* @return A location in a direct line from this to
* destination.
*/
public Location nextLocation(Location destination)
{
int x2 = destination.getX();
int y2 = destination.getY();
int x1 = x;
int y1 = y;
if(x2 == x1 && y2 != y1){
if(y1 < y2){
y1++;
}
else{
y1--;
}
}
else{
if(x2 != x1 && y2 == y1){
if(x1 < x2){
x1++;
}
else{
x1--;
}
}
else{
if(x2 != x1 && y2 != y1){
if (x1 < x2) {
x1++;
}
else{
x1--;
}
if(y1 < y2){
y1++;
}
else{
y1--;
}
}
}
}
Location siguiente = new Location (x1, y1);
return siguiente;
}
/**
* 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 x2 = destination.getX();
int y2 = destination.getY();
int disX = y2 - y;
int disY = x2 - x;
return Math.max(Math.abs(disX), Math.abs(disY));
}
/**
* @return A representation of the location.
*/
public String toString()
{
return "location " + x + "," + y;
}
/**
* Implement content equality for locations.
* @return true if this location matches the other, false otherwise.
*/
public boolean equals(Object other)
{
if(other instanceof Location) {
Location otherLocation = (Location) other;
return x == otherLocation.getX() &&
y == otherLocation.getY();
}
else {
return false;
}
}
/**
* Use the top 16 bits for the y value and the bottom for the x.
* Except for very big grids, this should give a unique hash code
* for each (x, y) pair.
* @return A hashcode for the location.
*/
public int hashCode()
{
return (y << 16) + x;
}
}