Skip to content

Commit 23c4aa4

Browse files
author
Gabriel Derrien
committed
Ajout de la classe abstraite 'Solver'
1 parent 7af2c6b commit 23c4aa4

16 files changed

+59
-70
lines changed

bin/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
/Main.class
77
/Maze.class
88
/Node.class
9+
/Solver.class
910
/Square.class

bin/AStarSolver$1.class

19 Bytes
Binary file not shown.

bin/AStarSolver$2.class

19 Bytes
Binary file not shown.

bin/AStarSolver.class

-155 Bytes
Binary file not shown.

bin/BFS_Solver.class

-254 Bytes
Binary file not shown.

bin/DFS_Solver.class

-50 Bytes
Binary file not shown.

bin/Main.class

113 Bytes
Binary file not shown.

bin/Maze.class

-31 Bytes
Binary file not shown.

bin/Square.class

-35 Bytes
Binary file not shown.

src/AStarSolver.java

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,19 @@
22
import java.util.Iterator;
33
import java.util.LinkedList;
44
import java.util.PriorityQueue;
5-
import java.util.Queue;
65

7-
public class AStarSolver
6+
public class AStarSolver extends Solver
87
{
9-
private Maze maze;
10-
private String result;
11-
private Queue<Node<Maze>> openNodes;
12-
private Queue<Square> closedSquares;
13-
private int nodesCounter;
14-
private int pathLength;
15-
168
/*
179
* Constructor
1810
* m: The maze to solve
1911
*/
20-
public AStarSolver(Maze m)
12+
public AStarSolver(Maze m, Boolean manhattan)
2113
{
2214
this.maze = m;
2315
this.result = "";
24-
this.openNodes = new PriorityQueue<Node<Maze>>(new Comparator<Node<Maze>>()
16+
this.manhattan = manhattan;
17+
this.frontier = new PriorityQueue<Node<Maze>>(new Comparator<Node<Maze>>()
2518
{
2619
public int compare(Node<Maze> s1, Node<Maze> s2)
2720
{
@@ -57,7 +50,7 @@ else if(cs1 == cs2)
5750
* Solves the maze with the A* algorithm
5851
* manhattan: Set as true to use the MANHATTAN DISTANCE instead of EUCLIDEAN DISTANCE
5952
*/
60-
public void solve(boolean manhattan)
53+
public void solve()
6154
{
6255
this.maze.initMaze(); //Re-init maze
6356

@@ -74,35 +67,35 @@ public void solve(boolean manhattan)
7467
this.maze.getStart().calcF();
7568

7669
//Init data structures
77-
this.openNodes.clear(); //Clear openNodes Queue
78-
this.openNodes.offer(new Node<Maze>(this.maze)); //Adding the first node (Start node) (G is at 0, Start to Start = 0)
70+
this.frontier.clear(); //Clear frontier Queue
71+
((PriorityQueue<Node<Maze>>) this.frontier).offer(new Node<Maze>(this.maze)); //Adding the first node (Start node) (G is at 0, Start to Start = 0)
7972
this.closedSquares.clear(); //Clear closedSquares
8073

8174
//Measure run time
8275
long startTime = System.currentTimeMillis();
8376

8477
while(!endfound)
8578
{
86-
if(this.openNodes.isEmpty())
79+
if(this.frontier.isEmpty())
8780
break;
8881

8982
else
9083
{
91-
Node<Maze> current = this.openNodes.remove();
84+
Node<Maze> current = ((PriorityQueue<Node<Maze>>) this.frontier).remove();
9285
this.maze = (Maze) current.getContent();
9386
Square currState = this.maze.getCurrState();
9487

9588
if(currState.getCol() == this.maze.getEnd().getCol() && currState.getLine() == this.maze.getEnd().getLine())
9689
{
9790
Node<Maze> temp = new Node<Maze>(this.maze);
9891
temp.setFather(current);
99-
this.openNodes.offer(temp);
92+
((PriorityQueue<Node<Maze>>) this.frontier).offer(temp);
10093
endfound = true;
10194
}
10295

10396
else
10497
{
105-
LinkedList<Node<Maze>> nexts = this.getNextSquares(manhattan);
98+
LinkedList<Node<Maze>> nexts = this.getNextSquares();
10699
if(!this.closedSquares.contains(currState))
107100
{
108101
this.closedSquares.add(currState);
@@ -119,10 +112,10 @@ public void solve(boolean manhattan)
119112
continue;
120113
else
121114
{
122-
if(!this.openNodes.contains(neighbor))
115+
if(!this.frontier.contains(neighbor))
123116
{
124117
neighbor.setFather(current);
125-
this.openNodes.offer(neighbor);
118+
((PriorityQueue<Node<Maze>>) this.frontier).offer(neighbor);
126119
this.nodesCounter++;
127120
}
128121
}
@@ -132,15 +125,14 @@ public void solve(boolean manhattan)
132125
}
133126
long endTime = System.currentTimeMillis();
134127

135-
this.setResult(endfound, manhattan, endTime - startTime);
128+
this.setResult(endfound, endTime - startTime);
136129
}
137130

138131
/*
139132
* Get the next ("walkables") squares from the given square
140133
* c: Square from where to get the nexts squares
141-
* manhattan: If True, the distances computed will use the MANHATTAN DISTANCE instead of EUCLIDEAN DISTANCE
142134
*/
143-
public LinkedList<Node<Maze>> getNextSquares(Boolean manhattan)
135+
public LinkedList<Node<Maze>> getNextSquares()
144136
{
145137
LinkedList<Node<Maze>> res = new LinkedList<Node<Maze>>();
146138

@@ -154,7 +146,7 @@ public LinkedList<Node<Maze>> getNextSquares(Boolean manhattan)
154146
Square tempSq = nexts.get(i).getCurrState();
155147
if(!this.closedSquares.contains(tempSq))
156148
{
157-
if(manhattan)
149+
if(this.manhattan)
158150
nexts.get(i).getCurrState().calcManhattanH();
159151
else
160152
nexts.get(i).getCurrState().calcEuclidH();
@@ -180,9 +172,9 @@ public LinkedList<Node<Maze>> getNextSquares(Boolean manhattan)
180172
*
181173
* PRIVATE: This method must be called only at the end of the solve method. Any other call may throw errors.
182174
*/
183-
private void setResult(boolean success, boolean manhattan, long time)
175+
private void setResult(boolean success, long time)
184176
{
185-
if(manhattan)
177+
if(this.manhattan)
186178
this.result = " ___ __ ___ __ __ __ \r\n" +
187179
" / | __/|_ / |/ /___ _____ / /_ ____ _/ /_/ /_____ _____ \r\n" +
188180
" / /| || / ______ / /|_/ / __ `/ __ \\/ __ \\/ __ `/ __/ __/ __ `/ __ \\\r\n" +
@@ -198,7 +190,7 @@ private void setResult(boolean success, boolean manhattan, long time)
198190
if(success)
199191
{
200192
this.maze.resetGrid();
201-
Node<Maze> revertedTree = this.openNodes.remove();
193+
Node<Maze> revertedTree = ((PriorityQueue<Node<Maze>>) this.frontier).remove();
202194

203195
revertedTree = revertedTree.getFather();
204196
this.result += "Path: " + this.maze.getEnd().toString() + "(End) <- ";

src/BFS_Solver.java

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import java.util.Iterator;
22
import java.util.LinkedList;
33

4-
public class BFS_Solver
4+
public class BFS_Solver extends Solver
55
{
6-
private Maze maze;
7-
private String result;
8-
private LinkedList<Node<Maze>> frontier;
9-
private LinkedList<Square> closedSquares;
10-
private int nodesCounter;
11-
private int pathLength;
126

137
/*
148
* Constructor
@@ -50,7 +44,7 @@ public void solve()
5044

5145
else
5246
{
53-
Node<Maze> current = this.frontier.removeFirst(); //Get first node from the frontier
47+
Node<Maze> current = ((LinkedList<Node<Maze>>) this.frontier).removeFirst(); //Get first node from the frontier
5448
this.maze = (Maze) current.getContent(); //Get maze from the node
5549
Square currState = this.maze.getCurrState(); //Get current state from the maze
5650

@@ -140,7 +134,7 @@ private void setResult(boolean success, long time)
140134
if(success)
141135
{
142136
this.maze.resetGrid();
143-
Node<Maze> revertedTree = this.frontier.removeLast();
137+
Node<Maze> revertedTree = ((LinkedList<Node<Maze>>) this.frontier).removeLast();
144138

145139
revertedTree = revertedTree.getFather();
146140
this.result += "Path: " + this.maze.getEnd().toString() + "(End) <- ";
@@ -180,23 +174,11 @@ public String getResult()
180174
return this.result;
181175
}
182176

183-
/*
184-
* Returns all the closed nodes in a string
185-
*/
186-
public String printClosedNodes()
187-
{
188-
String res = "Closed nodes : \n";
189-
for(int i = 0; i < this.closedSquares.size(); i++)
190-
res += "(" + i + ") " + this.closedSquares.get(i).toString() + "\n";
191-
192-
return res;
193-
}
194-
195177
/*
196178
* Returns the frontier from the last solving
197179
*/
198180
public LinkedList<Node<Maze>> getFrontier()
199181
{
200-
return this.frontier;
182+
return (LinkedList<Node<Maze>>) this.frontier;
201183
}
202184
}

src/DFS_Solver.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22
import java.util.LinkedList;
33
import java.util.Stack;
44

5-
public class DFS_Solver
5+
public class DFS_Solver extends Solver
66
{
7-
private Maze maze;
8-
private String result;
9-
private Stack<Node<Maze>> frontier;
10-
private Stack<Square> closedSquares;
11-
private int nodesCounter;
12-
private int pathLength;
7+
/*private Stack<Node<Maze>> frontier;
8+
private Stack<Square> closedSquares;*/
139

1410
/*
1511
* Constructor
@@ -38,7 +34,7 @@ public void solve()
3834

3935
//Init frontier
4036
this.frontier.clear();
41-
this.frontier.push(new Node<Maze>(this.maze)); //Add first state
37+
((Stack<Node<Maze>>) this.frontier).push(new Node<Maze>(this.maze)); //Add first state
4238

4339
//Measure run time
4440
long startTime = System.currentTimeMillis();
@@ -51,15 +47,15 @@ public void solve()
5147

5248
else
5349
{
54-
Node<Maze> current = this.frontier.pop(); //Get first node from the frontier
50+
Node<Maze> current = ((Stack<Node<Maze>>) this.frontier).pop(); //Get first node from the frontier
5551
this.maze = (Maze) current.getContent();
5652
Square currState = this.maze.getCurrState();
5753

5854
if(currState.getLine() == this.maze.getEnd().getLine() && currState.getCol() == this.maze.getEnd().getCol())
5955
{
6056
Node<Maze> temp = new Node<Maze>(this.maze);
6157
temp.setFather(current);
62-
this.frontier.push(temp);
58+
((Stack<Node<Maze>>) this.frontier).push(temp);
6359
endfound = true;
6460
}
6561

@@ -68,7 +64,7 @@ public void solve()
6864
LinkedList<Node<Maze>> nexts = this.getNextSquares(); //Get next possible states
6965
if(!this.closedSquares.contains(currState))
7066
{
71-
this.closedSquares.push(currState);
67+
((Stack<Square>) this.closedSquares).push(currState);
7268
currState.setAttribute("*");
7369
}
7470

@@ -78,7 +74,7 @@ public void solve()
7874
{
7975
Node<Maze> temp = x.next();
8076
temp.setFather(current);
81-
this.frontier.push(temp);
77+
((Stack<Node<Maze>>) this.frontier).push(temp);
8278
this.nodesCounter++;
8379
}
8480
}
@@ -135,7 +131,7 @@ private void setResult(boolean success, long time)
135131
if(success)
136132
{
137133
this.maze.initMaze();
138-
Node<Maze> revertedTree = this.frontier.pop();
134+
Node<Maze> revertedTree = ((Stack<Node<Maze>>) this.frontier).pop();
139135

140136
this.result += "Path: " + this.maze.getEnd().toString() + "(End) <- ";
141137
this.pathLength++;
@@ -171,14 +167,14 @@ public String getResult()
171167
if(result == "")
172168
return "No resolution computed, please use DFS_Solver.solve() first";
173169
else
174-
return result;
170+
return this.result;
175171
}
176172

177173
/*
178174
* Returns the frontier from the last solving
179175
*/
180176
public Stack<Node<Maze>> getFrontier()
181177
{
182-
return this.frontier;
178+
return (Stack<Node<Maze>>) this.frontier;
183179
}
184180
}

src/Main.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,19 @@ public static void main(String[] args) throws IOException
3131

3232
BFS_Solver b2 = new BFS_Solver(lab2);
3333
DFS_Solver d2 = new DFS_Solver(lab2);
34-
AStarSolver a2 = new AStarSolver(lab2);
34+
AStarSolver a2 = new AStarSolver(lab2, true);
3535

3636
b2.solve();
3737
d2.solve();
38-
a2.solve(true);
38+
a2.solve();
3939

4040
System.out.println(b2.getResult());
4141
System.out.println(d2.getResult());
4242
System.out.println(a2.getResult());
4343

44-
a2.solve(false);
44+
a2 = new AStarSolver(lab2, false);
45+
46+
a2.solve();
4547
System.out.println(a2.getResult());
4648
}
4749
}

src/Maze.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public Maze(int lRange, int cRange, Square start, Square end)
3939

4040
//Create start and end MazeState objects contaning the start and end squares (stated)
4141
this.end = end;
42-
this.end.depth = 0;
4342
this.start = start;
4443

4544
this.currState = this.getStart();

src/Solver.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.AbstractCollection;
2+
import java.util.LinkedList;
3+
4+
public abstract class Solver
5+
{
6+
protected Maze maze;
7+
protected String result;
8+
protected AbstractCollection<Node<Maze>> frontier;
9+
protected AbstractCollection<Square> closedSquares;
10+
protected int nodesCounter;
11+
protected int pathLength;
12+
protected Boolean manhattan;
13+
14+
public abstract void solve();
15+
16+
public abstract LinkedList<Node<Maze>> getNextSquares();
17+
18+
public abstract String getResult();
19+
}

src/Square.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public class Square
88
private int c;
99
private String attribute;
1010
private boolean wall;
11-
public int depth;
1211

1312
private int g;
1413
private double h;
@@ -33,7 +32,6 @@ public Square(int l, int c, String a)
3332
this.c = c;
3433
this.attribute = a; //S = Start; E = End; ' ' = Playable; * = Closed
3534
this.wall = false;
36-
this.depth = 0;
3735
this.g = 0;
3836
this.h = 0;
3937
this.f = 0;

0 commit comments

Comments
 (0)