-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.java
81 lines (70 loc) · 1.92 KB
/
State.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
79
80
81
import java.util.Objects;
/**
* Wrapper class representing a particular state in a 2D array.
*
* @author Daniel Bielech
*/
public class State {
private int x;
private int y;
/**
* Constructor from coordinates.
*
* @param x row
* @param y column
*/
public State(final int x, final int y) {
this.x = x;
this.y = y;
}
/**
* Constructor from state index.
*
* @param state state index
* @param gridSize numbers of cells in one row
*/
public State(final String state, final int gridSize) {
int stateIndex = Integer.parseInt(state);
int multiplier = (stateIndex - 1) / gridSize;
int modulo = (stateIndex - 1) % gridSize;
this.x = multiplier;
this.y = modulo;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getIndex() {
if (x == 0 && y == 0) return 1;
if (x == 0 && y == 1) return 2;
if (x == 0 && y == 2) return 3;
if (x == 1 && y == 0) return 4;
if (x == 1 && y == 1) return 5;
if (x == 1 && y == 2) return 6;
if (x == 2 && y == 0) return 7;
if (x == 2 && y == 1) return 8;
if (x == 2 && y == 2) return 9;
throw new RuntimeException("getIndex: Invalid State");
}
@Override
public String toString() {
return "State{" +
"x=" + x +
", y=" + y +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
State state = (State) o;
return getX() == state.getX() &&
getY() == state.getY();
}
@Override
public int hashCode() {
return Objects.hash(getX(), getY());
}
}