Skip to content

Commit 9397f91

Browse files
author
Gabriel Derrien
committed
Retrait de la methode setResult de tous les solveurs
1 parent 23c4aa4 commit 9397f91

11 files changed

+139
-184
lines changed

bin/AStarSolver$1.class

0 Bytes
Binary file not shown.

bin/AStarSolver$2.class

0 Bytes
Binary file not shown.

bin/AStarSolver.class

42 Bytes
Binary file not shown.

bin/BFS_Solver.class

-40 Bytes
Binary file not shown.

bin/DFS_Solver.class

-100 Bytes
Binary file not shown.

bin/Main.class

-67 Bytes
Binary file not shown.

src/AStarSolver.java

Lines changed: 45 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import java.util.AbstractCollection;
12
import java.util.Comparator;
23
import java.util.Iterator;
34
import java.util.LinkedList;
@@ -46,11 +47,7 @@ else if(cs1 == cs2)
4647
});
4748
}
4849

49-
/*
50-
* Solves the maze with the A* algorithm
51-
* manhattan: Set as true to use the MANHATTAN DISTANCE instead of EUCLIDEAN DISTANCE
52-
*/
53-
public void solve()
50+
public String solve()
5451
{
5552
this.maze.initMaze(); //Re-init maze
5653

@@ -125,55 +122,8 @@ public void solve()
125122
}
126123
long endTime = System.currentTimeMillis();
127124

128-
this.setResult(endfound, endTime - startTime);
129-
}
130-
131-
/*
132-
* Get the next ("walkables") squares from the given square
133-
* c: Square from where to get the nexts squares
134-
*/
135-
public LinkedList<Node<Maze>> getNextSquares()
136-
{
137-
LinkedList<Node<Maze>> res = new LinkedList<Node<Maze>>();
138-
139-
//Get 4 next squares
140-
LinkedList<Maze> nexts = this.maze.getCurrState().getNexts();
141-
142-
int gCurrent = this.maze.getCurrState().getG();
143-
144-
for(int i = 0; i < nexts.size(); i++)
145-
{
146-
Square tempSq = nexts.get(i).getCurrState();
147-
if(!this.closedSquares.contains(tempSq))
148-
{
149-
if(this.manhattan)
150-
nexts.get(i).getCurrState().calcManhattanH();
151-
else
152-
nexts.get(i).getCurrState().calcEuclidH();
153-
154-
nexts.get(i).getCurrState().incG(gCurrent);
155-
156-
nexts.get(i).getCurrState().calcF();
157-
158-
Node<Maze> tempNode = new Node<Maze>(nexts.get(i));
159-
res.add(tempNode);
160-
}
161-
}
125+
long time = endTime - startTime;
162126

163-
return res;
164-
}
165-
166-
/*
167-
* Sets the result in this format :
168-
* - Path trace
169-
* - Path length
170-
* - Number of nodes created
171-
* - The maze with the path written
172-
*
173-
* PRIVATE: This method must be called only at the end of the solve method. Any other call may throw errors.
174-
*/
175-
private void setResult(boolean success, long time)
176-
{
177127
if(this.manhattan)
178128
this.result = " ___ __ ___ __ __ __ \r\n" +
179129
" / | __/|_ / |/ /___ _____ / /_ ____ _/ /_/ /_____ _____ \r\n" +
@@ -187,7 +137,7 @@ private void setResult(boolean success, long time)
187137
" / ___ /_ __| /_____/ / /___/ /_/ / /__/ / / /_/ / \r\n" +
188138
"/_/ |_||/ /_____/\\__,_/\\___/_/_/\\__,_/ \n";
189139

190-
if(success)
140+
if(endfound)
191141
{
192142
this.maze.resetGrid();
193143
Node<Maze> revertedTree = ((PriorityQueue<Node<Maze>>) this.frontier).remove();
@@ -217,16 +167,55 @@ private void setResult(boolean success, long time)
217167
{
218168
this.result += "Failed : Unable to go further and/or end is unreachable.";
219169
}
170+
171+
return this.result;
220172
}
221-
173+
222174
/*
223-
* Returns the result from the last solving
175+
* Get the next ("walkables") squares from the given square
176+
* c: Square from where to get the nexts squares
224177
*/
178+
public LinkedList<Node<Maze>> getNextSquares()
179+
{
180+
LinkedList<Node<Maze>> res = new LinkedList<Node<Maze>>();
181+
182+
//Get 4 next squares
183+
LinkedList<Maze> nexts = this.maze.getCurrState().getNexts();
184+
185+
int gCurrent = this.maze.getCurrState().getG();
186+
187+
for(int i = 0; i < nexts.size(); i++)
188+
{
189+
Square tempSq = nexts.get(i).getCurrState();
190+
if(!this.closedSquares.contains(tempSq))
191+
{
192+
if(this.manhattan)
193+
nexts.get(i).getCurrState().calcManhattanH();
194+
else
195+
nexts.get(i).getCurrState().calcEuclidH();
196+
197+
nexts.get(i).getCurrState().incG(gCurrent);
198+
199+
nexts.get(i).getCurrState().calcF();
200+
201+
Node<Maze> tempNode = new Node<Maze>(nexts.get(i));
202+
res.add(tempNode);
203+
}
204+
}
205+
206+
return res;
207+
}
208+
225209
public String getResult()
226210
{
227211
if(this.result == "")
228212
return "No resolution computed, use the solve method first";
229213
else
230214
return this.result;
231215
}
216+
217+
public AbstractCollection<Node<Maze>> getFrontier()
218+
{
219+
return this.frontier;
220+
}
232221
}

src/BFS_Solver.java

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import java.util.AbstractCollection;
12
import java.util.Iterator;
23
import java.util.LinkedList;
34

45
public class BFS_Solver extends Solver
56
{
6-
77
/*
88
* Constructor
99
* m: Maze to solve
@@ -16,10 +16,7 @@ public BFS_Solver(Maze m)
1616
this.closedSquares = new LinkedList<Square>();
1717
}
1818

19-
/*
20-
* Solves the maze with the Breadth First Search algorithm
21-
*/
22-
public void solve()
19+
public String solve()
2320
{
2421
Boolean endfound = false;
2522
this.nodesCounter = 0;
@@ -85,53 +82,15 @@ public void solve()
8582

8683
long endTime = System.currentTimeMillis();
8784

88-
this.setResult(endfound, (endTime - startTime));
89-
}
90-
91-
/*
92-
* Get the next ("walkables") states from the given state
93-
* c: Square from where to get the nexts squares
94-
*/
95-
public LinkedList<Node<Maze>> getNextSquares()
96-
{
97-
LinkedList<Node<Maze>> res = new LinkedList<Node<Maze>>();
98-
99-
//Get 4 next squares
100-
LinkedList<Maze> nexts = this.maze.getCurrState().getNexts();
101-
102-
for(int i = 0; i < nexts.size(); i++)
103-
{
104-
Square tempSq = nexts.get(i).getCurrState();
105-
if(!this.closedSquares.contains(tempSq))
106-
{
107-
//this.closedSquares.add(tempSq);
108-
//this.maze.getGrid()[tempSq.getLine()][tempSq.getCol()].setAttribute("*");
109-
Node<Maze> tempNode = new Node<Maze>(nexts.get(i));
110-
res.add(tempNode); //Add the state
111-
}
112-
}
85+
long time = endTime - startTime;
11386

114-
return res;
115-
}
116-
117-
/*
118-
* Sets the result in this format :
119-
* - Path trace
120-
* - Path length
121-
* - Number of nodes created
122-
* - The maze with the path written
123-
*
124-
* PRIVATE: This method must be called only at the end of the solve method. Any other call may throw errors.
125-
*/
126-
private void setResult(boolean success, long time)
127-
{
12887
this.result = " ____ ____ __ _______ __ _____ __ \r\n" +
129-
" / __ )________ ____ _____/ / /_/ /_ / ____(_)_________/ /_ / ___/___ ____ ___________/ /_ \r\n" +
130-
" / __ / ___/ _ \\/ __ `/ __ / __/ __ \\ / /_ / / ___/ ___/ __/ \\__ \\/ _ \\/ __ `/ ___/ ___/ __ \\\r\n" +
131-
" / /_/ / / / __/ /_/ / /_/ / /_/ / / / / __/ / / / (__ ) /_ ___/ / __/ /_/ / / / /__/ / / /\r\n" +
132-
"/_____/_/ \\___/\\__,_/\\__,_/\\__/_/ /_/ /_/ /_/_/ /____/\\__/ /____/\\___/\\__,_/_/ \\___/_/ /_/ \n";
133-
134-
if(success)
88+
" / __ )________ ____ _____/ / /_/ /_ / ____(_)_________/ /_ / ___/___ ____ ___________/ /_ \r\n" +
89+
" / __ / ___/ _ \\/ __ `/ __ / __/ __ \\ / /_ / / ___/ ___/ __/ \\__ \\/ _ \\/ __ `/ ___/ ___/ __ \\\r\n" +
90+
" / /_/ / / / __/ /_/ / /_/ / /_/ / / / / __/ / / / (__ ) /_ ___/ / __/ /_/ / / / /__/ / / /\r\n" +
91+
"/_____/_/ \\___/\\__,_/\\__,_/\\__/_/ /_/ /_/ /_/_/ /____/\\__/ /____/\\___/\\__,_/_/ \\___/_/ /_/ \n";
92+
93+
if(endfound)
13594
{
13695
this.maze.resetGrid();
13796
Node<Maze> revertedTree = ((LinkedList<Node<Maze>>) this.frontier).removeLast();
@@ -161,11 +120,32 @@ private void setResult(boolean success, long time)
161120
{
162121
this.result += "Failed : Unable to go further and/or end is unreachable.";
163122
}
123+
124+
return this.result;
125+
}
126+
127+
public LinkedList<Node<Maze>> getNextSquares()
128+
{
129+
LinkedList<Node<Maze>> res = new LinkedList<Node<Maze>>();
130+
131+
//Get 4 next squares
132+
LinkedList<Maze> nexts = this.maze.getCurrState().getNexts();
133+
134+
for(int i = 0; i < nexts.size(); i++)
135+
{
136+
Square tempSq = nexts.get(i).getCurrState();
137+
if(!this.closedSquares.contains(tempSq))
138+
{
139+
//this.closedSquares.add(tempSq);
140+
//this.maze.getGrid()[tempSq.getLine()][tempSq.getCol()].setAttribute("*");
141+
Node<Maze> tempNode = new Node<Maze>(nexts.get(i));
142+
res.add(tempNode); //Add the state
143+
}
144+
}
145+
146+
return res;
164147
}
165148

166-
/*
167-
* Returns the result from the last solving
168-
*/
169149
public String getResult()
170150
{
171151
if(result == "")
@@ -174,11 +154,8 @@ public String getResult()
174154
return this.result;
175155
}
176156

177-
/*
178-
* Returns the frontier from the last solving
179-
*/
180-
public LinkedList<Node<Maze>> getFrontier()
157+
public AbstractCollection<Node<Maze>> getFrontier()
181158
{
182-
return (LinkedList<Node<Maze>>) this.frontier;
159+
return this.frontier;
183160
}
184161
}

0 commit comments

Comments
 (0)