61 lines
1.8 KiB
Java
61 lines
1.8 KiB
Java
|
|
package fr.iut_fbleau.Avalam.logic;
|
|||
|
|
|
|||
|
|
import fr.iut_fbleau.Avalam.Color;
|
|||
|
|
import fr.iut_fbleau.Avalam.Tower;
|
|||
|
|
import java.io.*;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* La classe <code>BoardLoader</code> est responsable du chargement d'un plateau Avalam
|
|||
|
|
* depuis un fichier texte externe (généralement Plateau.txt).
|
|||
|
|
*
|
|||
|
|
* Le fichier doit contenir une matrice 9×9 de valeurs numériques séparées par des virgules :
|
|||
|
|
* - 0 : case vide (trou)
|
|||
|
|
* - 1 : pion appartenant au joueur COLOR1 (Jaune)
|
|||
|
|
* - 2 : pion appartenant au joueur COLOR2 (Rouge)
|
|||
|
|
*
|
|||
|
|
* Aucun contrôle de cohérence avancé n'est effectué, le chargeur se contente
|
|||
|
|
* d’interpréter les valeurs comme demandé.
|
|||
|
|
*
|
|||
|
|
* @author
|
|||
|
|
* @version 1.0
|
|||
|
|
*/
|
|||
|
|
public class BoardLoader {
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Charge un plateau Avalam depuis un fichier.
|
|||
|
|
*
|
|||
|
|
* @param path chemin du fichier plateau.
|
|||
|
|
* @return un tableau 9×9 contenant des objets <code>Tower</code> ou <code>null</code>.
|
|||
|
|
*/
|
|||
|
|
public static Tower[][] loadFromFile(String path) {
|
|||
|
|
|
|||
|
|
Tower[][] grid = new Tower[9][9];
|
|||
|
|
|
|||
|
|
try (BufferedReader br = new BufferedReader(new FileReader(new File(path)))) {
|
|||
|
|
|
|||
|
|
String line;
|
|||
|
|
int row = 0;
|
|||
|
|
|
|||
|
|
while ((line = br.readLine()) != null && row < 9) {
|
|||
|
|
|
|||
|
|
String[] parts = line.split(",");
|
|||
|
|
|
|||
|
|
for (int col = 0; col < 9; col++) {
|
|||
|
|
int v = Integer.parseInt(parts[col]);
|
|||
|
|
|
|||
|
|
// Interprétation des valeurs
|
|||
|
|
if (v == 1) grid[row][col] = new Tower(Color.COLOR1);
|
|||
|
|
else if (v == 2) grid[row][col] = new Tower(Color.COLOR2);
|
|||
|
|
else grid[row][col] = null; // Case vide
|
|||
|
|
}
|
|||
|
|
row++;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
e.printStackTrace();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return grid;
|
|||
|
|
}
|
|||
|
|
}
|