en passant undo

This commit is contained in:
Romain MURPHY 2025-05-07 15:05:25 +02:00
parent ddb35ae92d
commit b54d05aeda
4 changed files with 16 additions and 9 deletions

View File

@ -36,7 +36,7 @@ public class Board {
}
this.turnNumber = 0;
this.turnColor = true;
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
boardHistory.add(new BoardHistory(board,turnNumber,turnColor,lastMove));
}
public int getWidth() {
@ -127,7 +127,7 @@ public class Board {
public void movePiece(int x, int y) {
if (select) {
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
boardHistory.add(new BoardHistory(board,turnNumber,turnColor,lastMove));
Piece pieceToMove = this.board.get(this.ym).get(this.xm);
boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2;
@ -263,7 +263,7 @@ public class Board {
board = fileToBoard.getBoard();
turnNumber = fileToBoard.getTurnNumber();
turnColor = fileToBoard.isTurnColor();
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
boardHistory.add(new BoardHistory(board,turnNumber,turnColor,lastMove));
}
// public Board toBoard(String[] array) {
// FileBoard fileToBoard = new FileBoard(array);
@ -294,6 +294,7 @@ public class Board {
board = boardHistory.get(boardHistory.size() - 1).getBoard();
this.turnNumber = boardHistory.getLast().getTurnNumber();
this.turnColor = boardHistory.getLast().isTurnColor();
this.lastMove = boardHistory.getLast().getLastMove();
boardHistory.removeLast();
// System.out.println(boardHistory); // Debug
}
@ -309,7 +310,7 @@ public class Board {
int ym = move.getFromY();
int x = move.getToX();
int y = move.getToY();
boardHistory.add(new BoardHistory(board,turnNumber,turnColor));
boardHistory.add(new BoardHistory(board,turnNumber,turnColor,lastMove));
Piece pieceToMove = this.board.get(ym).get(xm);
boolean isCastlingMove = pieceToMove.getType() == PieceType.King && Math.abs(x - xm) == 2;
lastMove = new Move(xm, ym, x, y);

View File

@ -8,10 +8,12 @@ public class BoardHistory {
public ArrayList<ArrayList<Piece>> board = new ArrayList<>();
public int turnNumber;
public boolean turnColor;
public BoardHistory(ArrayList<ArrayList<Piece>> board, int turnNumber, boolean turnColor) {
public Move lastMove;
public BoardHistory(ArrayList<ArrayList<Piece>> board, int turnNumber, boolean turnColor, Move lastMove) {
this.board = deepCopyBoard(board);
this.turnNumber = turnNumber;
this.turnColor = turnColor;
this.lastMove = lastMove;
}
private ArrayList<ArrayList<Piece>> deepCopyBoard(ArrayList<ArrayList<Piece>> original) {
ArrayList<ArrayList<Piece>> copy = new ArrayList<>();
@ -41,5 +43,8 @@ public class BoardHistory {
public String toString() {
return "BoardHistory [board=" + board + "]";
}
public Move getLastMove() {
return lastMove;
}
}

View File

@ -39,6 +39,7 @@ public class JPanelChessBoard extends JPanel {
selectedPieceType = PieceType.Pawn;
pieceSelectorMode = false;
try {
spriteSheet = ImageIO.read(new File("newPieces.png"));
} catch (IOException e) {
e.printStackTrace();

View File

@ -295,10 +295,10 @@ public class MyInterface extends JFrame {
);
switch (choice) {
case 0: return 1; // Easy
case 1: return 2; // Medium
case 2: return 3; // Hard
default: return 2; // fallback to Medium
case 0: return 2; // Easy
case 1: return 3; // Medium
case 2: return 4; // Hard
default: return 3; // fallback to Medium
}
}
}