Skip to content

Commit 7167604

Browse files
author
Gabriel Derrien
committed
Correction a*, mauvaise compréhension de l'algo
1 parent 0ec2836 commit 7167604

File tree

9 files changed

+29
-63
lines changed

9 files changed

+29
-63
lines changed

bin/.gitignore

Lines changed: 0 additions & 9 deletions
This file was deleted.

bin/AStarSolver.class

-96 Bytes
Binary file not shown.

bin/Main.class

205 Bytes
Binary file not shown.

bin/Maze.class

-1 Bytes
Binary file not shown.

bin/Square.class

-263 Bytes
Binary file not shown.

src/AStarSolver.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ public void solve(boolean manhattan)
6767

6868
//Compute F value of Starting square
6969
if(manhattan)
70-
this.maze.getStart().calcManhattanH(this.maze.getEnd());
70+
this.maze.getStart().calcManhattanH();
7171
else
72-
this.maze.getStart().calcEuclidH(this.maze.getEnd());
72+
this.maze.getStart().calcEuclidH();
7373

7474
this.maze.getStart().calcF();
7575

@@ -88,13 +88,10 @@ public void solve(boolean manhattan)
8888

8989
else
9090
{
91-
//Square current = this.openNodes.remove();
9291
Node<Maze> current = this.openNodes.remove();
9392
this.maze = (Maze) current.getContent();
9493
Square currState = this.maze.getCurrState();
9594

96-
//System.out.println(this.maze.printMaze());
97-
9895
if(currState.getCol() == this.maze.getEnd().getCol() && currState.getLine() == this.maze.getEnd().getLine())
9996
{
10097
Node<Maze> temp = new Node<Maze>(this.maze);
@@ -149,24 +146,20 @@ public LinkedList<Node<Maze>> getNextSquares(Boolean manhattan)
149146

150147
//Get 4 next squares
151148
LinkedList<Maze> nexts = this.maze.getCurrState().getNexts();
152-
Square start = this.maze.getStart();
153-
Square end = this.maze.getEnd();
149+
150+
int gCurrent = this.maze.getCurrState().getG();
154151

155152
for(int i = 0; i < nexts.size(); i++)
156153
{
157154
Square tempSq = nexts.get(i).getCurrState();
158155
if(!this.closedSquares.contains(tempSq))
159156
{
160157
if(manhattan)
161-
{
162-
nexts.get(i).getCurrState().calcManhattanG(start);
163-
nexts.get(i).getCurrState().calcManhattanH(end);
164-
}
158+
nexts.get(i).getCurrState().calcManhattanH();
165159
else
166-
{
167-
nexts.get(i).getCurrState().calcEuclidG(start);
168-
nexts.get(i).getCurrState().calcEuclidH(end);
169-
}
160+
nexts.get(i).getCurrState().calcEuclidH();
161+
162+
nexts.get(i).getCurrState().incG(gCurrent);
170163

171164
nexts.get(i).getCurrState().calcF();
172165

src/Main.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ public static void main(String[] args) throws IOException
2929

3030
//System.out.println(lab2.toString());
3131

32-
//BFS_Solver b2 = new BFS_Solver(lab2);
33-
//DFS_Solver d2 = new DFS_Solver(lab2);
32+
BFS_Solver b2 = new BFS_Solver(lab2);
33+
DFS_Solver d2 = new DFS_Solver(lab2);
3434
AStarSolver a2 = new AStarSolver(lab2);
3535

36-
//b2.solve();
37-
//d2.solve();
36+
b2.solve();
37+
d2.solve();
3838
a2.solve(true);
3939

40-
//System.out.println(b2.getResult());
41-
//System.out.println(d2.getResult());
40+
System.out.println(b2.getResult());
41+
System.out.println(d2.getResult());
4242
System.out.println(a2.getResult());
4343

4444
a2.solve(false);

src/Maze.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ public void setOrder(char[] newOrder)
229229
*/
230230
public void resetOrder()
231231
{
232-
char[] no = {'N', 'E', 'S', 'W'};
233-
this.order = no;
232+
char[] o = {'N', 'E', 'S', 'W'};
233+
this.order = o;
234234
}
235235

236236
/*

src/Square.java

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Square
1010
private boolean wall;
1111
public int depth;
1212

13-
private double g;
13+
private int g;
1414
private double h;
1515
private double f;
1616

@@ -248,24 +248,22 @@ public double getH()
248248
* Computes the H value with the Manhattan distance
249249
* end: The ending Square in the maze
250250
*/
251-
public void calcManhattanH(Square end)
251+
public void calcManhattanH()
252252
{
253-
if(end.getAttribute() == "E")
254-
this.h = Math.abs( this.getLine() - end.getLine() )
255-
+ Math.abs( this.getCol() - end.getCol() );
253+
this.h = Math.abs( this.getLine() - this.maze.getEnd().getLine() )
254+
+ Math.abs( this.getCol() - this.maze.getEnd().getCol() );
256255
}
257256

258257
/*
259258
* Computes the H value with the Euclidean distance
260259
* end: The ending Square in the maze
261260
*/
262-
public void calcEuclidH(Square end)
261+
public void calcEuclidH()
263262
{
264-
if(end.getAttribute() == "E")
265-
this.h = Math.sqrt(
266-
Math.pow( this.getLine() - end.getLine(), 2 )
267-
+ Math.pow( this.getCol() - end.getCol(), 2 )
268-
);
263+
this.h = Math.sqrt(
264+
Math.pow( this.getLine() - this.maze.getEnd().getLine(), 2 )
265+
+ Math.pow( this.getCol() - this.maze.getEnd().getCol(), 2 )
266+
);
269267
}
270268

271269
//----------------------
@@ -275,33 +273,17 @@ public void calcEuclidH(Square end)
275273
/*
276274
* Returns G value
277275
*/
278-
public double getG()
276+
public int getG()
279277
{
280278
return g;
281279
}
282280

283281
/*
284-
* Computes the G value with the Manhattan distance
285-
* start: The starting Square in the maze
282+
* Increases the G value
286283
*/
287-
public void calcManhattanG(Square start)
284+
public void incG(int prev)
288285
{
289-
if(start.getAttribute() == "S")
290-
this.g = Math.abs( this.getLine() - start.getLine() )
291-
+ Math.abs( this.getCol() - start.getCol() );
292-
}
293-
294-
/*
295-
* Computes the G value with the Euclidean distance
296-
* start: The starting Square in the maze
297-
*/
298-
public void calcEuclidG(Square start)
299-
{
300-
if(start.getAttribute() == "S")
301-
this.g = Math.sqrt(
302-
Math.pow( this.getLine() - start.getLine(), 2 )
303-
+ Math.pow( this.getCol() - start.getCol(), 2 )
304-
);
286+
this.g = 1 + prev;
305287
}
306288

307289
//----------------------

0 commit comments

Comments
 (0)