fix error of en passant

This commit is contained in:
Lucie 2025-05-06 11:54:45 +02:00
parent 45144b42e7
commit e7a64f69a7
1 changed files with 16 additions and 4 deletions

View File

@ -14,6 +14,8 @@ public class Board {
private boolean enPassant; private boolean enPassant;
private int pawnX; private int pawnX;
private int pawnY; private int pawnY;
private int enPassantX;
private int enPassantY;
private boolean whiteRookLeft; private boolean whiteRookLeft;
private boolean whiteRookRight; private boolean whiteRookRight;
private boolean blackRookLeft; private boolean blackRookLeft;
@ -36,6 +38,8 @@ public class Board {
enPassant = false; enPassant = false;
pawnX = -1; pawnX = -1;
pawnY = -1; pawnY = -1;
enPassantX = -1;
enPassantY = -1;
whiteRookLeft = false; whiteRookLeft = false;
whiteRookRight = false; whiteRookRight = false;
blackRookLeft = false; blackRookLeft = false;
@ -73,6 +77,8 @@ public class Board {
enPassant = false; enPassant = false;
pawnX = -1; pawnX = -1;
pawnY = -1; pawnY = -1;
enPassantX = -1;
enPassantY = -1;
whiteRookLeft = false; whiteRookLeft = false;
whiteRookRight = false; whiteRookRight = false;
blackRookLeft = false; blackRookLeft = false;
@ -182,7 +188,6 @@ public class Board {
// a piece is already selected (so the new click is where the user wants to move the piece) // a piece is already selected (so the new click is where the user wants to move the piece)
else { else {
Move move = new Move(this.x,this.y,x,y); Move move = new Move(this.x,this.y,x,y);
MoveCalculator legalMoves = new MoveCalculator(this.board);
// if the new location is the same as before, de-selects the piece and no move happens // if the new location is the same as before, de-selects the piece and no move happens
if (this.x == x && this.y == y || board[this.y][this.x] == null) { if (this.x == x && this.y == y || board[this.y][this.x] == null) {
this.x = -1; this.x = -1;
@ -191,9 +196,9 @@ public class Board {
// the new location is highlighted, meaning it is a legal displacement // the new location is highlighted, meaning it is a legal displacement
else if (isHighlighted(x,y)) { else if (isHighlighted(x,y)) {
// handling en passant // handling en passant
if (enPassant == true && board[this.y][this.x].getType() == PieceType.Pawn && x == legalMoves.getEnPassantX() && y == legalMoves.getEnPassantY()) { if (this.enPassant == true && board[this.y][this.x].getType() == PieceType.Pawn && x == enPassantX && y == enPassantY) {
playMove(move);
board[pawnY][pawnX] = null; board[pawnY][pawnX] = null;
playMove(move);
} }
// castling to the left // castling to the left
@ -232,6 +237,11 @@ public class Board {
pawnX = x; pawnX = x;
pawnY = y; pawnY = y;
} }
else {
enPassant = false;
pawnX = -1;
pawnX = -1;
}
// check if rooks or kings are beeing moved to enable (or not) castling later // check if rooks or kings are beeing moved to enable (or not) castling later
if (board[this.y][this.x].getType() == PieceType.Rook) { if (board[this.y][this.x].getType() == PieceType.Rook) {
@ -336,6 +346,8 @@ public class Board {
ArrayList<int[]> moves = legalMoves.getMove(board[this.y][this.x].getType(), board[this.y][this.x].isWhite(), this.x, this.y, king, rookRight, rookLeft, enPassant, pawnX, pawnY); ArrayList<int[]> moves = legalMoves.getMove(board[this.y][this.x].getType(), board[this.y][this.x].isWhite(), this.x, this.y, king, rookRight, rookLeft, enPassant, pawnX, pawnY);
this.castlingLeft = legalMoves.getCastlingLeft(); this.castlingLeft = legalMoves.getCastlingLeft();
this.castlingRight = legalMoves.getCastlingRight(); this.castlingRight = legalMoves.getCastlingRight();
this.enPassantX = legalMoves.getEnPassantX();
this.enPassantY = legalMoves.getEnPassantY();
for (int i = 0; i < moves.size(); i++) { for (int i = 0; i < moves.size(); i++) {
if (x == moves.get(i)[0] && y == moves.get(i)[1]) { if (x == moves.get(i)[0] && y == moves.get(i)[1]) {
return true; return true;