-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.java
More file actions
103 lines (89 loc) · 2.62 KB
/
State.java
File metadata and controls
103 lines (89 loc) · 2.62 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
import java.util.Arrays;
public class State {
private int tubeCount = 0;
private final Tube[] tubes;
public State(int num) {
this.tubes = new Tube[num];
}
public State(State state) {
this.tubes = new Tube[state.tubes.length];
for (Tube tube : state.tubes) {
this.addTube(tube.clone());
}
}
public State addTube(Tube tube) {
this.tubes[tubeCount++] = tube;
return this;
}
public boolean canMove(int tubeFrom, int tubeTo) {
return this.tubes[tubeFrom].canMoveTo(this.tubes[tubeTo]);
}
public State move(int tubeFrom, int tubeTo) {
State state = new State(this);
Ball ball = state.tubes[tubeFrom].pop();
state.tubes[tubeTo].push(ball);
return state;
}
@Override
public int hashCode() {
int[] hash = new int[this.tubes.length];
for (int i = 0; i < this.tubes.length; i++) {
hash[i] = this.tubes[i].size();
}
Arrays.sort(hash);
return Arrays.hashCode(hash);
}
@Override
public boolean equals(Object object) {
if (object instanceof State) {
boolean allMatchBoolean = true;
int i = 0;
while (allMatchBoolean && i < this.tubes.length) {
int j = 0;
boolean anyMatchBoolean = false;
while (!anyMatchBoolean && j < ((State) object).tubes.length) {
anyMatchBoolean = this.tubes[i].equals(((State) object).tubes[j]);
j++;
}
allMatchBoolean = anyMatchBoolean;
i++;
}
return allMatchBoolean;
} else {
return false;
}
}
public boolean validate() {
boolean allMatchBoolean = true;
int i = 0;
while (allMatchBoolean && i < this.tubes.length) {
if (this.tubes[i] != null) {
allMatchBoolean = this.tubes[i].validate();
}
i++;
}
return allMatchBoolean;
}
public boolean isComplete() {
boolean allMatchBoolean = true;
int i = 0;
while (allMatchBoolean && i < this.tubes.length) {
allMatchBoolean = this.tubes[i].isComplete();
i++;
}
return allMatchBoolean;
}
public int completeTubes() {
int count = 0;
for (Tube t : this.tubes) {
if (t.isComplete()) {
count++;
}
}
return count;
}
@Override
public String toString() {
return Arrays.toString(this.tubes);
}
}