Changed the saving and loading system to match the FEN notation type

This commit is contained in:
Marleen PETERSON 2025-05-20 00:04:33 +02:00
parent 79e7aaaa49
commit b223586906
1 changed files with 12 additions and 12 deletions

View File

@ -360,17 +360,19 @@ public class Board {
}
// PART3
public String[] toFileRep() { //board state to file representation
String[] result = new String[lineNum + 1];
public String[] toFileRep() {
String[] result = new String[lineNum + 1]; //+1 for turn indicator
for (int y = 0; y < lineNum; y++) {
StringBuilder sb = new StringBuilder();
for (int x = 0; x < colNum; x++) {
if (x > 0) sb.append(",");
Piece piece = board[y][x];
if (piece != null) {
sb.append(piece.isWhite() ? "W" : "B");
sb.append(piece.getType().getSummary());
char symbol = piece.getType().getSummary().charAt(0);
sb.append(piece.isWhite() ? Character.toUpperCase(symbol) : Character.toLowerCase(symbol));
} else {
sb.append(".");
}
sb.append(" ");
}
result[y] = sb.toString();
}
@ -384,13 +386,12 @@ public class Board {
this.board = new Piece[lineNum][colNum];
for (int y = 0; y < lineNum; y++) {
String[] tokens = array[y].split(",", -1);
String[] pieces = array[y].split(" ");
for (int x = 0; x < colNum; x++) {
String token = tokens[x].trim();
if (token.length() == 2) {
boolean isWhite = token.charAt(0) == 'W';
char typeChar = token.charAt(1);
PieceType type = PieceType.fromSummary(typeChar);
char pieceChar = pieces[x].charAt(0);
if (pieceChar != '.') {
boolean isWhite = Character.isUpperCase(pieceChar);
PieceType type = PieceType.fromSummary(Character.toUpperCase(pieceChar));
board[y][x] = new Piece(x, y, type, isWhite);
}
}
@ -398,7 +399,6 @@ public class Board {
this.isWhiteTurn = array[lineNum].equalsIgnoreCase("W");
}
public void undoLastMove() {
if (moveHistory.isEmpty()) return;