-
Notifications
You must be signed in to change notification settings - Fork 0
/
Astar.java
163 lines (153 loc) · 4.51 KB
/
Astar.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
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.function.Predicate;
public class Astar {
HashSet<Node> closed;
PriorityQueue<Node> open;
public Astar(Node initial)
{
this.closed = new HashSet<Node>();
//creating an priority queue with new compare function for Nodes
open = new PriorityQueue<Node>(new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
// TODO Auto-generated method stub
if (o1.getF_grade() == o2.getF_grade()) {
if (o1.getBoard().h() < o2.getBoard().h())
return -1;
if (o1.getBoard().h() > o2.getBoard().h())
return 1;
return 0;
}
if (o1.getF_grade() > o2.getF_grade())
return 1;
if (o1.getF_grade() < o2.getF_grade())
return -1;
System.out.println("Error With Node's Compare");
return 1;
}
});
initial.setDistance(0);
open.add(initial);
}
public Node aStarSearch()
{
//basic a* search
while (!open.isEmpty()) {
Node current = open.poll();
if (current.is_traget()) {
return current; // trace back the moves.
} else {
closed.add(current);
expand(current);
}
}
return null;
}
//creates a list of all possible option , addes to Open if is a new node or better existing node
public void expand(Node node)
{
ArrayList<Node> neighboors = successor(node);
for(int i=0;i<neighboors.size();i++)
{
Node current = neighboors.get(i);
current.calcGrade();
boolean isInOpen = open.contains(current);
boolean isInClosed = closed.contains(current);
if(!isInClosed&&!isInOpen)
{
open.add(current);
}
else
{
if(isInOpen)
{
Predicate<Node> condition = t->((t.getF_grade()>current.getF_grade())&&(t.equals(current)));
open.removeIf(condition);
if(!open.contains(current))
open.add(current);
}
else
{
if(isInClosed)
{
Predicate<Node> condition = t->((t.getF_grade()>current.getF_grade())&&(t.equals(current)));
closed.removeIf(condition);
if(!closed.contains(current))
open.add(current);
}
}
}
}
}
// tries to create every possible move , if legal creates a node of this move
public ArrayList<Node> successor(Node node)
{
ArrayList<Node> neighbors = new ArrayList<Node>();
Board current = node.getBoard();
ArrayList<Car> cars = current.getCars();
if(current.getbCars()==0) // end case in which the red car path is not blocked and the next move should be the end move
{
if(current.isPossibleMove(current.getRedCar(),current.getxExit()-current.getRedCar().getLastX(),true))
{
Board newBoard = current.moveCar(current.getRedCar(),current.getxExit()-current.getRedCar().getLastX(),true);
Node newNode= new Node(node.getDistance()+1);
newNode.setBoard(newBoard);
newNode.setFather(node);
newNode.calcGrade();
char direction='R';
String moveS =String.valueOf("X"+String.valueOf(direction)+String.valueOf(current.getxExit()-current.getRedCar().getLastX()));
newNode.setMove(moveS);
neighbors.add(newNode);
}
return neighbors;
}
for (int c = 0; c < cars.size(); c++) {
for (int steps = 1; steps < current.size; steps++) {
boolean dir = false; // false idicates up or left
if(current.isPossibleMove(cars.get(c), steps, dir))
{
Board newBoard = current.moveCar(cars.get(c), steps, dir);
Node newNode= new Node(node.getDistance()+1);
newNode.setBoard(newBoard);
newNode.setFather(node);
newNode.calcGrade();
char direction='U';
if(cars.get(c).isHor_orient())
{
direction='L';
}
String moveS =String.valueOf(cars.get(c).getKey())+String.valueOf(direction)+String.valueOf(steps);
newNode.setMove(moveS);
neighbors.add(newNode);
} else {
steps = current.size;
}
}
for (int steps = 1; steps < current.size; steps++) {
boolean dir = true; // right idicates right or down
if(current.isPossibleMove(cars.get(c), steps, dir))
{
Board newBoard = current.moveCar(cars.get(c), steps, dir);
Node newNode= new Node(node.getDistance()+1);
newNode.setBoard(newBoard);
newNode.setFather(node);
newNode.calcGrade();
char direction='D';
if(cars.get(c).isHor_orient())
{
direction='R';
}
String moveS =String.valueOf(cars.get(c).getKey())+String.valueOf(direction)+String.valueOf(steps);
newNode.setMove(moveS);
neighbors.add(newNode);
} else {
steps = current.size;
}
}
}
return neighbors;
}
}