This commit is contained in:
eliot 2025-05-21 21:41:36 +02:00
parent e63064501b
commit 3ac5c282a5
4 changed files with 101 additions and 8 deletions

View File

@ -6,4 +6,3 @@ BP,BP,BP,BP,BP,BP,BP,BP
,,,,,,,
WP,WP,WP,WP,WP,WP,WP,WP
WR,WN,WB,WQ,WK,WB,WN,WR
W

View File

@ -0,0 +1,9 @@
BR,BN,BB,BQ,BK,BB,BN,BR
BP,BP,BP,BP,BP,BP,BP,BP
,,,,,,,
,,,,,,,
,,,,WP,,,
,,,,,,,
WP,WP,WP,WP,,WP,WP,WP
WR,WN,WB,WQ,WK,WB,WN,WR
B

View File

@ -182,12 +182,63 @@ public class Board {
}
public String[] toFileRep() {
//TODO
return null;
String[] fileRep = new String[lineNum + 1];
for (int y = 0; y < lineNum; y++) {
StringBuilder rowBuilder = new StringBuilder();
for (int x = 0; x < colNum; x++) {
Piece p = getPieceAt(x, y);
if (p != null) {
char colorChar = p.isWhite() ? 'W' : 'B';
char typeChar = p.getType().getSummary().charAt(0);
rowBuilder.append(colorChar).append(typeChar);
}
if (x < colNum - 1) rowBuilder.append(",");
}
fileRep[y] = rowBuilder.toString();
}
fileRep[lineNum] = isWhiteTurn ? "W" : "B";
return fileRep;
}
public Board(String[] array) {
//TODO
public Board(String[] fileRepresentation) {
if (fileRepresentation == null || fileRepresentation.length < 1) {
throw new IllegalArgumentException("Invalid file format");
}
int totalRows = fileRepresentation.length - 1;
this.lineNum = totalRows;
String[] firstRow = fileRepresentation[0].split(",", -1);
this.colNum = firstRow.length;
this.pieces = new ArrayList<>();
for (int y = 0; y < lineNum; y++) {
String[] squares = fileRepresentation[y].split(",", -1);
if (squares.length != colNum) {
throw new IllegalArgumentException("Row " + y + " has invalid column count");
}
for (int x = 0; x < colNum; x++) {
String square = squares[x].trim();
if (!square.isEmpty()) {
if (square.length() != 2) {
throw new IllegalArgumentException("Invalid piece format: " + square);
}
boolean isWhite = square.charAt(0) == 'W';
char typeChar = square.charAt(1);
PieceType type = PieceType.fromSummary(typeChar);
pieces.add(new Piece(isWhite, type, x, y));
}
}
}
String turnLine = fileRepresentation[fileRepresentation.length - 1].trim();
if (turnLine.isEmpty()) {
throw new IllegalArgumentException("Turn line is missing");
}
this.isWhiteTurn = turnLine.charAt(0) == 'W';
this.turnNumber = 0;
}
public boolean isHighlighted(int x, int y) {

View File

@ -1,6 +1,40 @@
package backend;
public class Move {
}
private Piece movedPiece;
private int fromX, fromY;
private int toX, toY;
private Piece capturedPiece;
private int capturedX, capturedY;
private int pieceMoveCountBefore;
private int boardTurnNumberBefore;
private boolean isWhiteTurnBefore;
public Move(
Piece movedPiece, int fromX, int fromY, int toX, int toY,
Piece capturedPiece, int capturedX, int capturedY,
int pieceMoveCountBefore, int boardTurnNumberBefore, boolean isWhiteTurnBefore
) {
this.movedPiece = movedPiece;
this.fromX = fromX;
this.fromY = fromY;
this.toX = toX;
this.toY = toY;
this.capturedPiece = capturedPiece;
this.capturedX = capturedX;
this.capturedY = capturedY;
this.pieceMoveCountBefore = pieceMoveCountBefore;
this.boardTurnNumberBefore = boardTurnNumberBefore;
this.isWhiteTurnBefore = isWhiteTurnBefore;
}
public Piece getMovedPiece() { return movedPiece; }
public int getFromX() { return fromX; }
public int getFromY() { return fromY; }
public Piece getCapturedPiece() { return capturedPiece; }
public int getCapturedX() { return capturedX; }
public int getCapturedY() { return capturedY; }
public int getPieceMoveCountBefore() { return pieceMoveCountBefore; }
public int getBoardTurnNumberBefore() { return boardTurnNumberBefore; }
public boolean isWhiteTurnBefore() { return isWhiteTurnBefore; }
}