mirror of
https://github.com/guezoloic/racing-game.git
synced 2026-03-28 18:03:50 +00:00
fix(Map.java): modif des fonc bind, follow et build path
- `bindPath`: `Point(i, j)` -> `Point(j, i)` - `buildPath`: init ArrayList - `followPath`: ajout current == ROAD ET next == FINISH
This commit is contained in:
28
src/Map.java
28
src/Map.java
@@ -9,7 +9,13 @@ public class Map
|
||||
public Map(Circuit[][] map)
|
||||
{
|
||||
this.map = map;
|
||||
this.buildPath();
|
||||
boolean isPossible = this.buildPath();
|
||||
|
||||
if (!isPossible)
|
||||
{
|
||||
System.err.println("La map contient des doublons ou est impossible à finir!");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean buildPath()
|
||||
@@ -20,8 +26,9 @@ public class Map
|
||||
return false;
|
||||
|
||||
Point start = p[0];
|
||||
Point end = p[0];
|
||||
Point end = p[1];
|
||||
|
||||
this.pathMap = new ArrayList<>();
|
||||
return followPath(start, end);
|
||||
}
|
||||
|
||||
@@ -37,11 +44,11 @@ public class Map
|
||||
switch (map[i][j].getType())
|
||||
{
|
||||
case CircuitCell.START:
|
||||
if (start == null) start = new Point(i, j);
|
||||
if (start == null) start = new Point(j, i);
|
||||
else return null;
|
||||
break;
|
||||
case CircuitCell.FINISH:
|
||||
if (end == null) end = new Point(i, j);
|
||||
if (end == null) end = new Point(j, i);
|
||||
else return null;
|
||||
break;
|
||||
default:
|
||||
@@ -49,7 +56,7 @@ public class Map
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (start == null || end == null) return null;
|
||||
return new Point[] {start, end};
|
||||
}
|
||||
|
||||
@@ -68,14 +75,14 @@ public class Map
|
||||
{0, -1}, // gauche
|
||||
{0, 1} // droite
|
||||
};
|
||||
|
||||
|
||||
// sécurité pour éviter les boucles infinie
|
||||
int step = 0;
|
||||
int max = map.length * map[0].length;
|
||||
for (; step < max; step++)
|
||||
{
|
||||
pathMap.add(current);
|
||||
|
||||
|
||||
if (current.equals(end))
|
||||
return true;
|
||||
|
||||
@@ -86,7 +93,7 @@ public class Map
|
||||
int x = current.x + pos[0];
|
||||
int y = current.y + pos[1];
|
||||
|
||||
if ((x >= 0 || map[0].length > x) || (y >= 0 || map.length > y))
|
||||
if ((x >= 0 && map[0].length > x) && (y >= 0 && map.length > y))
|
||||
{
|
||||
Point next = new Point(x, y);
|
||||
|
||||
@@ -94,8 +101,8 @@ public class Map
|
||||
continue;
|
||||
|
||||
CircuitCell type = map[y][x].getType();
|
||||
if (type == CircuitCell.FINISH || type == CircuitCell.ROAD
|
||||
|| type == CircuitCell.YROAD)
|
||||
if ((type == CircuitCell.ROAD || type == CircuitCell.YROAD) ||
|
||||
(type == CircuitCell.FINISH && map[current.y][current.x] == CircuitCell.ROAD))
|
||||
{
|
||||
previous = current;
|
||||
current = next;
|
||||
@@ -104,6 +111,7 @@ public class Map
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Si il est bloqué
|
||||
if (!moved) break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user