Compare commits

...

3 Commits

Author SHA1 Message Date
HP 8c5a288035 findKingposition but unused for now 2025-05-19 17:51:27 +02:00
HP 595ca18c90 nope sorry wrong change 2025-05-17 20:36:34 +02:00
HP 3509653392 promotion with the undo ! 2025-05-17 20:36:12 +02:00
2 changed files with 46 additions and 28 deletions

View File

@ -2,6 +2,7 @@ package backend;
import java.util.ArrayList;
public class Board {
private int width;
@ -204,7 +205,7 @@ public class Board {
selectedX = null;
selectedY = null;
highlightedSquares.clear();
// Check for en passant capture BEFORE updating enPassantTarget!
if (selectedPiece.getType() == PieceType.Pawn &&
enPassantTarget != null &&
@ -440,6 +441,32 @@ private boolean isEnemy(int x, int y, boolean isWhite) {
}
public boolean isKingInCheck(boolean isWhite) {
// find king position
int kingX = 1, kingY = 1;
for (Piece p : pieces) {
if (p.isWhite() == isWhite && p.getType() == PieceType.King) {
kingX = p.getX();
kingY = p.getY();
System.out.println("kingfound");
break;
}
}
// Generate and test enemies moves
for (Piece enemy : pieces) {
if (enemy.isWhite() != isWhite) {
ArrayList<int[]> moves = computeLegalMoves(enemy);
for (int[] move : moves) {
if (move[0] == kingX && move[1] == kingY) {
return true; // king in check
}
}
}
}
System.out.println("kingfound");
return false; // king is not in check
}
public ArrayList<int[]> computeLegalMoves(Piece piece) {
ArrayList<int[]> moves = new ArrayList<>();
int x = piece.getX();
@ -460,14 +487,6 @@ public ArrayList<int[]> computeLegalMoves(Piece piece) {
if (y == startRow && isEmpty(x, twoStepsY)) {
moves.add(new int[]{x, twoStepsY});
}
if (nextY == 7 ){
setPiece(false , PieceType.Queen, x,nextY);
pieces.remove(piece);
}
if (nextY==0 ){
setPiece(true , PieceType.Queen, x,nextY);
pieces.remove(piece);
}
}
// Diagonal capture
@ -477,24 +496,28 @@ public ArrayList<int[]> computeLegalMoves(Piece piece) {
if (isEnemy(x + 1, nextY, piece.isWhite())) {
moves.add(new int[]{x + 1, nextY});
}
if (y==7){
setPiece(false , PieceType.Queen, x,y);
pieces.remove(piece);
}
if (y==0 ){
setPiece(true , PieceType.Queen, x,y);
pieces.remove(piece);
}
// En passant
if (enPassantTarget != null) {
int targetX = enPassantTarget[0];
int targetY = enPassantTarget[1];
if (nextY == targetY && Math.abs(x - targetX) == 1) {
moves.add(new int[]{targetX, targetY});
if(!piece.isWhite() && y==4 && targetY==5 && Math.abs(x-targetX)==1) {
moves.add(new int[] {targetX, targetY});
}
}
// Promotion check (you shouldn't promote here only on move execution)
// Do NOT change piece type inside this method
}
if (type == PieceType.Rook) {
// Directions : haut, bas, gauche, droite
int[][] directions = {{1,0},{-1,0},{0,1},{0,-1}};
@ -584,14 +607,8 @@ public ArrayList<int[]> computeLegalMoves(Piece piece) {
}
}
}
}
return moves;
}
}
}

View File

@ -24,6 +24,7 @@ public class Move {
public boolean isWhite() { return isWhite; }
public PieceType getType() { return type; }
public Piece getCapturedPiece() { return capturedPiece; }
}
}