From 3ac5c282a529ff5437a84ee834ee066c90552cf2 Mon Sep 17 00:00:00 2001 From: eliot Date: Wed, 21 May 2025 21:41:36 +0200 Subject: [PATCH] TEST --- default.board | 1 - save.board | 9 +++++++ src/backend/Board.java | 59 +++++++++++++++++++++++++++++++++++++++--- src/backend/Move.java | 40 +++++++++++++++++++++++++--- 4 files changed, 101 insertions(+), 8 deletions(-) diff --git a/default.board b/default.board index a50fc7c..fb604df 100644 --- a/default.board +++ b/default.board @@ -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 diff --git a/save.board b/save.board index e69de29..928949a 100644 --- a/save.board +++ b/save.board @@ -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 diff --git a/src/backend/Board.java b/src/backend/Board.java index 939ed1b..808a512 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -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) { diff --git a/src/backend/Move.java b/src/backend/Move.java index ec0528d..e8ac071 100644 --- a/src/backend/Move.java +++ b/src/backend/Move.java @@ -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; } +} \ No newline at end of file