From 6b3740960c08e9665d380dde061c171470642f1c Mon Sep 17 00:00:00 2001 From: thibaud Date: Tue, 13 May 2025 15:06:09 +0200 Subject: [PATCH] =?UTF-8?q?c'est=20bon!!!=20Lib=C3=A9ration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/Board.java | 38 ++++++++++++++++++++++++++ src/backend/Move.java | 60 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/src/backend/Board.java b/src/backend/Board.java index 598791d..5d6910f 100644 --- a/src/backend/Board.java +++ b/src/backend/Board.java @@ -310,4 +310,42 @@ public class Board { return null; } + public Move getLastMove() { + if (moveHistory.isEmpty()) return null; + return moveHistory.get(moveHistory.size() - 1); + } + + /** + * Returns true if the given side’s king is under attack. + */ + public boolean isInCheck(boolean white) { + // 1) find that king + Piece king = null; + for (Piece p : pieces) { + if (p.getType() == PieceType.King && p.isWhite() == white) { + king = p; + break; + } + } + if (king == null) return false; // no king on board? + + int kx = king.getX(), ky = king.getY(); + + // 2) see if any *enemy* piece has a legal move onto the king + for (Piece p : pieces) { + if (p.isWhite() != white) { + // we use Move.getPossibleMoves(...) which now *filters* out moves that leave + // that piece’s own king in check—so we need a raw unfiltered list instead. + ArrayList attacks = Move.getAllPseudoLegalMoves(this, p); + for (int[] m : attacks) { + if (m[0] == kx && m[1] == ky) { + return true; + } + } + } + } + return false; + } + + } diff --git a/src/backend/Move.java b/src/backend/Move.java index 0461524..05b6647 100644 --- a/src/backend/Move.java +++ b/src/backend/Move.java @@ -180,4 +180,64 @@ public class Move { private static boolean isInsideBoard(Board b,int x,int y){ return x>=0 && x=0 && y getAllPseudoLegalMoves(Board board, Piece piece) { + ArrayList moves = new ArrayList<>(); + int x = piece.getX(), y = piece.getY(); + + switch (piece.getType()) { + case Pawn: + // EXACTLY the same pawn logic you already had (including *NOT* flagging hasMoved, + // but you can include en passant here if you like) + addPawnMoves(board, piece, moves); + break; + case Rook: + addLinearMoves(board, piece, moves, new int[][]{{1,0},{-1,0},{0,1},{0,-1}}); + break; + case Bishop: + addLinearMoves(board, piece, moves, new int[][]{{1,1},{1,-1},{-1,1},{-1,-1}}); + break; + case Queen: + addLinearMoves(board, piece, moves, new int[][]{ + {1,0},{-1,0},{0,1},{0,-1}, + {1,1},{1,-1},{-1,1},{-1,-1} + }); + break; + case King: + // **RAW** king moves: only one‐square steps, no castling + for (int dx = -1; dx <= 1; dx++) { + for (int dy = -1; dy <= 1; dy++) { + if (dx == 0 && dy == 0) continue; + int nx = x + dx, ny = y + dy; + if (isInsideBoard(board, nx, ny)) { + Piece p = board.getPieceAt(nx, ny); + if (p == null || p.isWhite() != piece.isWhite()) { + moves.add(new int[]{nx, ny}); + } + } + } + } + break; + case Knight: + // same as before + int[][] d = {{1,2},{2,1},{-1,2},{-2,1},{1,-2},{2,-1},{-1,-2},{-2,-1}}; + for (int[] dd : d) { + int nx = x + dd[0], ny = y + dd[1]; + if (isInsideBoard(board, nx, ny)) { + Piece p = board.getPieceAt(nx, ny); + if (p == null || p.isWhite() != piece.isWhite()) { + moves.add(new int[]{nx, ny}); + } + } + } + break; + } + + return moves; + } + }