Modification of the representation of the board so saves function + add

of two examples saves
This commit is contained in:
maieu 2025-05-20 21:46:58 +02:00
parent ad91fec269
commit 4405e87f7c
3 changed files with 35 additions and 24 deletions

9
OOP_2B6_PROJECT/save4 Normal file
View File

@ -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

9
OOP_2B6_PROJECT/save5 Normal file
View File

@ -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

View File

@ -55,7 +55,7 @@ public class Board {
for (int y = 0; y < 8; y++) { for (int y = 0; y < 8; y++) {
String[] cells = array[y].split(","); String[] cells = array[y].split(",");
for (int x = 0; x < 8; x++) { for (int x = 0; x < 8; x++) {
if (cells[x].length() == 2) { if (cells[x].equals("..")) continue; {
boolean isWhite = cells[x].charAt(0) == 'w'; boolean isWhite = cells[x].charAt(0) == 'w';
PieceType type = PieceType.fromSummary(cells[x].charAt(1)); PieceType type = PieceType.fromSummary(cells[x].charAt(1));
setPiece(isWhite, type, x, y); setPiece(isWhite, type, x, y);
@ -432,42 +432,35 @@ public class Board {
return null; return null;
} }
public String toString() { public String[] toFileRep() {
StringBuilder sb = new StringBuilder(); 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++) { for (int y = 0; y < height; y++) {
int rank = height - y; StringBuilder row = new StringBuilder();
sb.append(rank).append(" ");
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
if (x > 0) sb.append(" "); if (x > 0) row.append(","); // Restore commas
Piece p = getPieceAt(x, y); Piece p = getPieceAt(x, y);
if (p == null) { if (p == null) {
sb.append(".."); row.append("..");
} else { } else {
char colorChar = p.isWhite() ? 'w' : 'b'; char colorChar = p.isWhite() ? 'w' : 'b';
char typeChar = Character.toLowerCase(p.getType().getSummary()); // standardized as lowercase char typeChar = Character.toUpperCase(p.getType().getSummary());
sb.append(colorChar).append(typeChar); row.append(colorChar).append(typeChar);
} }
} }
sb.append("\n"); output[y] = row.toString();
} }
// 3) Turn indicator output[height] = turnIsWhite ? "W" : "B";
sb.append("Turn: ").append(turnIsWhite ? "White" : "Black");
return sb.toString(); return output;
} }
// console print
public void printBoard() { public void printBoard() {
System.out.println(this); String[] lines = toFileRep();
for (String line : lines) {System.out.println(line);
}
} }
public void playMove(Move move) { public void playMove(Move move) {