to stringggg

This commit is contained in:
chloe 2025-05-07 16:30:50 +02:00
parent 93251fd76a
commit a05d192a18
2 changed files with 26 additions and 3 deletions

View File

@ -32,7 +32,7 @@ public class Board {
public void setPiece(boolean isWhite, PieceType type, int x, int y) { public void setPiece(boolean isWhite, PieceType type, int x, int y) {
if (x >= 0 && x < width && y >= 0 && y < height) { if (x >= 0 && x < width && y >= 0 && y < height) {
board[x][y] = new Piece(isWhite, type, x, y); board[x][y] = new Piece(x, y, isWhite, type);
} }
} }
@ -72,8 +72,30 @@ public class Board {
} }
public String toString() { public String toString() {
return "board"; StringBuilder stringboard= new StringBuilder();
} for (int row = height - 1; row >= 0; row--) {
for (int column = 0; column < width; column++) {
Piece piece = board[row][column];
if (piece == null) {
stringboard.append(". ");
} else {
char sym = piece.getType().name().charAt(0);
stringboard.append(piece.isWhite() ? sym : Character.toLowerCase(sym))
.append(' ');
}
}
stringboard.append("\n");
}
stringboard.append(" ");
for (int x = 0; x < width; x++) {
stringboard.append((char)('a' + x)).append(' ');
}
stringboard.append("\n");
stringboard.append("Turn: ").append(isTurnWhite() ? "White" : "Black");
return stringboard.toString();
}
public ArrayList<Piece> getPieces() { public ArrayList<Piece> getPieces() {
ArrayList<Piece> pieces = new ArrayList<>(); ArrayList<Piece> pieces = new ArrayList<>();

View File

@ -14,6 +14,7 @@ public class Piece {
this.type = type; this.type = type;
} }
public int getX() { public int getX() {
return x; return x;
} }