feat(Map.java): ajout followPath fonction

This commit is contained in:
2025-10-29 15:12:25 +01:00
parent 04b95601ab
commit 5dd84cef90

View File

@@ -39,6 +39,63 @@ public class Map
return new Point[] {start, end}; return new Point[] {start, end};
} }
private boolean followPath(Point start, Point end)
{
// remettre à 0 la liste
pathMap.clear();
// positions
Point current = start;
Point previous = null;
int[][] coord = {
{1, 0}, // haut
{-1, 0}, // bas
{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;
boolean moved = false;
for (int[] pos : coord)
{
int x = current.x + pos[0];
int y = current.y + pos[1];
if ((x >= 0 || map[0].length > x) || (y >= 0 || map.length > y))
{
Point next = new Point(x, y);
if (next.equals(previous))
continue;
CircuitCell type = map[y][x].getType();
if (type == CircuitCell.FINISH || type == CircuitCell.ROAD
|| type == CircuitCell.YROAD)
{
previous = current;
current = next;
moved = true;
break;
}
}
}
// Si il est bloqué
if (!moved) break;
}
return false;
}
public Circuit getElement(int x, int y) public Circuit getElement(int x, int y)
{ {