Maj BoardLoader + makefile
This commit is contained in:
@@ -3,39 +3,55 @@ package fr.iut_fbleau.Avalam.logic;
|
||||
import fr.iut_fbleau.Avalam.Color;
|
||||
import fr.iut_fbleau.Avalam.Tower;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
public static Tower[][] loadFromFile(String resourcePath) {
|
||||
|
||||
Tower[][] grid = new Tower[9][9];
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
|
||||
InputStream in = BoardLoader.class.getResourceAsStream("/" + resourcePath);
|
||||
|
||||
if (in == null) {
|
||||
System.err.println("❌ Ressource introuvable : /" + resourcePath);
|
||||
return grid;
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
|
||||
|
||||
String line;
|
||||
int row = 0;
|
||||
|
||||
while ((line = br.readLine()) != null && row < 9) {
|
||||
while ((line = reader.readLine()) != null && row < 9) {
|
||||
|
||||
String[] vals = line.split(",");
|
||||
// 🔥 Accepte SOIT les espaces, SOIT les virgules
|
||||
line = line.replace(",", " ");
|
||||
String[] parts = line.trim().split("\\s+");
|
||||
|
||||
for (int col = 0; col < 9; col++) {
|
||||
int v = Integer.parseInt(vals[col].trim());
|
||||
int value = Integer.parseInt(parts[col]);
|
||||
|
||||
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;
|
||||
switch (value) {
|
||||
case 1:
|
||||
grid[row][col] = new Tower(Color.YELLOW);
|
||||
break;
|
||||
case 2:
|
||||
grid[row][col] = new Tower(Color.RED);
|
||||
break;
|
||||
default:
|
||||
grid[row][col] = null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
row++;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user