-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis17217679.java
374 lines (313 loc) · 11.6 KB
/
is17217679.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import java.util.ArrayList;
import javax.swing.JOptionPane;
/* Damian Skrzypek - 17217679
* Darragh Kelly - 17235545
* Eoghan Russell - 17202124
* Pawel Ostach - 17214211
*/
public class is17217679
{
static class State
{
private static int[][] endState;
private int[][] board;
private int gValue;
private int hValue;
private int fValue;
private State previousState;
State(int[][] b, int g, State previous)
{
board = b;
gValue = g;
hValue = calculateH();
fValue = gValue + hValue;
previousState = previous;
}
// Calculates the 'H' value
private int calculateH()
{
int counter = 0;
for(int i = 0; i < board.length; i++)
for(int j = 0; j < board.length; j++)
for(int x = 0; x < board.length; x++)
for(int y = 0; y < board.length; y++)
if(board[i][j] == endState[x][y] && board[i][j] != 0)
{
counter += Math.abs(i - x) + Math.abs(j - y);
x = board.length;
y = board.length;
}
return counter;
}
public ArrayList<State> getPossibleMoves()
{
ArrayList<State> moves = new ArrayList<>();
boolean stop = false;
int i = 0, j = 0;
for(i = 0; i < board.length && !stop; i++)
for(j = 0; j < board[i].length && !stop; j++)
stop = (board[i][j] == 0);
i--;
j--;
int[][] tempBoard = new int[board.length][board[0].length];
copy2DArray(tempBoard);
if(i - 1 > -1)
{
tempBoard[i-1][j] = 0;
tempBoard[i][j] = board[i-1][j];
moves.add(new State(tempBoard, gValue + 1, this));
tempBoard = new int[board.length][board[0].length];
copy2DArray(tempBoard);
}
if(i + 1 < board.length)
{
tempBoard[i+1][j] = 0;
tempBoard[i][j] = board[i+1][j];
moves.add(new State(tempBoard, gValue + 1, this));
tempBoard = new int[board.length][board[0].length];
copy2DArray(tempBoard);
}
if(j - 1 > -1)
{
tempBoard[i][j-1] = 0;
tempBoard[i][j] = board[i][j-1];
moves.add(new State(tempBoard, gValue + 1, this));
tempBoard = new int[board.length][board[0].length];
copy2DArray(tempBoard);
}
if(j + 1 < board.length)
{
tempBoard[i][j+1] = 0;
tempBoard[i][j] = board[i][j+1];
moves.add(new State(tempBoard, gValue + 1, this));
tempBoard = new int[board.length][board[0].length];
copy2DArray(tempBoard);
}
return moves;
}
private void copy2DArray(int[][] ar)
{
for(int i = 0; i < board.length; i++)
for(int j = 0; j < board[i].length; j++)
ar[i][j] = board[i][j];
}
// Prints out possible moves, max 4
public void printPossibleMovements()
{
boolean stop = false;
int i = 0, j = 0;
for(i = 0; i < board.length && !stop; i++)
for(j = 0; j < board[i].length && !stop; j++)
stop = (board[i][j] == 0);
i--;
j--;
if(i - 1 > -1)
System.out.println("-> " + board[i-1][j] + " to the south");
if(i + 1 < board.length)
System.out.println("-> " + board[i+1][j] + " to the north");
if(j - 1 > -1)
System.out.println("-> " + board[i][j - 1] + " to the east");
if(j + 1 < board.length)
System.out.println("-> " + board[i][j + 1] + " to the west");
System.out.println("Value of h: " + hValue + "\n");
}
// Returns the board state as a string
public String toString()
{
String temp = "";
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++)
temp += board[i][j] + " ";
temp += "\n";
}
return temp;
}
public boolean isEqual(State s)
{
int[][] temp = s.getBoard();
for(int i = 0; i < temp.length; i++)
for(int j = 0; j < temp[i].length; j++)
if(board[i][j] != temp[i][j])
return false;
return true;
}
public static void printEndState()
{
for(int i = 0; i < endState.length; i++)
{
for(int j = 0; j < endState[i].length; j++)
System.out.print(endState[i][j] + " ");
System.out.println();
}
}
public static void setEndState(int[][] e)
{
endState = e;
}
public static int[][] getEndState()
{
return endState;
}
public int[][] getBoard()
{
return board;
}
public int getG()
{
return gValue;
}
public int getH()
{
return hValue;
}
public int getf()
{
return fValue;
}
public State getPreviousState()
{
return previousState;
}
}
public static void main(String[] args)
{
String startState = "";
String endState = "";
startState = JOptionPane.showInputDialog("Enter the start state: ");
while(startState != null && !validateInput(startState))
startState = JOptionPane.showInputDialog("Wrong format, renter the start state: ");
if(startState != null)
{
endState = JOptionPane.showInputDialog("Enter the end state: ");
while(endState != null && !validateInput(endState, startState.length()))
endState = JOptionPane.showInputDialog("Wrong format, renter the end state: ");
if(endState != null)
{
State.setEndState(convertInput(endState));
ArrayList<State> openSet = new ArrayList<>();
ArrayList<State> closedSet = new ArrayList<>();
State currentState = null;
boolean found = false;
openSet.add(new State(convertInput(startState), 0, null));
System.out.println("Finding solution for:\n" + openSet.get(0));
while(!openSet.isEmpty() && !found)
{
int index = 0;
int lowestf = openSet.get(0).getf();
int lowestg = openSet.get(0).getG();
for(int i = 1; i < openSet.size(); i++)
{
if(openSet.get(i).getf() < lowestf)
{
index = i;
lowestf = openSet.get(i).getf();
}
else if(openSet.get(i).getf() == lowestf)
{
if(openSet.get(i).getG() < lowestg)
{
lowestg = openSet.get(i).getG();
index = i;
}
}
}
currentState = openSet.get(index);
if(currentState.getH() == 0)
found = true;
else
{
openSet.remove(index);
closedSet.add(currentState);
ArrayList<State> possibleMoves = currentState.getPossibleMoves();
for(int i = 0; i < possibleMoves.size(); i++)
{
State temp = possibleMoves.get(i);
if(!inSet(closedSet, temp) && !inSet(openSet, temp))
openSet.add(temp);
}
}
}
if(!found)
System.out.println("Solution not found!");
else
{
ArrayList<State> path = new ArrayList<>();
System.out.println("Solution found!");
System.out.println("-------END-------");
while(currentState.getPreviousState() != null)
{
path.add(currentState);
currentState = currentState.getPreviousState();
}
for(int i = 0 ; i < path.size(); i++)
{
System.out.println("Move " + (path.size() - i) + ":\n" + path.get(i));
}
System.out.println("Scroll up for moves\n-------Start-------\nInitial State:\n" + currentState);
System.out.println("\nIt took " + path.size() + " moves.");
}
}
}
}
public static int[][] convertInput(String in)
{
String [] temp;
temp = (in.split(" "));
int count = 0;
int size = (int)Math.sqrt(temp.length);
int [][] board = new int [size][size];
for(int i = 0; i < size; i++)
for(int j = 0; j < size; j++)
{
board[i][j] = Integer.parseInt(temp [count]);
count++;
}
return board;
}
public static boolean validateInput(String in)
{
if(in == null)
return false;
ArrayList<Integer> numbers = new ArrayList<>();
String[] temp = in.split("\\s");
ArrayList<String> checkIndividual = new ArrayList<>();
if(temp.length == 9 || temp.length == 16)
{
for(int i = 0; i < temp.length;i++)
numbers.add(i);
for(int i = 0; i < temp.length;i++)
if(numbers.contains(Integer.parseInt(temp[i])) && (!checkIndividual.contains(temp[i])))
checkIndividual.add(temp[i]);
if(checkIndividual.size() == numbers.size())
return true;
}
return false;
}
public static boolean validateInput(String in, int s)
{
if(in == null || in.length() != s)
return false;
ArrayList<Integer> numbers = new ArrayList<>();
String[] temp = in.split("\\s");
ArrayList<String> checkIndividual = new ArrayList<>();
if(temp.length == 9 || temp.length == 16)
{
for(int i = 0; i < temp.length;i++)
numbers.add(i);
for(int i = 0; i < temp.length;i++)
if(numbers.contains(Integer.parseInt(temp[i])) && (!checkIndividual.contains(temp[i])))
checkIndividual.add(temp[i]);
if(checkIndividual.size() == numbers.size())
return true;
}
return false;
}
public static boolean inSet(ArrayList<State> set, State s)
{
for(int i = 0; i < set.size(); i++)
if(s.isEqual(set.get(i)))
return true;
return false;
}
}