-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle.java
493 lines (442 loc) · 11.3 KB
/
Puzzle.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Puzzle {
private char[][] board;
private double DistanceToStart;
private int xExit;
private int yExit;
private int redDis;
private int bCars;
private Car redCar;
private float alpha;
private float beta;
public int size = 6;
private String filePath = "C:\\Users\\user\\nov - eclipse-workspace\\lab1-AI\\src\\boards.txt";
private ArrayList<Car> cars;
public Puzzle() {
board = new char[size][size];
setxExit(5);
setyExit(2);
cars = new ArrayList<Car>();
alpha = 7;
beta = 3;
}
public Puzzle(int i, int j) {
board = new char[size][size];
setxExit(j);
setyExit(i);
cars = new ArrayList<Car>();
alpha = 7;
beta = 3;
}
public Puzzle(int i, int j, int _size) {
size = _size;
board = new char[size][size];
setxExit(j);
setyExit(i);
cars = new ArrayList<Car>();
alpha = 7;
beta = 3;
}
public Puzzle(Puzzle current) {
size = current.size;
board = new char[current.size][current.size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++)
board[i][j] = current.getBoard()[i][j];
}
setxExit(current.getxExit());
setyExit(current.getyExit());
cars = new ArrayList<Car>();
for (int i = 0; i < current.getCars().size(); i++)
cars.add(new Car(current.getCars().get(i)));
bCars = current.getbCars();
redDis = current.getRedDis();
redCar = current.getRedCar();
DistanceToStart = current.getDistanceToStart();
alpha = current.getAlpha();
beta = current.getBeta();
// TODO Auto-generated constructor stub
}
public void print_board() {
System.out.print("\n\n");
for (int i = 0; i < size; i++) {
System.out.print("***** ");
String exitSym = "*****";
for (int j = 0; j < size; j++) {
if (i == yExit && j == xExit)
exitSym = "====>";
System.out.print(board[i][j]);
}
System.out.print(" " + exitSym + " \n");
}
System.out.print("\n\n");
}
// checks if the move input is possible
public boolean isPossibleMove(Car car, int steps, boolean dir) {
if (car.isHor_orient()) {
if (dir) {
int xCord = car.getLastX()+steps;
if(xCord>=this.size) //out of bounds
return false;
for(int j=car.getLastX()+1;j<=xCord;j++)
{
if(board[car.getY()][j]!='.')
return false;;
}
} else {
// the car wants to go left;
int xCord = car.getX()-steps;
if(xCord<0) //out of bounds
return false;;
for(int j=car.getX()-1;j>=xCord;j--)
{
if(board[car.getY()][j]!='.')
return false;;
}
}
} else {
if (dir) {
// the car wants to go down
int yCord = car.getLastY()+steps;
if(yCord>=this.size) //out of bounds
return false;
for(int i=car.getLastY()+1;i<=yCord;i++)
{
if(board[i][car.getX()]!='.')
return false;
}
} else {
// the car wants to go up
int yCord = car.getY()-steps;
if(yCord<0) //out of bounds
return false;
for(int i=car.getY()-1;i>=yCord;i--)
{
if(board[i][car.getX()]!='.')
return false;
}
}
}
return true;
}
// return a board in which the move is made , while calculating it's h function properties
public Puzzle moveCar(Car car, int steps, boolean dir) {
if (isPossibleMove(car, steps, dir)) {
Puzzle newBoard = new Puzzle(this);
Car move = newBoard.retCar(car.getKey());
if(car.isHor_orient())
{
if(dir)
{
//the car need to be moved right
int x = move.getX();
// erasing the car
for(int i=x;i<x+move.getLength();i++)
{
newBoard.getBoard()[car.getY()][i]='.';
}
//inserting the car in the right place
int xDest = move.getLastX() + steps;
for(int i=xDest;i>xDest-move.getLength();i--)
{
newBoard.getBoard()[car.getY()][i]=move.getKey();
}
move.setLastX(xDest);
move.setX(xDest-move.getLength()+1);
if(move.getKey()=='X')
{
newBoard.setRedCar(move);
newBoard.setRedDis(xExit-redCar.getLastX());
}
return newBoard;
}
else
{
//the car need to be moved left
int x = car.getLastX();
//erasing the car
for(int i = x;i>x-move.getLength();i--)
{
newBoard.getBoard()[car.getY()][i]='.';
}
//inserting the car in the right place
int xDest = move.getX()-steps;
for(int i=xDest;i<xDest+move.getLength();i++)
{
newBoard.getBoard()[car.getY()][i]=move.getKey();
}
move.setLastX(xDest+move.getLength()-1);
move.setX(xDest);
if(move.getKey()=='X')
{
newBoard.setRedCar(move);
newBoard.setRedDis(xExit-redCar.getLastX());
}
return newBoard;
}
}
else
{
if(dir)
{
//the car need to be moved down
int y = car.getY();
//erasing the car
int blocking = newBoard.getbCars();
if(newBoard.isOnRedPath(move))
blocking--;
for(int i = y;i<y+move.getLength();i++)
{
newBoard.getBoard()[i][move.getLastX()]='.';
}
//inserting the car in the right place
int yDest = car.getLastY() + steps;
for(int i=yDest;i>yDest-move.getLength();i--)
{
newBoard.getBoard()[i][move.getLastX()]=move.getKey();
}
move.setLastY(yDest);
move.setY(yDest-move.getLength()+1);
if(newBoard.isOnRedPath(move))
blocking++;
newBoard.setbCars(blocking);
return newBoard;
}
else
{
//the car need to be moved up
int y = car.getLastY();
int blocking = this.getbCars();
if(newBoard.isOnRedPath(move))
blocking--;
//erasing the car
for(int i = y;i>y-move.getLength();i--)
{
newBoard.getBoard()[i][move.getLastX()]='.';
}
//inserting the car in the right place
int yDest = move.getY()-steps;
for(int i=yDest;i<yDest+move.getLength();i++)
{
newBoard.getBoard()[i][move.getLastX()]=move.getKey();
}
move.setLastY(yDest+move.getLength()-1);
move.setY(yDest);
if(newBoard.isOnRedPath(move))
blocking++;
newBoard.setbCars(blocking);
return newBoard;
}
}
}
else
{
return null;
}
}
public float h() {
return (float) beta * redDis + alpha * (float) bCars;
}
public void readBoard_file(int line) throws FileNotFoundException {
File boardFile = new File(filePath);
java.util.Scanner reader = new java.util.Scanner(boardFile);
String boardLine = "";
for (int i = 1; i <= line; i++) {
boardLine = reader.nextLine();
}
readBoard_line(boardLine);
cars = Car.getCarsFromBoard(this);
}
public void readBoard_scan() {
Scanner scan = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter Puzzle Line");
String line = scan.nextLine(); // Read user input
line.trim();
readBoard_line(line);
cars = Car.getCarsFromBoard(this);
}
public void readBoard_string(String line) {
line.trim();
readBoard_line(line);
cars = Car.getCarsFromBoard(this);
}
public void readBoard_line(String line) {
char[] array = line.toCharArray();
int y = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = array[y];
y++;
}
}
}
public void print_cars() {
for (int i = 0; i < cars.size(); i++) {
Car current = cars.get(i);
System.out.println(current.toString());
}
}
public int getXCarDis() {
Car Xcar = retCar('X');
return xExit - Xcar.getLastX();
}
public int getXcarBlock() {
Car Xcar = retCar('X');
int counter = 0;
for (int j = Xcar.getLastX() + 1; j < size; j++) {
if (board[Xcar.getY()][j] != '.')
counter++;
}
return counter;
}
public int getxExit() {
return xExit;
}
public Car retCar(char c) {
for (int i = 0; i < cars.size(); i++) {
Car car = cars.get(i);
if (car.getKey() == c)
return car;
}
return null;
}
public void setxExit(int xExit) {
this.xExit = xExit;
}
public int getyExit() {
return yExit;
}
public void setyExit(int yExit) {
this.yExit = yExit;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public char[][] getBoard() {
return board;
}
public void setBoard(char[][] board) {
this.board = board;
}
// public ArrayList<Car> getCars() {
// return cars;
// }
// public void setCars(ArrayList<Car> cars) {
// this.cars = cars;
// }
public int getRedDis() {
return redDis;
}
public void setRedDis(int redDis) {
this.redDis = redDis;
}
public int getbCars() {
return bCars;
}
public void setbCars(int bCars) {
this.bCars = bCars;
}
public Car getRedCar() {
return redCar;
}
public void setRedCar(Car redCar) {
this.redCar = redCar;
}
public void removeCar(Car remove) {
cars.remove(remove);
}
public void addCar(Car add) {
cars.add(add);
if (add.getKey() == 'X') {
setRedCar(add);
redDis = (xExit - add.getLastX());
}
}
public boolean isOnRedPath(Car c) {
if ((xExit - c.getLastX()) < getRedDis()) {
if ((c.getLastY() - c.getLength()) < yExit)
return true;
}
return false;
}
public boolean isTargetBoard() {
return (redCar.getLastX() == xExit);
}
public void print_h() {
System.out.println("Red Distance : " + redDis);
System.out.println("#Blocking Cars : " + bCars);
System.out.println("h : " + h());
}
public void print_full_stats() {
print_board();
print_cars();
print_h();
System.out.println("is Targe Puzzle : " + isTargetBoard());
}
public double getDistanceToStart() {
return DistanceToStart;
}
public void setDistanceToStart(double distanceToStart) {
DistanceToStart = distanceToStart;
}
public ArrayList<Car> getCars() {
return cars;
}
public void setCars(ArrayList<Car> cars) {
this.cars = cars;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(DistanceToStart);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + bCars;
result = prime * result + Arrays.deepHashCode(board);
result = prime * result + ((cars == null) ? 0 : cars.hashCode());
result = prime * result + ((filePath == null) ? 0 : filePath.hashCode());
result = prime * result + ((redCar == null) ? 0 : redCar.hashCode());
result = prime * result + redDis;
result = prime * result + size;
result = prime * result + xExit;
result = prime * result + yExit;
return result;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Puzzle other = (Puzzle) obj;
if (other.size != size)
return false;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (this.board[i][j] != other.getBoard()[i][j])
return false;
}
}
return true;
}
public float getAlpha() {
return alpha;
}
public void setAlpha(float alpha) {
this.alpha = alpha;
}
public float getBeta() {
return beta;
}
public void setBeta(float beta) {
this.beta = beta;
}
}