forked from saschazar21/rushhour
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockingHeuristic.java
executable file
·51 lines (36 loc) · 1 KB
/
BlockingHeuristic.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
package Heuristics;
import AStar.Puzzle;
import AStar.State;
public class BlockingHeuristic implements Heuristic {
private Puzzle puzzle;
private int numCars;
private int carPosFixed;
private int carSize;
public BlockingHeuristic(Puzzle puzzle) {
this.puzzle = puzzle;
this.numCars = this.puzzle.getNumCars();
this.carPosFixed = puzzle.getFixedPosition(0);
this.carSize = this.puzzle.getCarSize(0);
}
public int getValue(State state) {
if (state.isGoal()) {
return 0;
}
int blocking = 1;
int carPosFront = state.getVariablePosition(0) + this.carSize;
for (int i = 1; i < this.numCars; i++) {
if (!this.puzzle.getCarOrient(i)) {
continue;
}
if (this.puzzle.getFixedPosition(i) < carPosFront) {
continue;
}
int currentCarPos = state.getVariablePosition(i);
int currentCarPosFront = currentCarPos + this.puzzle.getCarSize(i);
if (this.carPosFixed >= currentCarPos && this.carPosFixed < currentCarPosFront) {
blocking++;
}
}
return blocking;
}
}