mirror of
https://github.com/guezoloic/racing-game.git
synced 2026-03-28 18:03:50 +00:00
chore(Map.java): init javadoc (va très vite changer)
This commit is contained in:
116
src/Map.java
116
src/Map.java
@@ -2,11 +2,35 @@ import java.awt.Point;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p> <code>Map</code> représente le circuit de course.</p>
|
||||||
|
* <p>Cette classe contient: </p>
|
||||||
|
* <ul>
|
||||||
|
* <li>Un tableau 2D de CircuitCell</li>
|
||||||
|
* <li>Une liste de Points qui représente le chemin pour la voiture entre <code>START</code> et <code>FINISH</code></li>
|
||||||
|
* </ul>
|
||||||
|
*/
|
||||||
public class Map
|
public class Map
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Tableau 2D représentant le circuit.
|
||||||
|
* Chaque élément est une instance de <code>Circuit</code>.
|
||||||
|
*/
|
||||||
private Circuit[][] map;
|
private Circuit[][] map;
|
||||||
|
/**
|
||||||
|
* Liste des coordonnées représentant le chemin du START au FINISH.
|
||||||
|
*/
|
||||||
private ArrayList<Point> pathMap;
|
private ArrayList<Point> pathMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Crée une nouvelle instance de Map à partir d'un tableau générique
|
||||||
|
* et d'une fonction de transformation.
|
||||||
|
*
|
||||||
|
* @param <T> Type des éléments du tableau source
|
||||||
|
* @param fn Fonction qui transforme un élément T en Circuit
|
||||||
|
* @param array Tableau 2D d'éléments T
|
||||||
|
* @return Une nouvelle instance de <code>Map</code>
|
||||||
|
*/
|
||||||
public static <T>Map create(Function<T, Circuit> fn, T[][] map)
|
public static <T>Map create(Function<T, Circuit> fn, T[][] map)
|
||||||
{
|
{
|
||||||
int lenX = map[0].length;
|
int lenX = map[0].length;
|
||||||
@@ -25,6 +49,19 @@ public class Map
|
|||||||
return new Map(newmap);
|
return new Map(newmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Crée une map à partir d'un tableau d'entiers.
|
||||||
|
* <ul>
|
||||||
|
* <li>0 -> EMPTY</li>
|
||||||
|
* <li>1 -> ROAD</li>
|
||||||
|
* <li>2 -> START</li>
|
||||||
|
* <li>3 -> FINISH</li>
|
||||||
|
* <li>autres -> YROAD avec la valeur associée</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @param array Tableau 2D d'entiers
|
||||||
|
* @return Une nouvelle instance de <code>Map</code>
|
||||||
|
*/
|
||||||
public static Map fromInts(Integer[][] map)
|
public static Map fromInts(Integer[][] map)
|
||||||
{
|
{
|
||||||
return create((i) -> switch (i) {
|
return create((i) -> switch (i) {
|
||||||
@@ -36,6 +73,19 @@ public class Map
|
|||||||
}, map);
|
}, map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Crée une map à partir d'un tableau de caractères.
|
||||||
|
* <ul>
|
||||||
|
* <li>'#' -> ROAD</li>
|
||||||
|
* <li>'S' -> START</li>
|
||||||
|
* <li>'F' -> FINISH</li>
|
||||||
|
* <li>'0'-'9' -> YROAD avec valeur correspondante</li>
|
||||||
|
* <li>autres -> EMPTY</li>
|
||||||
|
* </ul>
|
||||||
|
*
|
||||||
|
* @param array Tableau 2D de caractères
|
||||||
|
* @return Une nouvelle instance de <code>Map</code>
|
||||||
|
*/
|
||||||
public static Map fromChars(Character[][] map)
|
public static Map fromChars(Character[][] map)
|
||||||
{
|
{
|
||||||
return create((i) -> switch (i) {
|
return create((i) -> switch (i) {
|
||||||
@@ -48,6 +98,13 @@ public class Map
|
|||||||
}, map);
|
}, map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructeur privé utilisé par les méthodes statiques de création.
|
||||||
|
* Initialise la map et construit le chemin.
|
||||||
|
* Si la map est invalide ou impossible à résoudre, termine le programme.
|
||||||
|
*
|
||||||
|
* @param map Tableau 2D de <code>Circuit</code>
|
||||||
|
*/
|
||||||
private Map(Circuit[][] map)
|
private Map(Circuit[][] map)
|
||||||
{
|
{
|
||||||
this.map = map;
|
this.map = map;
|
||||||
@@ -59,7 +116,12 @@ public class Map
|
|||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construit le chemin entre START et FINISH.
|
||||||
|
*
|
||||||
|
* @return true si un chemin valide existe, false sinon
|
||||||
|
*/
|
||||||
private boolean buildPath()
|
private boolean buildPath()
|
||||||
{
|
{
|
||||||
Point[] p = bindPath();
|
Point[] p = bindPath();
|
||||||
@@ -74,6 +136,11 @@ public class Map
|
|||||||
return followPath(start, end);
|
return followPath(start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cherche les points START et FINISH sur la map.
|
||||||
|
*
|
||||||
|
* @return Un tableau de 2 éléments : {START, FINISH} ou null si la map est invalide
|
||||||
|
*/
|
||||||
private Point[] bindPath()
|
private Point[] bindPath()
|
||||||
{
|
{
|
||||||
Point start = null;
|
Point start = null;
|
||||||
@@ -102,6 +169,14 @@ public class Map
|
|||||||
return new Point[] {start, end};
|
return new Point[] {start, end};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suivi du chemin depuis START jusqu'à FINISH.
|
||||||
|
* Parcours les cases ROAD et YROAD jusqu'à atteindre FINISH.
|
||||||
|
*
|
||||||
|
* @param start Point de départ
|
||||||
|
* @param end Point d'arrivée
|
||||||
|
* @return true si un chemin complet a été trouvé, false sinon
|
||||||
|
*/
|
||||||
private boolean followPath(Point start, Point end)
|
private boolean followPath(Point start, Point end)
|
||||||
{
|
{
|
||||||
// remettre à 0 la liste
|
// remettre à 0 la liste
|
||||||
@@ -161,11 +236,46 @@ public class Map
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CircuitCell getElement(int x, int y)
|
/**
|
||||||
|
* Retourne l'objet Circuit à la position (x, y).
|
||||||
|
*
|
||||||
|
* @param x Coordonnée X
|
||||||
|
* @param y Coordonnée Y
|
||||||
|
* @return L'objet <code>Circuit</code>
|
||||||
|
*/
|
||||||
|
public Circuit getElement(int x, int y)
|
||||||
{
|
{
|
||||||
return map[y][x].getType();
|
return map[y][x];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retourne le type de cellule (CircuitCell) à la position (x, y).
|
||||||
|
*
|
||||||
|
* @param x Coordonnée X
|
||||||
|
* @param y Coordonnée Y
|
||||||
|
* @return Le type de cellule <code>CircuitCell</code>
|
||||||
|
*/
|
||||||
|
public CircuitCell getCell(int x, int y)
|
||||||
|
{
|
||||||
|
return getElement(x, y).getType();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retourne le point du chemin à l'indice donné.
|
||||||
|
*
|
||||||
|
* @param index Indice dans le chemin
|
||||||
|
* @return Point correspondant
|
||||||
|
*/
|
||||||
|
public Point getPath(int i)
|
||||||
|
{
|
||||||
|
return this.pathMap.get(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retourne la taille du chemin trouvé.
|
||||||
|
*
|
||||||
|
* @return Nombre de points dans le chemin
|
||||||
|
*/
|
||||||
public int getPathSize()
|
public int getPathSize()
|
||||||
{
|
{
|
||||||
return this.pathMap.size();
|
return this.pathMap.size();
|
||||||
|
|||||||
Reference in New Issue
Block a user