diff --git a/src/GomokuBoard.java b/src/GomokuBoard.java index e864dab..1580e28 100644 --- a/src/GomokuBoard.java +++ b/src/GomokuBoard.java @@ -95,7 +95,9 @@ public class GomokuBoard{ public GomokuCell get(int x, int y){ int i = 0, j = 0; GomokuCell act = this.firstCell; - while (i != x && j != y){ + // System.out.println("x: "+x+" y: "+y); + while (i != x || j != y){ + // System.out.println("i: "+i+" j: "+j); Cardinal c = Cardinal.SE; if (i < x) i++; else c = Cardinal.E; @@ -191,7 +193,7 @@ public class GomokuBoard{ public String toString() { StringBuilder out = new StringBuilder(); GomokuCell[][] cells = this.getAllsCells(); - for (GomokuCell[] line : cells) { + for (GomokuCell[] line : cells) { for (GomokuCell c : line) { out.append(c.toString()); } diff --git a/src/GomokuCell.java b/src/GomokuCell.java index 53d41c0..cab9345 100644 --- a/src/GomokuCell.java +++ b/src/GomokuCell.java @@ -151,9 +151,9 @@ public class GomokuCell{ case Color.BLACK: return "X"; case Color.WHITE: - return "0"; + return "O"; default: - return "BUG"; + throw new IllegalStateException("Unexpected value: " + this.getState()); } } } diff --git a/src/GomokuGame.java b/src/GomokuGame.java index 5291e5e..10eee43 100644 --- a/src/GomokuGame.java +++ b/src/GomokuGame.java @@ -1,4 +1,8 @@ import java.nio.file.Path; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.file.Files; public class GomokuGame { @@ -11,9 +15,25 @@ public class GomokuGame { private int boardWidth; private int boardHeight; + private GomokuBoard board; + + Color colorP1; + int currP; + int nbTokens1, nbTokens2; public static void main(String[] args) { - System.out.println("Hello World!"); + // Test + GomokuGame g = new GomokuGame(); + System.out.println(g.load(Path.of(".cache/test.dat"))); + System.out.println(g.getBoard()); + System.out.println(g.save(Path.of(".cache/save.dat"))); + + for (int i = 0; i < g.boardHeight; ++i) { + for (int j = 0; j < g.boardWidth; ++j) { + System.out.print(g.getBoard().get(i ,j)); + } + System.out.println(); + } } public void newGame() { @@ -24,15 +44,130 @@ public class GomokuGame { } - public void save(Path filename) { + public boolean save(Path filename) { + // save the game state to the file + // 1. Open the file + // 2. Write the first line with: + // w h colorP1 currP nbTokens1 nbTokens2 + // 3. Write the next h lines with the board + // 4. Close the file + // 5. Return true if successful, false otherwise + // if filename is null, save to default file in ".cache/save.dat" + if (filename == null) { + filename = Path.of(".cache/save.dat"); + } + + try (BufferedWriter writer = Files.newBufferedWriter(filename)) { + // Write the first line + writer.write(String.format("%d %d %s %d %d %d%n", + this.boardWidth, this.boardHeight, colorP1, currP, nbTokens1, nbTokens2)); + + // Write the board + for (int i = 0; i < boardHeight; ++i) { + for (int j = 0; j < boardWidth; ++j) { + char c; + switch (board.get(i, j).getState()) { + case BLACK: c = 'X'; break; + case WHITE: c = 'O'; break; + case NIL: c = '.'; break; + default: throw new IllegalStateException( + String.format("Unexpected value at cell (%d, %d): %s", i, j, board.get(i, j).getState()) + ); + } + writer.write(c); + } + writer.newLine(); + } + } catch (IOException e) { + System.err.println("Error writing file: " + e.getMessage()); + return false; + } catch (Exception e) { + System.err.println("Unexpected error: " + e.getMessage()); + return false; + } + return true; } - public void load(Path filename) { + public boolean load(Path filename) { + // load the game state from the file + // 1. Open the file + // 2. Read the first line to get: + // w h colorP1 currP nbTokens1 nbTokens2 + // 3. Read the next h lines to get the board + // 4. Close the file + // 5. Initialize the board with the read values + int w, h; + Color[][] colors; + + try (BufferedReader reader = Files.newBufferedReader(filename)) { + String line = reader.readLine(); + if (line == null) { + throw new IOException("Empty save file"); + } + + String[] parts = line.split(" "); + if (parts.length != 6) { + throw new IOException("Invalid save file format"); + } + w = Integer.parseInt(parts[0]); + h = Integer.parseInt(parts[1]); + colorP1 = Color.valueOf(parts[2]); + currP = Integer.parseInt(parts[3]); + nbTokens1 = Integer.parseInt(parts[4]); + nbTokens2 = Integer.parseInt(parts[5]); + + if (w <= 0 || h <= 0) { + throw new IllegalArgumentException("Invalid board dimensions"); + } + + colors = new Color[h][w]; + + for (int i = 0; i < h; ++i) { + line = reader.readLine(); + if (line == null) { + throw new IOException("Unexpected end of file"); + } + + if (line.length() != w) { + throw new IOException("Invalid board line format"); + } + + for (int j = 0; j < w; ++j) { + switch (line.charAt(j)) { + case 'X': colors[i][j] = Color.BLACK; break; + case 'O': colors[i][j] = Color.WHITE; break; + case '.': colors[i][j] = Color.NIL; break; + default: + throw new IllegalArgumentException("Invalid color: " + line.charAt(j)); + } + } + } + + } catch (IOException e) { + System.err.println("Error reading file: " + e.getMessage()); + return false; + } catch (NumberFormatException e) { + System.err.println("Invalid number format: " + e.getMessage()); + return false; + } catch (IllegalArgumentException e) { + System.err.println("Invalid color format: " + e.getMessage()); + return false; + } catch (Exception e) { + System.err.println("Unexpected error: " + e.getMessage()); + return false; + } + + // Initialize the board with the read values + this.boardWidth = w; + this.boardHeight = h; + this.board = new GomokuBoard(w, h, colors); + + return true; } public GomokuBoard getBoard() { - return new GomokuBoard(0,0); + return this.board; } }