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++) {
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) {