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:
2025-10-29 16:00:21 +01:00
parent df30cd0716
commit 072b37ece7

View File

@@ -9,7 +9,13 @@ public class Map
public Map(Circuit[][] map) public Map(Circuit[][] map)
{ {
this.map = 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() private boolean buildPath()
@@ -20,8 +26,9 @@ public class Map
return false; return false;
Point start = p[0]; Point start = p[0];
Point end = p[0]; Point end = p[1];
this.pathMap = new ArrayList<>();
return followPath(start, end); return followPath(start, end);
} }
@@ -37,11 +44,11 @@ public class Map
switch (map[i][j].getType()) switch (map[i][j].getType())
{ {
case CircuitCell.START: case CircuitCell.START:
if (start == null) start = new Point(i, j); if (start == null) start = new Point(j, i);
else return null; else return null;
break; break;
case CircuitCell.FINISH: case CircuitCell.FINISH:
if (end == null) end = new Point(i, j); if (end == null) end = new Point(j, i);
else return null; else return null;
break; break;
default: default:
@@ -49,7 +56,7 @@ public class Map
} }
} }
} }
if (start == null || end == null) return null;
return new Point[] {start, end}; return new Point[] {start, end};
} }
@@ -68,14 +75,14 @@ public class Map
{0, -1}, // gauche {0, -1}, // gauche
{0, 1} // droite {0, 1} // droite
}; };
// sécurité pour éviter les boucles infinie // sécurité pour éviter les boucles infinie
int step = 0; int step = 0;
int max = map.length * map[0].length; int max = map.length * map[0].length;
for (; step < max; step++) for (; step < max; step++)
{ {
pathMap.add(current); pathMap.add(current);
if (current.equals(end)) if (current.equals(end))
return true; return true;
@@ -86,7 +93,7 @@ public class Map
int x = current.x + pos[0]; int x = current.x + pos[0];
int y = current.y + pos[1]; 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); Point next = new Point(x, y);
@@ -94,8 +101,8 @@ public class Map
continue; continue;
CircuitCell type = map[y][x].getType(); CircuitCell type = map[y][x].getType();
if (type == CircuitCell.FINISH || type == CircuitCell.ROAD if ((type == CircuitCell.ROAD || type == CircuitCell.YROAD) ||
|| type == CircuitCell.YROAD) (type == CircuitCell.FINISH && map[current.y][current.x] == CircuitCell.ROAD))
{ {
previous = current; previous = current;
current = next; current = next;
@@ -104,6 +111,7 @@ public class Map
} }
} }
} }
// Si il est bloqué // Si il est bloqué
if (!moved) break; if (!moved) break;
} }