feat(Map.java): ajout static factory

ajout fonctionnel dans constructeur
This commit is contained in:
2025-10-29 17:35:46 +01:00
parent c9affba9f5
commit 5a62b58e9e

View File

@@ -1,12 +1,42 @@
import java.awt.Point;
import java.util.ArrayList;
import java.util.function.Function;
public class Map
{
private CircuitCell[][] map;
private Circuit[][] map;
private ArrayList<Point> pathMap;
private Map(CircuitCell[][] map)
public static <T>Map create(Function<T, Circuit> fn, T[][] map)
{
int lenX = map[0].length;
int lenY = map.length;
Circuit[][] newmap = new Circuit[lenY][lenX];
for (int y = 0; y < lenY; y++)
{
for (int x = 0; x < lenX; x++)
{
newmap[y][x] = fn.apply(map[y][x]);
}
}
return new Map(newmap);
}
public static Map fromInts(Integer[][] map)
{
return create((i) -> switch (i) {
case 0 -> new Circuit(CircuitCell.EMPTY);
case 1 -> new Circuit(CircuitCell.ROAD);
case 2 -> new Circuit(CircuitCell.START);
case 3 -> new Circuit(CircuitCell.FINISH);
default -> new Circuit(CircuitCell.YROAD, i);
}, map);
}
private Map(Circuit[][] map)
{
this.map = map;
boolean isPossible = this.buildPath();
@@ -41,7 +71,7 @@ public class Map
{
for (int j = 0; j < map.length; j++)
{
switch (map[i][j])
switch (map[i][j].getType())
{
case CircuitCell.START:
if (start == null) start = new Point(j, i);
@@ -100,9 +130,9 @@ public class Map
if (next.equals(previous))
continue;
CircuitCell type = map[y][x];
CircuitCell type = map[y][x].getType();
if ((type == CircuitCell.ROAD || type == CircuitCell.YROAD) ||
(type == CircuitCell.FINISH && map[current.y][current.x] == CircuitCell.ROAD))
(type == CircuitCell.FINISH && map[current.y][current.x].getType() == CircuitCell.ROAD))
{
previous = current;
current = next;
@@ -121,6 +151,6 @@ public class Map
public CircuitCell getElement(int x, int y)
{
return map[y][x];
return map[y][x].getType();
}
}