mirror of
https://github.com/guezoloic/racing-game.git
synced 2026-03-31 19:21:38 +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.awt.Point;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class Map
|
public class Map
|
||||||
{
|
{
|
||||||
private CircuitCell[][] map;
|
private Circuit[][] map;
|
||||||
private ArrayList<Point> pathMap;
|
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;
|
this.map = map;
|
||||||
boolean isPossible = this.buildPath();
|
boolean isPossible = this.buildPath();
|
||||||
@@ -41,7 +71,7 @@ public class Map
|
|||||||
{
|
{
|
||||||
for (int j = 0; j < map.length; j++)
|
for (int j = 0; j < map.length; j++)
|
||||||
{
|
{
|
||||||
switch (map[i][j])
|
switch (map[i][j].getType())
|
||||||
{
|
{
|
||||||
case CircuitCell.START:
|
case CircuitCell.START:
|
||||||
if (start == null) start = new Point(j, i);
|
if (start == null) start = new Point(j, i);
|
||||||
@@ -100,9 +130,9 @@ public class Map
|
|||||||
if (next.equals(previous))
|
if (next.equals(previous))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
CircuitCell type = map[y][x];
|
CircuitCell type = map[y][x].getType();
|
||||||
if ((type == CircuitCell.ROAD || type == CircuitCell.YROAD) ||
|
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;
|
previous = current;
|
||||||
current = next;
|
current = next;
|
||||||
@@ -121,6 +151,6 @@ public class Map
|
|||||||
|
|
||||||
public CircuitCell getElement(int x, int y)
|
public CircuitCell getElement(int x, int y)
|
||||||
{
|
{
|
||||||
return map[y][x];
|
return map[y][x].getType();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user