-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPosition.java
78 lines (63 loc) · 1.8 KB
/
Position.java
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
package model;
import java.io.Serializable;
import java.util.Objects;
public class Position implements Serializable {
private final int x;
private final int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public Position getLeft() {
return new Position(this.x - 1, this.y);
}
public Position getRight() {
return new Position(this.x + 1, this.y);
}
public Position getUp() {
return new Position(this.x, this.y - 1);
}
public Position getDown() {
return new Position(this.x, this.y + 1);
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public Position getTranslated(Position position) {
return new Position(this.getX() + position.getX(), this.getY() + position.getY());
}
public Position getRelativeTo(Position position) {
return this.getTranslated(new Position(-position.getX(), -position.getY()));
}
public Position getRandomNeighbour() {
int n = (int) (Math.random() * 4);
switch (n) {
case 0:
return this.getDown();
case 1:
return this.getRight();
case 2:
return this.getUp();
default:
return this.getLeft();
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Position position = (Position) o;
return this.x == position.x && this.y == position.y;
}
@Override
public int hashCode() {
return Objects.hash(this.x, this.y);
}
@Override
public String toString() {
return "Position(" + this.x + ", " + this.y + ")";
}
}