c'est bon!!! Libération
This commit is contained in:
parent
1d66174191
commit
6b3740960c
|
|
@ -310,4 +310,42 @@ public class Board {
|
||||||
return null;
|
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<int[]> attacks = Move.getAllPseudoLegalMoves(this, p);
|
||||||
|
for (int[] m : attacks) {
|
||||||
|
if (m[0] == kx && m[1] == ky) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -180,4 +180,64 @@ public class Move {
|
||||||
private static boolean isInsideBoard(Board b,int x,int y){
|
private static boolean isInsideBoard(Board b,int x,int y){
|
||||||
return x>=0 && x<b.getWidth() && y>=0 && y<b.getHeight();
|
return x>=0 && x<b.getWidth() && y>=0 && y<b.getHeight();
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Returns all pseudo-legal moves for this piece,
|
||||||
|
* i.e. exactly the same code you had in your original
|
||||||
|
* getPossibleMoves before you added the check-filter.
|
||||||
|
*/
|
||||||
|
public static ArrayList<int[]> getAllPseudoLegalMoves(Board board, Piece piece) {
|
||||||
|
ArrayList<int[]> 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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue