This commit is contained in:
Jérôme BEDIER 2025-05-16 09:14:13 +02:00
commit d8374c2061
1 changed files with 46 additions and 4 deletions

View File

@ -116,6 +116,8 @@ public class Board {
return pieces;
}
public void userTouch(int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= height) return;
@ -166,14 +168,54 @@ public class Board {
/* saving-loading feature :*/
public String[] toFileRep() {
//TODO
return null;
String[] lines = new String[height + 1]; // one line per row + 1 for turn
for (int y = 0; y < height; y++) {
StringBuilder sb = new StringBuilder();
for (int x = 0; x < width; x++) {
Piece piece = board[x][y];
if (piece == null) {
sb.append("--");
} else {
sb.append(piece.isWhite() ? "W" : "B");
sb.append(piece.getType().getSummary());
}
if (x < width - 1) sb.append(",");
}
lines[y] = sb.toString();
}
// Last line stores the turn number
lines[height] = Integer.toString(turn);
return lines;
}
public Board(String[] array) {
//TODO
public Board(String[] fileRepresentation) {
this.height = fileRepresentation.length - 1;
this.width = fileRepresentation[0].split(",").length;
this.board = new Piece[width][height];
this.highlightedSquares = new boolean[width][height];
for (int y = 0; y < height; y++) {
String[] tokens = fileRepresentation[y].split(",");
for (int x = 0; x < width; x++) {
String token = tokens[x];
if (!token.equals("--")) {
boolean isWhite = token.charAt(0) == 'W';
char pieceChar = token.charAt(1);
PieceType type = PieceType.fromSummary(pieceChar);
board[x][y] = new Piece(isWhite, type, x, y);
} else {
board[x][y] = null;
}
}
}
this.turn = Integer.parseInt(fileRepresentation[height]);
}
private void calculateValidMoves(Piece piece) {
clearHighlights();