casling check

This commit is contained in:
Romain MURPHY 2025-05-13 14:28:11 +02:00
parent bac93938e8
commit 51cb78c256
3 changed files with 91 additions and 74 deletions

View File

@ -187,11 +187,13 @@ public class AutoPlayer {
public ArrayList<Move> getAllLegalMoves(ArrayList<ArrayList<Piece>> board, boolean isWhiteTurn, Move lastMove) { public ArrayList<Move> getAllLegalMoves(ArrayList<ArrayList<Piece>> board, boolean isWhiteTurn, Move lastMove) {
ArrayList<Move> legalMoves = new ArrayList<>(); ArrayList<Move> legalMoves = new ArrayList<>();
ArrayList<ArrayList<Boolean>> moves;
for (ArrayList<Piece> row : board) { for (ArrayList<Piece> row : board) {
for (Piece p : row) { for (Piece p : row) {
if (p != null && p.isWhite() == isWhiteTurn) { if (p != null && p.isWhite() == isWhiteTurn) {
ArrayList<ArrayList<Boolean>> moves = kingCheck.getLegalMoves(p, board, lastMove); if (p.getType() == PieceType.King && p.getX() == 4 && (p.getY() == 0 || p.getY() == 7)) {
moves = kingCheck.checkCastling(p, board,lastMove, isWhiteTurn);
} else {moves = kingCheck.getLegalMoves(p, board, lastMove);}
for (int y = 0; y < 8; y++) { for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) { for (int x = 0; x < 8; x++) {
if (moves.get(y).get(x)) { if (moves.get(y).get(x)) {

View File

@ -219,7 +219,10 @@ public class Board {
this.xm = x; this.xm = x;
this.ym = y; this.ym = y;
select = true; select = true;
possibleMoves = kingCheck.getLegalMoves(board.get(y).get(x), board,lastMove); Piece pieceToMove = this.board.get(y).get(x);
if (pieceToMove.getType() == PieceType.King && x == 4 && (y == 0 || y == 7)) {
possibleMoves = kingCheck.checkCastling(board.get(y).get(x), board,lastMove, turnColor);
} else {possibleMoves = kingCheck.getLegalMoves(pieceToMove, board, lastMove);}
// possibleMoves = board.get(y).get(x).getPossibleMoves(board); // possibleMoves = board.get(y).get(x).getPossibleMoves(board);
// possibleMoves = board.get(y).get(x).getPossibleMoves(board, lastMove);//same as kingCheck // possibleMoves = board.get(y).get(x).getPossibleMoves(board, lastMove);//same as kingCheck

View File

@ -7,94 +7,106 @@ public class KingCheck {
private SoundEffect soundEffect = new SoundEffect(); private SoundEffect soundEffect = new SoundEffect();
private boolean soundShouldPlay = true; private boolean soundShouldPlay = true;
public ArrayList<ArrayList<Boolean>> getLegalMoves(Piece piece, ArrayList<ArrayList<Piece>> board, Move lastMove) { public ArrayList<ArrayList<Boolean>> getLegalMoves(Piece piece, ArrayList<ArrayList<Piece>> board, Move lastMove) {
ArrayList<ArrayList<Boolean>> rawMoves = piece.getPossibleMoves(board,lastMove); ArrayList<ArrayList<Boolean>> rawMoves = piece.getPossibleMoves(board,lastMove);
ArrayList<ArrayList<Boolean>> legalMoves = new ArrayList<>(); ArrayList<ArrayList<Boolean>> legalMoves = new ArrayList<>();
for (int y = 0; y < 8; y++) { for (int y = 0; y < 8; y++) {
ArrayList<Boolean> row = new ArrayList<>(); ArrayList<Boolean> row = new ArrayList<>();
for (int x = 0; x < 8; x++) { for (int x = 0; x < 8; x++) {
row.add(false); row.add(false);
} }
legalMoves.add(row); legalMoves.add(row);
} }
for (int y = 0; y < 8; y++) { for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) { for (int x = 0; x < 8; x++) {
if (rawMoves.get(y).get(x)) { if (rawMoves.get(y).get(x)) {
Move move = new Move(piece.getX(), piece.getY(), x, y); Move move = new Move(piece.getX(), piece.getY(), x, y);
if (isMoveSafe(board, move, piece.isWhite())) { if (isMoveSafe(board, move, piece.isWhite())) {
legalMoves.get(y).set(x, true); legalMoves.get(y).set(x, true);
} }
} }
} }
} }
return legalMoves; return legalMoves;
} }
public boolean isKingInCheck(ArrayList<ArrayList<Piece>> board, boolean isWhiteTurn) { public boolean isKingInCheck(ArrayList<ArrayList<Piece>> board, boolean isWhiteTurn) {
int kingX = -1, kingY = -1; int kingX = -1, kingY = -1;
// Find the king // Find the king
for (ArrayList<Piece> row : board) { for (ArrayList<Piece> row : board) {
for (Piece p : row) { for (Piece p : row) {
if (p != null && p.isWhite() == isWhiteTurn && p.getType() == PieceType.King) { if (p != null && p.isWhite() == isWhiteTurn && p.getType() == PieceType.King) {
kingX = p.getX(); kingX = p.getX();
kingY = p.getY(); kingY = p.getY();
break; break;
} }
} }
} }
if (kingX == -1 || kingY == -1) { if (kingX == -1 || kingY == -1) {
return true; // King not found, technically in check return true; // King not found, technically in check
} }
// Check if any enemy piece can capture the king // Check if any enemy piece can capture the king
for (ArrayList<Piece> row : board) { for (ArrayList<Piece> row : board) {
for (Piece p : row) { for (Piece p : row) {
if (p != null && p.isWhite() != isWhiteTurn) { if (p != null && p.isWhite() != isWhiteTurn) {
ArrayList<ArrayList<Boolean>> moves = p.getPossibleMoves(board); ArrayList<ArrayList<Boolean>> moves = p.getPossibleMoves(board);
if (moves.get(kingY).get(kingX)) { if (moves.get(kingY).get(kingX)) {
return true;
} }
} }
} }
} }
return false; // King is safe return false; // King is safe
} }
private boolean isMoveSafe(ArrayList<ArrayList<Piece>> board, Move move, boolean isWhiteTurn) { private boolean isMoveSafe(ArrayList<ArrayList<Piece>> board, Move move, boolean isWhiteTurn) {
ArrayList<ArrayList<Piece>> simulatedBoard = simulateMove(board, move); ArrayList<ArrayList<Piece>> simulatedBoard = simulateMove(board, move);
return !isKingInCheck(simulatedBoard, isWhiteTurn); return !isKingInCheck(simulatedBoard, isWhiteTurn);
} }
private ArrayList<ArrayList<Piece>> simulateMove(ArrayList<ArrayList<Piece>> board, Move move) { private ArrayList<ArrayList<Piece>> simulateMove(ArrayList<ArrayList<Piece>> board, Move move) {
ArrayList<ArrayList<Piece>> newBoard = deepCopyBoard(board); ArrayList<ArrayList<Piece>> newBoard = deepCopyBoard(board);
Piece piece = newBoard.get(move.fromY).get(move.fromX); Piece piece = newBoard.get(move.fromY).get(move.fromX);
newBoard.get(move.fromY).set(move.fromX, null); newBoard.get(move.fromY).set(move.fromX, null);
piece.x = move.toX; piece.x = move.toX;
piece.y = move.toY; piece.y = move.toY;
newBoard.get(move.toY).set(move.toX, piece); newBoard.get(move.toY).set(move.toX, piece);
return newBoard; return newBoard;
} }
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<>();
for (ArrayList<Piece> row : original) { for (ArrayList<Piece> row : original) {
ArrayList<Piece> newRow = new ArrayList<>(); ArrayList<Piece> newRow = new ArrayList<>();
for (Piece p : row) { for (Piece p : row) {
if (p != null) { if (p != null) {
newRow.add(PieceCreation.createPiece(p.getX(), p.getY(), p.getType(), p.isWhite())); newRow.add(PieceCreation.createPiece(p.getX(), p.getY(), p.getType(), p.isWhite()));
} else { } else {
newRow.add(null); newRow.add(null);
} }
} }
copy.add(newRow); copy.add(newRow);
} }
return copy; return copy;
} }
public ArrayList<ArrayList<Boolean>> checkCastling(Piece piece, ArrayList<ArrayList<Piece>> board, Move lastMove, boolean isWhiteTurn) {
ArrayList<ArrayList<Boolean>> possibleMoves = getLegalMoves(piece, board, lastMove);
if (isKingInCheck(board,isWhiteTurn)) {
int x = piece.getX();
int y = piece.getY();
possibleMoves.get(y).set(x+2,false);
possibleMoves.get(y).set(x-2,false);
}
return possibleMoves;
}
} }