45 lines
1.1 KiB
Java
45 lines
1.1 KiB
Java
package fr.iut_fbleau.Avalam.logic;
|
||
|
||
import fr.iut_fbleau.Avalam.Color;
|
||
import fr.iut_fbleau.Avalam.Tower;
|
||
|
||
import java.io.*;
|
||
|
||
/**
|
||
* Charge un plateau Avalam depuis un fichier texte.
|
||
* Format attendu : matrice 9×9 de 0,1,2 séparés par virgule.
|
||
*/
|
||
public class BoardLoader {
|
||
|
||
public static Tower[][] loadFromFile(String file) {
|
||
Tower[][] grid = new Tower[9][9];
|
||
|
||
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||
|
||
String line;
|
||
int row = 0;
|
||
|
||
while ((line = br.readLine()) != null && row < 9) {
|
||
|
||
String[] vals = line.split(",");
|
||
|
||
for (int col = 0; col < 9; col++) {
|
||
int v = Integer.parseInt(vals[col].trim());
|
||
|
||
switch (v) {
|
||
case 1 -> grid[row][col] = new Tower(Color.YELLOW);
|
||
case 2 -> grid[row][col] = new Tower(Color.RED);
|
||
default -> grid[row][col] = null;
|
||
}
|
||
}
|
||
row++;
|
||
}
|
||
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
}
|
||
|
||
return grid;
|
||
}
|
||
}
|