Remove board dimensions and update load() method

This commit is contained in:
Dorian HAMDANI
2025-04-07 08:57:53 +02:00
parent 707ad73291
commit 40e61c452d

View File

@@ -13,10 +13,10 @@ public class GomokuGame {
private Player player1; private Player player1;
private Player player2; private Player player2;
private int boardWidth;
private int boardHeight;
private GomokuBoard board; private GomokuBoard board;
private GomokuRenderer renderer;
Color colorP1; Color colorP1;
int currP; int currP;
int nbTokens1, nbTokens2; int nbTokens1, nbTokens2;
@@ -25,15 +25,11 @@ public class GomokuGame {
// Test // Test
GomokuGame g = new GomokuGame(); GomokuGame g = new GomokuGame();
System.out.println(g.load(Path.of(".cache/test.dat"))); System.out.println(g.load(Path.of(".cache/test.dat")));
System.out.println(g.getBoard()); g.renderer = new ConsoleRenderer();
System.out.println(g.save(Path.of(".cache/save.dat"))); g.renderer.init(g);
g.renderer.update();
for (int i = 0; i < g.boardHeight; ++i) { g.board.expandBoard(Cardinal.SE);
for (int j = 0; j < g.boardWidth; ++j) { g.renderer.update();
System.out.print(g.getBoard().get(i ,j));
}
System.out.println();
}
} }
public void newGame() { public void newGame() {
@@ -61,11 +57,17 @@ public class GomokuGame {
try (BufferedWriter writer = Files.newBufferedWriter(filename)) { try (BufferedWriter writer = Files.newBufferedWriter(filename)) {
// Write the first line // Write the first line
writer.write(String.format("%d %d %s %d %d %d%n", writer.write(String.format("%d %d %s %d %d %d%n",
this.boardWidth, this.boardHeight, colorP1, currP, nbTokens1, nbTokens2)); this.getBoard().getWidth(),
this.getBoard().getHeight(),
colorP1,
currP,
nbTokens1,
nbTokens2
));
// Write the board // Write the board
for (int i = 0; i < boardHeight; ++i) { for (int i = 0; i < this.getBoard().getHeight(); ++i) {
for (int j = 0; j < boardWidth; ++j) { for (int j = 0; j < this.getBoard().getWidth(); ++j) {
char c; char c;
switch (board.get(i, j).getState()) { switch (board.get(i, j).getState()) {
case BLACK: c = 'X'; break; case BLACK: c = 'X'; break;
@@ -160,8 +162,6 @@ public class GomokuGame {
} }
// Initialize the board with the read values // Initialize the board with the read values
this.boardWidth = w;
this.boardHeight = h;
this.board = new GomokuBoard(w, h, colors); this.board = new GomokuBoard(w, h, colors);
return true; return true;