Change in the function to display the color of the pieces on the console

This commit is contained in:
maieu 2025-05-20 11:59:00 +02:00
parent 014faea8ca
commit ad91fec269
1 changed files with 23 additions and 17 deletions

View File

@ -432,36 +432,42 @@ public class Board {
return null;
}
public String[] toFileRep() {
String[] output = new String[height + 2];
StringBuilder top = new StringBuilder(" ");
public String toString() {
StringBuilder sb = new StringBuilder();
// 1) File letters
sb.append(" ");
for (int x = 0; x < width; x++) {
if (x > 0) top.append(" ");
top.append((char)('A' + x));
char fileLetter = (char)('A' + x);
sb.append(" ").append(fileLetter);
}
output[0] = top.toString();
sb.append("\n");
// 2) Ranks with pieces
for (int y = 0; y < height; y++) {
int rank = height - y;
StringBuilder row = new StringBuilder();
row.append(rank).append(" ");
sb.append(rank).append(" ");
for (int x = 0; x < width; x++) {
if (x > 0) row.append(" ");
if (x > 0) sb.append(" ");
Piece p = getPieceAt(x, y);
if (p == null) {
row.append(".");
sb.append("..");
} else {
char c = p.getType().getSummary();
row.append(p.isWhite() ? Character.toUpperCase(c) : Character.toLowerCase(c));
char colorChar = p.isWhite() ? 'w' : 'b';
char typeChar = Character.toLowerCase(p.getType().getSummary()); // standardized as lowercase
sb.append(colorChar).append(typeChar);
}
}
output[y + 1] = row.toString();
sb.append("\n");
}
output[height + 1] = "Turn: " + (turnIsWhite ? "White" : "Black");
return output;
}
// 3) Turn indicator
sb.append("Turn: ").append(turnIsWhite ? "White" : "Black");
return sb.toString();
}
// console print
public void printBoard() {
for (String line : toFileRep()) System.out.println(line);
System.out.println(this);
}
public void playMove(Move move) {