From 67013f6594ac3a476a5bb9472690ea24237adb26 Mon Sep 17 00:00:00 2001 From: Lucie Date: Wed, 14 May 2025 15:49:00 +0200 Subject: [PATCH] fix bug for the undolastmove --- src/backend/Board.java | 160 ++++++++++++++++++++++++----------------- 1 file changed, 93 insertions(+), 67 deletions(-) diff --git a/src/backend/Board.java b/src/backend/Board.java index 31181ca..8b0cf1e 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -31,25 +31,7 @@ public class Board { this.colNum = colNum; this.lineNum = lineNum; this.board = new Piece[lineNum][colNum]; - this.x = -1; - this.y = -1; - turns = 0; - whiteTurn = true; - - enPassant = false; - pawnX = -1; - pawnY = -1; - enPassantX = -1; - enPassantY = -1; - whiteRookLeft = false; - whiteRookRight = false; - blackRookLeft = false; - blackRookRight = false; - whiteKing = false; - blackKing = false; - castlingRight = false; - castlingLeft = false; - states = new ArrayList<>(); + initialise(); } // GETTERS @@ -69,10 +51,29 @@ public class Board { return whiteTurn; } + public ArrayList getPieces() { + ArrayList pieces = new ArrayList<>(); + for (int i = 0; i < colNum; i++) { + for (int j = 0; j < lineNum; j++) { + if (board[j][i] != null) { + pieces.add(board[j][i]); + } + } + } + return pieces; + } + + public Piece getPieceAt(int x, int y) { + if (inBoard(x,y)) { + return board[y][x]; + } + return null; + } + // INITIALISE THE BOARD public void initialise() { - x = -1; - y = -1; + this.x = -1; + this.y = -1; turns = 0; whiteTurn = true; @@ -148,40 +149,29 @@ public class Board { } str += "\n"; } - String turn = "B"; + String end = "B"; if (isTurnWhite()) { - turn = "W"; + end = "W"; } - str += turn; + Boolean[] booleans = {enPassant, whiteRookLeft, whiteRookRight, blackRookLeft, blackRookRight, whiteKing, blackKing}; + for (int k = 0; k < 7; k++) { + end += Boolean.toString(booleans[k]).charAt(0); + } + int[] integers = {pawnX, pawnY, enPassantX, enPassantY}; + for (int n = 0; n < 4; n++) { + end += Integer.toString(integers[n]).charAt(0); + } + str += end; return str; } - // LIST THE PIECES OF THE BOARD - public ArrayList getPieces() { - ArrayList pieces = new ArrayList<>(); - for (int i = 0; i < colNum; i++) { - for (int j = 0; j < lineNum; j++) { - if (board[j][i] != null) { - pieces.add(board[j][i]); - } - } - } - return pieces; - } - public boolean inBoard(int x, int y) { if (x > -1 && x < colNum && y > -1 && y < lineNum) { return true; } return false; } - - public Piece getPieceAt(int x, int y) { - if (inBoard(x,y)) { - return board[y][x]; - } - return null; - } + // PROCESS THE USER CLICK public void userTouch(int x, int y) { @@ -203,16 +193,18 @@ public class Board { this.y = -1; } // the new location is highlighted, meaning it is a legal displacement - else if (isHighlighted(x,y)) { + else if (isHighlighted(x,y)) { + Piece piece = board[this.y][this.x]; // handling en passant - if (this.enPassant == true && board[this.y][this.x].getType() == PieceType.Pawn && x == enPassantX && y == enPassantY) { + if (this.enPassant == true && piece.getType() == PieceType.Pawn && x == enPassantX && y == enPassantY) { board[pawnY][pawnX] = null; playMove(move); + this.states.add(toString()); } // castling to the left else if (this.castlingLeft && x == this.x-2) { - if (board[this.y][this.x].isWhite()) { + if (piece.isWhite()) { // rook displacement (white left) playMove(new Move(0,7,x+1,y)); } @@ -222,11 +214,12 @@ public class Board { } // king displacement playMove(move); + this.states.add(toString()); } // castling to the right else if (this.castlingRight && x == this.x+2) { - if (board[this.y][this.x].isWhite()) { + if (piece.isWhite()) { // rook displacement (white right) playMove(new Move(7,7,x-1,y)); } @@ -236,50 +229,54 @@ public class Board { } // king displacement playMove(move); + this.states.add(toString()); } // basic legal move else { // verify if the pawn goes two steps forward (to know if en passant will be possible for the opponent) - if (board[this.y][this.x].getType() == PieceType.Pawn && (y == this.y+2 || y == this.y-2)) { + if (piece.getType() == PieceType.Pawn && (y == this.y+2 || y == this.y-2)) { + // sets that en passant can be done next turn enPassant = true; + // coordinates of the pawn that can be taken pawnX = x; pawnY = y; } else { enPassant = false; pawnX = -1; - pawnX = -1; + pawnY = -1; } // check if rooks or kings are beeing moved to enable (or not) castling later - if (board[this.y][this.x].getType() == PieceType.Rook) { + playMove(move); + this.states.add(toString()); + if (piece.getType() == PieceType.Rook) { if (this.x == 0) { - if (board[this.y][this.x].isWhite() && whiteRookLeft == false) { + if (piece.isWhite()) { whiteRookLeft = true; } - else if (board[this.y][this.x].isWhite() == false && blackRookLeft == false) { + else { blackRookLeft = true; } } else if (this.x == 7) { - if (board[this.y][this.x].isWhite() && whiteRookRight == false) { + if (piece.isWhite()) { whiteRookRight = true; } - else if (board[this.y][this.x].isWhite() == false && blackRookRight == false) { + else { blackRookRight = true; } } } - else if (board[this.y][this.x].getType() == PieceType.King) { - if (board[this.y][this.x].isWhite()) { + else if (piece.getType() == PieceType.King) { + if (piece.isWhite()) { whiteKing = true; } - else if (board[this.y][this.x].isWhite() == false) { + else { blackKing = true; } } - playMove(move); } turns += 1; this.x = -1; @@ -328,12 +325,10 @@ public class Board { } } } - if (array[lineNum] == "B") { + whiteTurn = true; + if (array[lineNum].charAt(0) == 'B') { whiteTurn = false; } - else { - whiteTurn = true; - } } @@ -382,12 +377,44 @@ public class Board { Board newBoard = new Board(undo); this.board = newBoard.board; - if (undo[lineNum] == "B" || turns == 0) { - whiteTurn = true; + if (turns != 0) { + whiteTurn = !whiteTurn; } else { - whiteTurn = false; + whiteTurn = true; } + + Boolean[] booleans = new Boolean[7]; + for (int k = 1; k < 8; k++) { + if (undo[lineNum].charAt(k) == 'f') { + booleans[k-1] = false; + } + else { + booleans[k-1] = true; + } + } + + int[] integers = new int[4]; + for (int n = 8; n < 12; n++) { + if (undo[lineNum].charAt(n) == '-') { + integers[n-8] = -1; + } + else { + integers[n-8] = Character.getNumericValue(undo[lineNum].charAt(n)); + } + } + + this.enPassant = booleans[0]; + this.whiteRookLeft = booleans[1]; + this.whiteRookRight = booleans[2]; + this.blackRookLeft = booleans[3]; + this.blackRookRight = booleans[4]; + this.whiteKing = booleans[5]; + this.blackKing = booleans[6]; + this.pawnX = integers[0]; + this.pawnY = integers[1]; + this.enPassantX = integers[2]; + this.enPassantY = integers[3]; } } @@ -409,7 +436,6 @@ public class Board { board[move.getFromY()][move.getFromX()].setY(move.getToY()); board[move.getToY()][move.getToX()] = board[move.getFromY()][move.getFromX()]; board[move.getFromY()][move.getFromX()] = null; - this.states.add(toString()); } }