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

View File

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

View File

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

View File

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