mirror of
https://github.com/guezoloic/racing-game.git
synced 2026-03-28 18:03:50 +00:00
feat(Map.java): ajout static factory
ajout fonctionnel dans constructeur
This commit is contained in:
42
src/Map.java
42
src/Map.java
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user