Modification of the String toString, make it easier
This commit is contained in:
parent
58f64e97db
commit
b2a5923ded
|
|
@ -86,38 +86,40 @@ public class Board {
|
|||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
// Header (column labels)
|
||||
// Create a temporary grid representation
|
||||
char[][] grid = new char[lineNum][colNum];
|
||||
// Initialize grid with empty squares
|
||||
for (int y = 0; y < lineNum; y++) {
|
||||
for (int x = 0; x < colNum; x++) {
|
||||
grid[y][x] = '.';
|
||||
}
|
||||
}
|
||||
|
||||
// Fill pieces into the grid
|
||||
for (Piece p : pieces) {
|
||||
int x = p.getX();
|
||||
int y = p.getY();
|
||||
if (x >= 0 && x < colNum && y >= 0 && y < lineNum) {
|
||||
grid[y][x] = getPieceSymbol(p);
|
||||
}
|
||||
}
|
||||
|
||||
// Build header
|
||||
sb.append(" ");
|
||||
for (int x = 0; x < colNum; x++) {
|
||||
sb.append((char)('a' + x)).append(" ");
|
||||
sb.append((char) ('a' + x)).append(' ');
|
||||
}
|
||||
sb.append("\n");
|
||||
sb.append('\n');
|
||||
|
||||
// Board rows (8 to 1, since chess starts from bottom)
|
||||
// Build board rows (top to bottom)
|
||||
for (int y = 0; y < lineNum; y++) {
|
||||
sb.append(lineNum - y).append(" "); // Row number (8 to 1)
|
||||
|
||||
sb.append(lineNum - y).append(' ');
|
||||
for (int x = 0; x < colNum; x++) {
|
||||
boolean pieceFound = false;
|
||||
|
||||
// Check if a piece exists at (x, y)
|
||||
for (Piece p : pieces) {
|
||||
if (p.getX() == x && p.getY() == y) {
|
||||
char pieceChar = getPieceSymbol(p);
|
||||
sb.append(pieceChar).append(" ");
|
||||
pieceFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If no piece, add a dot (.)
|
||||
if (!pieceFound) {
|
||||
sb.append(". ");
|
||||
}
|
||||
sb.append(grid[y][x]).append(' ');
|
||||
}
|
||||
sb.append("\n");
|
||||
sb.append('\n');
|
||||
}
|
||||
//TODO
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
// Helper method to convert Piece to symbol (e.g., 'K' for white king)
|
||||
|
|
|
|||
Loading…
Reference in New Issue