This commit is contained in:
Admin 2025-05-23 08:42:55 +02:00
parent 8d7a88552b
commit da7f9739fc
2 changed files with 25 additions and 17 deletions

View File

@ -133,23 +133,23 @@ public class Move {
switch (piece.getType()) {
case Pawn:
addPawnMoves(moves, piece, board);
break;
return moves;
case Rook:
addRookMoves(moves, piece, board);
break;
return moves;
case Knight:
addKnightMoves(moves, piece, board);
break;
return moves;
case Bishop:
addBishopMoves(moves, piece, board);
break;
return moves;
case Queen:
addRookMoves(moves, piece, board);
addBishopMoves(moves, piece, board);
break;
return moves;
case King:
addKingMoves(moves, piece, board);
break;
return moves;
}
return moves;
@ -177,12 +177,14 @@ public class Move {
for (Piece p : board.getPieces()) {
if (p.getType() == PieceType.King && p.isWhite() == isWhiteKing) {
king = p;
break;
return checkIfKingUnderAttack(board, isWhiteKing, king);
}
}
if (king == null) return false;
return false;
}
private boolean checkIfKingUnderAttack(Board board, boolean isWhiteKing, Piece king) {
// Check if any opponent piece can attack the king
for (Piece p : board.getPieces()) {
if (p.isWhite() == isWhiteKing) continue; // Skip pieces of same color
@ -250,8 +252,9 @@ public class Move {
int newX = x + dx;
int newY = y + dy;
boolean continueInDirection = true;
while (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
while (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight() && continueInDirection) {
Piece targetPiece = board.getPieceAt(newX, newY);
if (targetPiece == null) {
@ -263,11 +266,13 @@ public class Move {
// Capture opponent's piece
validMoves.add(new Position(newX, newY));
}
break; // Cannot move beyond a piece
continueInDirection = false; // Cannot move beyond a piece
}
newX += dx;
newY += dy;
if (continueInDirection) {
newX += dx;
newY += dy;
}
}
}
}
@ -312,8 +317,9 @@ public class Move {
int newX = x + dx;
int newY = y + dy;
boolean continueInDirection = true;
while (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
while (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight() && continueInDirection) {
Piece targetPiece = board.getPieceAt(newX, newY);
if (targetPiece == null) {
@ -325,11 +331,13 @@ public class Move {
// Capture opponent's piece
validMoves.add(new Position(newX, newY));
}
break; // Cannot move beyond a piece
continueInDirection = false; // Cannot move beyond a piece
}
newX += dx;
newY += dy;
if (continueInDirection) {
newX += dx;
newY += dy;
}
}
}
}