Compare commits
2 Commits
48eab6ae73
...
6e899924bd
| Author | SHA1 | Date |
|---|---|---|
|
|
6e899924bd | |
|
|
27fcb3fda1 |
|
|
@ -226,6 +226,26 @@ public class Board {
|
|||
}
|
||||
}
|
||||
}
|
||||
if (selectedPiece.getType() == PieceType.King && Math.abs(x - selectedX) == 2) {
|
||||
int row = selectedPiece.isWhite() ? 7 : 0;
|
||||
if (x > selectedX) {
|
||||
// King-side castling
|
||||
Piece rook = getPieceAt(7, row);
|
||||
if (rook != null) {
|
||||
rook.setX(5);
|
||||
rook.setY(row);
|
||||
rook.setMoved(true);
|
||||
}
|
||||
} else {
|
||||
// Queen-side castling
|
||||
Piece rook = getPieceAt(0, row);
|
||||
if (rook != null) {
|
||||
rook.setX(3);
|
||||
rook.setY(row);
|
||||
rook.setMoved(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSelected(int x, int y) {
|
||||
|
|
@ -482,48 +502,6 @@ public class Board {
|
|||
|
||||
// 6. If the king is in check and no move avoids it → checkmate
|
||||
return kingInCheck && !hasEscape;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
private void enPassant(Board board, List<Move> moves) {
|
||||
int x = this.x;
|
||||
int y = this.y;
|
||||
|
||||
if (isWhite() == true && this.y == 3) {
|
||||
if(isEnPassant(board, new Piece(x - 1, y)))
|
||||
moves.add(new Move(this, new Piece(x - 1, y - 1),
|
||||
board.getPieceAt(new Piece(x - 1, y))));
|
||||
if(canCaptureEnPassant(board, new Piece(x + 1, y)))
|
||||
moves.add(new Move(this, new Piece(x + 1, y - 1),
|
||||
board.getPieceAt(new Piece(x + 1, y))));
|
||||
}
|
||||
if (isWhite() == false && this.y == 4) {
|
||||
if(isEnPassant(board, new Piece(x - 1, y)))
|
||||
moves.add(new Move(this, new Piece(x - 1, y + 1),
|
||||
board.getPieceAt(new Piece(x - 1, y))));
|
||||
if(canCaptureEnPassant(board, new Piece(x + 1, y)))
|
||||
moves.add(new Move(this, new Piece(x + 1, y + 1),
|
||||
board.getPieceAt(new Piece(x + 1, y))));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
* Checks if the pawn can capture another pawn by en passant
|
||||
* @param pt location of the other pawn
|
||||
* @return true if can be captured
|
||||
|
||||
|
||||
private boolean isEnPassant(Board board, Point pt) {
|
||||
Piece temp = board.getPieceAt(pt);
|
||||
if(temp != null) {
|
||||
if (temp instanceof Pawn && temp.getColor() != this.color)
|
||||
if (((Pawn)temp).enPassantOk)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -198,6 +198,17 @@ public class MoveConditions {
|
|||
|
||||
if (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
|
||||
Piece target = board.getPieceAt(newX, newY);
|
||||
|
||||
if (!piece.hasMoved() && !board.isKingInCheck(isWhite)) {
|
||||
|
||||
if (canUseCastling(isWhite, true)) {
|
||||
moves.add(new int[]{x + 2, y});
|
||||
}
|
||||
|
||||
if (canUseCastling(isWhite, false)) {
|
||||
moves.add(new int[]{x - 2, y});
|
||||
}
|
||||
}
|
||||
|
||||
if (target == null || target.isWhite() != isWhite) {
|
||||
Board simBoard = new Board(board);
|
||||
|
|
@ -229,10 +240,39 @@ public class MoveConditions {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return moves;
|
||||
}
|
||||
private boolean canUseCastling(boolean isWhite, boolean kingSide) {
|
||||
int row = isWhite ? 7 : 0;
|
||||
int kingX = 4;
|
||||
|
||||
int rookX = kingSide ? 7 : 0;
|
||||
int step = kingSide ? 1 : -1;
|
||||
|
||||
Piece rook = board.getPieceAt(rookX, row);
|
||||
Piece king = board.getPieceAt(kingX, row);
|
||||
|
||||
if (rook == null || rook.getType() != PieceType.Rook || rook.hasMoved()) return false;
|
||||
if (king == null || king.getType() != PieceType.King || king.hasMoved()) return false;
|
||||
|
||||
// Squares between king and rook must be empty
|
||||
int start = Math.min(kingX, rookX) + 1;
|
||||
int end = Math.max(kingX, rookX) - 1;
|
||||
for (int i = start; i <= end; i++) {
|
||||
if (board.getPieceAt(i, row) != null) return false;
|
||||
}
|
||||
|
||||
// King cannot pass through or land in check
|
||||
for (int i = 1; i <= 2; i++) {
|
||||
Board temp = new Board(board);
|
||||
Piece simKing = temp.getPieceAt(kingX, row);
|
||||
simKing.setX(kingX + (step * i));
|
||||
if (temp.isKingInCheck(isWhite)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -47,8 +47,19 @@ public class Piece {
|
|||
this.y = y;
|
||||
}
|
||||
|
||||
private boolean hasMoved = false;
|
||||
|
||||
public boolean hasMoved() {
|
||||
return hasMoved;
|
||||
}
|
||||
|
||||
public void setMoved(boolean moved) {
|
||||
this.hasMoved = moved;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return (isWhite ? "White " : "Black ") + type + " at (" + x + "," + y + ")";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue