Files

61 lines
1.7 KiB
Java
Raw Permalink Normal View History

package fr.iut_fbleau.Avalam.logic;
import fr.iut_fbleau.Avalam.Color;
import fr.iut_fbleau.Avalam.Tower;
2025-11-25 16:02:23 -05:00
2025-11-27 13:06:14 +01:00
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class BoardLoader {
2025-11-27 13:06:14 +01:00
public static Tower[][] loadFromFile(String resourcePath) {
Tower[][] grid = new Tower[9][9];
2025-11-27 13:06:14 +01:00
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;
2025-11-27 13:06:14 +01:00
while ((line = reader.readLine()) != null && row < 9) {
2025-11-27 13:06:14 +01:00
// 🔥 Accepte SOIT les espaces, SOIT les virgules
line = line.replace(",", " ");
String[] parts = line.trim().split("\\s+");
for (int col = 0; col < 9; col++) {
2025-11-27 13:06:14 +01:00
int value = Integer.parseInt(parts[col]);
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;
2025-11-25 16:02:23 -05:00
}
}
2025-11-27 13:06:14 +01:00
row++;
}
2025-11-27 13:06:14 +01:00
} catch (IOException e) {
e.printStackTrace();
}
return grid;
}
}