-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
226 lines (204 loc) · 6.73 KB
/
Node.java
File metadata and controls
226 lines (204 loc) · 6.73 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import java.util.ArrayList;
import java.util.List;
public class Node implements Comparable<Node>{
public static int time = 0;
private final List<List<String>> matrix;
private String moves;
private int cost;
private int num;
private boolean isGoal;
private boolean isCutoff;
private boolean out;
private String lastStep;
private int heuristic;
private int last_x;
private int last_y;
private final int time_created;
// constructor
public Node(List<List<String>> mat) {
matrix = new ArrayList<>(mat);
moves = "";
cost = 0;
num =0;
isGoal = false;
isCutoff = false;
out = false;
lastStep = "";
heuristic = 0;
time++;
time_created = time;
}
// constructor
public Node() {
matrix = new ArrayList<>();
moves = "";
cost = 0;
num =0;
isGoal = false;
isCutoff = false;
out = false;
lastStep = "";
heuristic = 0;
time++;
time_created = time;
}
// constructor
public Node(Node n, List<List<String>> mat) {
matrix = new ArrayList<>(mat);
moves = n.moves;
cost = n.cost;
num =0;
isGoal = false;
isCutoff = false;
out = false;
lastStep = "";
heuristic = 0;
time++;
time_created = time;
}
// return the node matrix
public List<List<String>> getMatrix(){
return this.matrix;
}
// Increasing the cost and adding a step to the string after a step was made
public void add_move(int price, String step, String where, int x, int y) {
this.lastStep = where;
this.cost = cost+price;
// save the location and name of the step, so we won't do the oposite
last_x = x;
last_y = y;
if(moves.length()<2) {
moves = moves + step;
}
else {
moves = moves + "--" + step;
}
}
// checking if it is possible to do step up and return the cost of the step
public int step_up(int x, int y) {
if(this.lastStep.equals("down") && last_x==x && last_y==y) {
return 0;
}
if (this.matrix.get(x).get(y).equals("X") || this.matrix.get(x).get(y).equals("_")) {
return 0;
}
else if(this.matrix.get(x).get(y).equals("R") && this.matrix.get((x+3-1)%3).get(y).equals("_")) {
return 10;
}
else if(this.matrix.get(x).get(y).equals("G")&& this.matrix.get((x+3-1)%3).get(y).equals("_")) {
return 3;
}
else if(this.matrix.get(x).get(y).equals("B")&& this.matrix.get((x+3-1)%3).get(y).equals("_")) {
return 1;
}
return 0;
}
// checking if it is possible to do step down and return the cost of the step
public int step_down(int x, int y) {
if(this.lastStep.equals("up") && last_x==x && last_y==y) {
return 0;
}
if (this.matrix.get(x).get(y).equals("X") || this.matrix.get(x).get(y).equals("_")) {
return 0;
}
else if(this.matrix.get(x).get(y).equals("R") && this.matrix.get((x +1)%3).get(y).equals("_")) {
return 10;
}
else if(this.matrix.get(x).get(y).equals("G")&& this.matrix.get((x +1)%3).get(y).equals("_")) {
return 3;
}
else if(this.matrix.get(x).get(y).equals("B")&& this.matrix.get((x +1)%3).get(y).equals("_")) {
return 1;
}
return 0;
}
// checking if it is possible to do step right and return the cost of the step
public int step_right(int x, int y) {
if(this.lastStep.equals("left") && last_x==x && last_y==y) {
return 0;
}
if (this.matrix.get(x).get(y).equals("X") || this.matrix.get(x).get(y).equals("_")) {
return 0;
}
else if(this.matrix.get(x).get(y).equals("R") && this.matrix.get(x).get((y+1)%3).equals("_")) {
return 10;
}
else if(this.matrix.get(x).get(y).equals("G")&& this.matrix.get(x).get((y+1)%3).equals("_")) {
return 3;
}
else if(this.matrix.get(x).get(y).equals("B")&& this.matrix.get(x).get((y+1)%3).equals("_")) {
return 1;
}
return 0;
}
// checking if it is possible to do step left and return the cost of the step
public int step_left(int x, int y) {
if(this.lastStep.equals("right") && last_x==x && last_y==y) {
return 0;
}
if (this.matrix.get(x).get(y).equals("X") || this.matrix.get(x).get(y).equals("_")) {
return 0;
}
else if(this.matrix.get(x).get(y).equals("R") && this.matrix.get(x).get((y+3-1)%3).equals("_")) {
return 10;
}
else if(this.matrix.get(x).get(y).equals("G")&& this.matrix.get(x).get((y+3-1)%3).equals("_")) {
return 3;
}
else if(this.matrix.get(x).get(y).equals("B")&& this.matrix.get(x).get((y+3-1)%3).equals("_")) {
return 1;
}
return 0;
}
// check if the matrix in the node equals to the matrix the function got
public boolean is_equals(List<List<String>> other) {
return this.matrix.equals(other);
}
// when the goal was found, so we insert the value of num and change the isGoal to be true
public void found_goal(int n) {
num = n;
isGoal = true;
}
// return the moves string
public String get_moves() {
return this.moves;
}
// return the cost
public int get_cost() {
return this.cost;
}
// return the value of isCutoff
public boolean get_isCutoff() {
return isCutoff;
}
// change the value of isCutoff
public void set_isCutoff(boolean temp) {
this.isCutoff = temp;
}
// return the value of out
public boolean get_out() {
return out;
}
// mark the node an out
public void mark_out() {
this.out = true;
}
// return the f value (g+h)
public int get_f() {
return this.cost +this.heuristic;
}
@Override
public int compareTo(Node other){
// compare by f value
int ans = Integer.compare(this.get_f(), other.get_f());
// if the f values are the same so compare by the time that was created
if(ans == 0){
ans = Integer.compare(this.time_created, other.time_created);
}
return ans;
}
// set the heuristic value
public void set_h(int h) {
heuristic = h;
}
}