-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolver.java
355 lines (316 loc) · 10.8 KB
/
Solver.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class Solver {
static HashSet<Board> tray = new HashSet<Board> ();
/*
////////////////////get the initial configuration and print the board //////////////////
public static Board board_setup(String filename) throws IOException{
BufferedReader initConfig = null;
try {
initConfig = new BufferedReader (new FileReader (filename));
} catch (Exception e) {
System.err.println ("Couldn't access file!");
System.exit (1);
}
String line = null;
line = initConfig.readLine();
StringTokenizer tokens = new StringTokenizer (line, " ");
int row = Integer.parseInt((String) tokens.nextElement ( ));
int col = Integer.parseInt((String) tokens.nextElement ( ));
Board board= new Board(row, col);
while ((line=initConfig.readLine()) != null) {
tokens = new StringTokenizer (line, " ");
int b_len = Integer.parseInt((String) tokens.nextElement ( ));
int b_wid = Integer.parseInt((String) tokens.nextElement ( ));
int b_row = Integer.parseInt((String) tokens.nextElement ( ));
int b_col = Integer.parseInt((String) tokens.nextElement ( ));
board.adding_block(b_len, b_wid, b_row, b_col);
//board.block_set.add((new block(b_len, b_wid, b_row, b_col)));
//block b0 = board.adding_block(b_len, b_wid, b_row, b_col);
//board.block_set.add(b0);
//board.block_set = board.adding_block(b_len, b_wid, b_row, b_col);
}
board.displayBoard();
initConfig.close();
return board;
}
public static ArrayList<block> getGoal(String filename) throws IOException{
ArrayList<block> result= new ArrayList<block>();
BufferedReader initConfig = null;
try {
initConfig = new BufferedReader (new FileReader (filename));
} catch (Exception e) {
System.err.println ("Couldn't access file!");
System.exit (1);
}
String line = null;
// line = initConfig.readLine();
StringTokenizer tokens = new StringTokenizer (" ");
//
//int row = Integer.parseInt((String) tokens.nextElement ( ));
//int col = Integer.parseInt((String) tokens.nextElement ( ));
//Board board= new Board(row, col);
while ((line=initConfig.readLine()) != null) {
tokens = new StringTokenizer (line, " ");
int b_len = Integer.parseInt((String) tokens.nextElement ( ));
int b_wid = Integer.parseInt((String) tokens.nextElement ( ));
int b_row = Integer.parseInt((String) tokens.nextElement ( ));
int b_col = Integer.parseInt((String) tokens.nextElement ( ));
block b= new block(b_len, b_wid, b_row, b_col);
result.add(b);
//board.block_set.add((new block(b_len, b_wid, b_row, b_col)));
//block b0 = board.adding_block(b_len, b_wid, b_row, b_col);
//board.block_set.add(b0);
//board.block_set = board.adding_block(b_len, b_wid, b_row, b_col);
}
System.out.println("Goal has "+ result.size()+" blocks");
initConfig.close();
return result;
}
public static boolean isAtGoal(Board playBoard, Board goalBoard){
return false;
}
*/
/* public Board generatemove(Board brd){
//String inputConfigName = "";
Board board = null;
Iterator <block> iter = brd.getblock().iterator();
while (iter.hasNext()){
block b = iter.next();
if (brd.MoveRight(b)){
board= new Board(brd.getRows(), brd.getCols());
board.adding_block(b.get_block_len(), b.get_block_wid(), b.get_block_row(), b.get_block_col());
}else if (brd.MoveUp(b)){
board= new Board(brd.getRows(), brd.getCols());
board.adding_block(b.get_block_len(), b.get_block_wid(), b.get_block_row(), b.get_block_col());
}else if (brd.MoveLeft(b)){
board= new Board(brd.getRows(), brd.getCols());
board.adding_block(b.get_block_len(), b.get_block_wid(), b.get_block_row(), b.get_block_col());
}else if (brd.MoveDown(b)){
board= new Board(brd.getRows(), brd.getCols());
board.adding_block(b.get_block_len(), b.get_block_wid(), b.get_block_row(), b.get_block_col());
}else{}
}
return board;
}
*/
/*
public static Board generaterightmove(Board brd, block b){
Board temp;
temp = brd.clone();
brd.MoveRight(b);
if (tray.contains(brd)){
return generateleftmove(temp, b);}
else
tray.add(brd);
return brd;
}
public static Board generateleftmove(Board brd, block b){
Board temp;
temp = brd.clone();
brd.MoveLeft(b);
if (tray.contains(brd)){
return generateupmove(temp, b);}
else
tray.add(brd);
return brd;
}
public static Board generateupmove(Board brd, block b){
Board temp;
temp = brd.clone();
brd.MoveUp(b);
if (tray.contains(brd)){
return generatedownmove(temp, b);}
else
tray.add(brd);
return brd;
}
public static Board generatedownmove(Board brd, block b){
brd.MoveDown(b);
if (tray.contains(brd)){
return null;}
else
tray.add(brd);
return brd;
}
public static Board generatemove(Board brd, block b){
Iterator <block> iter = brd.getblock().iterator();
block t = iter.next();
Board temp = brd.clone();
Board newp = null;
System.out.println("temp is");
temp.displayBoard();
generateleftmove(temp, t);
System.out.println("temp after move is");
temp.displayBoard();
System.out.println("newp is");
newp = brd.clone();
newp.displayBoard();
System.out.println("brd is");
brd.displayBoard();
System.out.println("");
return temp;
}
*/
/*
public static Board generatemove(Board brd, block b){
Board myboard;
Board temp;
temp = brd.clone();
Iterator <block> iter = brd.getblock().iterator();
myboard = null;
while (myboard == null && iter.hasNext()){
temp = brd.clone();
myboard = generaterightmove(temp, iter.next());
while (myboard != null){
temp = brd.clone();
myboard = generaterightmove(temp, b);}
temp = brd.clone();
b = iter.next();}
return myboard;
}
*/
/*
public Board generatemove(Board brd){
Board board;
Board temp = brd;
Iterator <block> iter = brd.getblock().iterator();
block b = iter.next();
while ()
if (brd.MoveRight(b)){
board= new Board(brd.getRows(), brd.getCols());
board.adding_block(b.get_block_len(), b.get_block_wid(), b.get_block_row(), b.get_block_col());
if (tray.contains(board)){
brd = temp;
return generatemove(brd);}
else
tray.add(board);
return board;
}else if (brd.MoveUp(b)){
board= new Board(brd.getRows(), brd.getCols());
board.adding_block(b.get_block_len(), b.get_block_wid(), b.get_block_row(), b.get_block_col());
if (tray.contains(board)){
brd = temp;
return generatemove(brd);}
else
tray.add(board);
return board;
}else if (brd.MoveLeft(b)){
board= new Board(brd.getRows(), brd.getCols());
board.adding_block(b.get_block_len(), b.get_block_wid(), b.get_block_row(), b.get_block_col());
if (tray.contains(board)){
brd = temp;
return generatemove(brd);}
else
tray.add(board);
return board;
}else if (brd.MoveDown(b)){
board= new Board(brd.getRows(), brd.getCols());
board.adding_block(b.get_block_len(), b.get_block_wid(), b.get_block_row(), b.get_block_col());
if (tray.contains(board)){
brd = temp;
return generatemove(brd);}
else
tray.add(board);
return board;
}else{
return null;
}
}*/
/*
public static boolean play(Board brd, Board fnl){
Board board = null;
Board head= null;
Board ref = null;
boolean t = false;
if (brd == null)
return false;
if (brd.equals(fnl))
return true;
tray.add(brd);
ref = board = head = brd.clone();
head.myparent = brd;
brd.Leftmostchild = head;
generatemove(board, null);
/* while (ref!= null){
generatemove(board, null);
//board = brd.clone();
ref.mysibbling = board;
board.myparent = brd;
ref = board;
if (head == null)
return false;
// while (!t){
// t = play(head, fnl);
// if (t == true)
// return true;
// }
}
return false;
}
*/
////////////////////////////////////////////////////////////////////////////////////////
public static void main (String [ ] args) throws IOException{
String inputConfigName = "", finalConfigName = "";
if (args.length == 2) {
inputConfigName = args[0];
finalConfigName = args[1];
} else if (args.length == 3){
inputConfigName = args[1];
finalConfigName = args[2];
} else{
System.out.println("Wrong number of arguments");
System.exit (0);
}
Board my_board = new Board(20, 20);
my_board.adding_block(1, 1, 1, 1);
my_board.adding_block(1, 2, 0, 0);
my_board.adding_block(1, 1, 0, 2);
my_board.adding_block(1, 1, 2, 2);
my_board.adding_block(1, 1, 1, 0);
my_board.adding_block(1, 2, 3, 1);
my_board.adding_block(1, 1, 3, 0);
my_board.adding_block(7, 7, 5, 5);
my_board.adding_block(1, 1, 5, 0);
my_board.adding_block(1, 1, 19, 19);
my_board.displayboard();
my_board.display();
/*
Board my_board, finalboard;//board_final;
my_board = board_setup(inputConfigName);
System.out.println("");
finalboard = board_setup(finalConfigName);
if (Solver.play(my_board, finalboard))
System.out.println("Solution found");
else
System.out.println("No solution exists");
*/
/*
Board my_board;//board_final;
my_board = board_setup(inputConfigName);
System.out.println();
ArrayList<block>theGoal= getGoal(finalConfigName);
System.out.println(my_board.compareToGoal(theGoal));
block b=my_board.findBlockByID(1);
if (b==null) System.out.println("It is null!!\n");else
my_board.MoveRight(b);
my_board.displayBoard();
b=my_board.findBlockByID(3);
my_board.MoveUp(b);
System.out.println();
my_board.displayBoard();
b=my_board.findBlockByID(6);
my_board.MoveUp(b);
System.out.println();
my_board.displayBoard();
b=my_board.findBlockByID(2);
my_board.MoveLeft(b);
System.out.println();
my_board.displayBoard();
//if (my_board.equal(board_final) )
// System.out.println("the compare is good");
*/
}
}