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

@ -117,6 +117,8 @@ public class Board {
return pieces; return pieces;
} }
public void userTouch(int x, int y) { public void userTouch(int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= height) return; if (x < 0 || x >= width || y < 0 || y >= height) return;
@ -166,15 +168,55 @@ public class Board {
/* saving-loading feature :*/ /* saving-loading feature :*/
public String[] toFileRep() { public String[] toFileRep() {
//TODO String[] lines = new String[height + 1]; // one line per row + 1 for turn
return null;
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();
} }
public Board(String[] array) { // Last line stores the turn number
//TODO lines[height] = Integer.toString(turn);
return lines;
} }
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) { private void calculateValidMoves(Piece piece) {
clearHighlights(); clearHighlights();
if (piece == null) return; if (piece == null) return;