From 4405e87f7cb450146d3a385712d7c6d886607eb9 Mon Sep 17 00:00:00 2001 From: maieu Date: Tue, 20 May 2025 21:46:58 +0200 Subject: [PATCH] Modification of the representation of the board so saves function + add of two examples saves --- OOP_2B6_PROJECT/save4 | 9 ++++++ OOP_2B6_PROJECT/save5 | 9 ++++++ OOP_2B6_PROJECT/src/backend/Board.java | 41 +++++++++++--------------- 3 files changed, 35 insertions(+), 24 deletions(-) create mode 100644 OOP_2B6_PROJECT/save4 create mode 100644 OOP_2B6_PROJECT/save5 diff --git a/OOP_2B6_PROJECT/save4 b/OOP_2B6_PROJECT/save4 new file mode 100644 index 0000000..1eafa64 --- /dev/null +++ b/OOP_2B6_PROJECT/save4 @@ -0,0 +1,9 @@ +bR,bN,bB,bQ,bK,bB,bN,bR +bP,bP,bP,bP,bP,..,bP,bP +..,..,..,..,..,..,..,.. +..,..,..,..,..,bP,..,.. +..,..,..,..,..,wP,..,.. +..,..,..,..,..,..,..,.. +wP,wP,wP,wP,wP,..,wP,wP +wR,wN,wB,wQ,wK,wB,wN,wR +W diff --git a/OOP_2B6_PROJECT/save5 b/OOP_2B6_PROJECT/save5 new file mode 100644 index 0000000..660e7fd --- /dev/null +++ b/OOP_2B6_PROJECT/save5 @@ -0,0 +1,9 @@ +bR,bN,bB,..,bK,bB,bN,bR +bP,bP,bP,..,bP,bP,bP,bP +..,..,..,bQ,..,..,..,.. +..,..,..,bP,..,..,..,.. +..,..,..,..,..,wP,..,.. +..,..,..,..,wP,..,..,.. +wP,wP,wP,wP,..,..,wP,wP +wR,wN,wB,wQ,wK,wB,wN,wR +W diff --git a/OOP_2B6_PROJECT/src/backend/Board.java b/OOP_2B6_PROJECT/src/backend/Board.java index 0592514..c6e0fc0 100644 --- a/OOP_2B6_PROJECT/src/backend/Board.java +++ b/OOP_2B6_PROJECT/src/backend/Board.java @@ -55,7 +55,7 @@ public class Board { for (int y = 0; y < 8; y++) { String[] cells = array[y].split(","); for (int x = 0; x < 8; x++) { - if (cells[x].length() == 2) { + if (cells[x].equals("..")) continue; { boolean isWhite = cells[x].charAt(0) == 'w'; PieceType type = PieceType.fromSummary(cells[x].charAt(1)); setPiece(isWhite, type, x, y); @@ -432,42 +432,35 @@ public class Board { return null; } - public String toString() { - StringBuilder sb = new StringBuilder(); + public String[] toFileRep() { + String[] output = new String[height + 1]; // +1 for turn info - // 1) File letters - sb.append(" "); - for (int x = 0; x < width; x++) { - char fileLetter = (char)('A' + x); - sb.append(" ").append(fileLetter); - } - sb.append("\n"); - - // 2) Ranks with pieces for (int y = 0; y < height; y++) { - int rank = height - y; - sb.append(rank).append(" "); + StringBuilder row = new StringBuilder(); for (int x = 0; x < width; x++) { - if (x > 0) sb.append(" "); + if (x > 0) row.append(","); // Restore commas Piece p = getPieceAt(x, y); if (p == null) { - sb.append(".."); + row.append(".."); } else { char colorChar = p.isWhite() ? 'w' : 'b'; - char typeChar = Character.toLowerCase(p.getType().getSummary()); // standardized as lowercase - sb.append(colorChar).append(typeChar); + char typeChar = Character.toUpperCase(p.getType().getSummary()); + row.append(colorChar).append(typeChar); } } - sb.append("\n"); + output[y] = row.toString(); } - // 3) Turn indicator - sb.append("Turn: ").append(turnIsWhite ? "White" : "Black"); - return sb.toString(); + output[height] = turnIsWhite ? "W" : "B"; + + return output; } - // console print + + public void printBoard() { - System.out.println(this); + String[] lines = toFileRep(); + for (String line : lines) {System.out.println(line); + } } public void playMove(Move move) {