Has moved castling
This commit is contained in:
parent
8d5e97c5f5
commit
a028336c78
|
|
@ -145,47 +145,47 @@ public class Board implements Cloneable {
|
|||
}
|
||||
|
||||
public void userTouch(int x, int y) {
|
||||
if (selectedX == null && selectedY == null) {
|
||||
Piece pieceAtPos = getPieceAt(x, y);
|
||||
if (selectedX == null && selectedY == null) {
|
||||
Piece pieceAtPos = getPieceAt(x, y);
|
||||
|
||||
if (pieceAtPos != null && pieceAtPos.isWhite() == turnIsWhite) {
|
||||
selectedX = x;
|
||||
selectedY = y;
|
||||
if (pieceAtPos != null && pieceAtPos.isWhite() == turnIsWhite) {
|
||||
selectedX = x;
|
||||
selectedY = y;
|
||||
|
||||
highlightedPositions.clear();
|
||||
highlightedPositions.clear();
|
||||
|
||||
List<Move> legalMoves = pieceAtPos.getLegalMoves(this, y, x);
|
||||
for (Move move : legalMoves) {
|
||||
highlightedPositions.add(new int[]{move.getToCol(), move.getToRow()});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (selectedX == x && selectedY == y) {
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
highlightedPositions.clear();
|
||||
} else {
|
||||
Piece pieceToMove = getPieceAt(selectedX, selectedY);
|
||||
List<Move> legalMoves = pieceAtPos.getLegalMoves(this, y, x);
|
||||
for (Move move : legalMoves) {
|
||||
highlightedPositions.add(new int[]{move.getToCol(), move.getToRow()});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (selectedX == x && selectedY == y) {
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
highlightedPositions.clear();
|
||||
} else {
|
||||
Piece pieceToMove = getPieceAt(selectedX, selectedY);
|
||||
|
||||
if (pieceToMove != null && pieceToMove.isWhite() == turnIsWhite) {
|
||||
List<Move> legalMoves = pieceToMove.getLegalMoves(this, selectedY, selectedX);
|
||||
if (pieceToMove != null && pieceToMove.isWhite() == turnIsWhite) {
|
||||
List<Move> legalMoves = pieceToMove.getLegalMoves(this, selectedY, selectedX);
|
||||
|
||||
for (Move move : legalMoves) {
|
||||
if (move.getToCol() == x && move.getToRow() == y) {
|
||||
saveStateToHistory();
|
||||
playMove(move);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Move move : legalMoves) {
|
||||
if (move.getToCol() == x && move.getToRow() == y) {
|
||||
saveStateToHistory();
|
||||
playMove(move);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
highlightedPositions.clear();
|
||||
}
|
||||
}
|
||||
selectedX = null;
|
||||
selectedY = null;
|
||||
highlightedPositions.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Piece makeNewPiece(PieceType type, boolean isWhite, int x, int y) {
|
||||
Piece newPiece;
|
||||
switch (type) {
|
||||
|
|
@ -230,12 +230,12 @@ public class Board implements Cloneable {
|
|||
|
||||
|
||||
public Piece getPieceAt(int x, int y) {
|
||||
for (Piece piece : pieces) {
|
||||
if (piece.getX() == x && piece.getY() == y) {
|
||||
return piece;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
for (Piece piece : pieces) {
|
||||
if (piece.getX() == x && piece.getY() == y) {
|
||||
return piece;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -294,13 +294,13 @@ public class Board implements Cloneable {
|
|||
/* The following methods require more work ! */
|
||||
|
||||
public boolean isHighlighted(int x, int y) {
|
||||
// Ensure coordinates are correct
|
||||
for (int[] pos : highlightedPositions) {
|
||||
if (pos[0] == x && pos[1] == y) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
// Ensure coordinates are correct
|
||||
for (int[] pos : highlightedPositions) {
|
||||
if (pos[0] == x && pos[1] == y) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void undoLastMove() {
|
||||
|
|
@ -317,69 +317,69 @@ public class Board implements Cloneable {
|
|||
}
|
||||
|
||||
public Board(Board board) {
|
||||
this.Width = board.Width;
|
||||
this.Height = board.Height;
|
||||
this.pieces = new ArrayList<>();
|
||||
for (Piece piece : board.pieces) {
|
||||
this.pieces.add(piece.clone());
|
||||
}
|
||||
this.selectedX = board.selectedX;
|
||||
this.selectedY = board.selectedY;
|
||||
this.turnNumber = board.turnNumber;
|
||||
this.turnIsWhite = board.turnIsWhite;
|
||||
this.Width = board.Width;
|
||||
this.Height = board.Height;
|
||||
this.pieces = new ArrayList<>();
|
||||
for (Piece piece : board.pieces) {
|
||||
this.pieces.add(piece.clone());
|
||||
}
|
||||
this.selectedX = board.selectedX;
|
||||
this.selectedY = board.selectedY;
|
||||
this.turnNumber = board.turnNumber;
|
||||
this.turnIsWhite = board.turnIsWhite;
|
||||
}
|
||||
|
||||
public void playMove(Move move) {
|
||||
// Note: getPieceAt(x, y), x = col, y = row
|
||||
Piece pieceToMove = getPieceAt(move.getFromCol(), move.getFromRow());
|
||||
// Note: getPieceAt(x, y), x = col, y = row
|
||||
Piece pieceToMove = getPieceAt(move.getFromCol(), move.getFromRow());
|
||||
|
||||
if (pieceToMove == null) {
|
||||
System.err.println("Invalid move: no piece at source");
|
||||
return;
|
||||
}
|
||||
if (pieceToMove == null) {
|
||||
System.err.println("Invalid move: no piece at source");
|
||||
return;
|
||||
}
|
||||
|
||||
if (pieceToMove.isWhite() != turnIsWhite) {
|
||||
System.err.println("Invalid move: wrong turn");
|
||||
return;
|
||||
}
|
||||
if (pieceToMove.isWhite() != turnIsWhite) {
|
||||
System.err.println("Invalid move: wrong turn");
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove captured piece, if any
|
||||
Piece capturedPiece = getPieceAt(move.getToCol(), move.getToRow());
|
||||
if (capturedPiece != null) {
|
||||
pieces.remove(capturedPiece);
|
||||
}
|
||||
// Remove captured piece, if any
|
||||
Piece capturedPiece = getPieceAt(move.getToCol(), move.getToRow());
|
||||
if (capturedPiece != null) {
|
||||
pieces.remove(capturedPiece);
|
||||
}
|
||||
|
||||
pieces.remove(pieceToMove);
|
||||
pieces.remove(pieceToMove);
|
||||
|
||||
// Add moved piece at the new position
|
||||
Piece movedPiece = makeNewPiece(pieceToMove.getType(), pieceToMove.isWhite(), move.getToCol(), move.getToRow());
|
||||
pieces.add(movedPiece);
|
||||
// Add moved piece at the new position
|
||||
Piece movedPiece = makeNewPiece(pieceToMove.getType(), pieceToMove.isWhite(), move.getToCol(), move.getToRow());
|
||||
pieces.add(movedPiece);
|
||||
|
||||
// Update turn info
|
||||
turnIsWhite = !turnIsWhite;
|
||||
turnNumber++;
|
||||
|
||||
|
||||
// Play move sound if enabled
|
||||
playMoveSound();
|
||||
|
||||
// Update turn info
|
||||
turnIsWhite = !turnIsWhite;
|
||||
turnNumber++;
|
||||
|
||||
|
||||
// Play move sound if enabled
|
||||
playMoveSound();
|
||||
|
||||
|
||||
}
|
||||
|
||||
public ArrayList<Move> getAllLegalMoves(boolean isWhite) {
|
||||
ArrayList<Move> moves = new ArrayList<>();
|
||||
ArrayList<Move> moves = new ArrayList<>();
|
||||
|
||||
for (Piece piece : pieces) {
|
||||
if (piece.isWhite() == isWhite) {
|
||||
moves.addAll(piece.getLegalMoves(this, piece.getY(), piece.getX()));
|
||||
}
|
||||
}
|
||||
for (Piece piece : pieces) {
|
||||
if (piece.isWhite() == isWhite) {
|
||||
moves.addAll(piece.getLegalMoves(this, piece.getY(), piece.getX()));
|
||||
}
|
||||
}
|
||||
|
||||
return moves;
|
||||
return moves;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public ArrayList<Piece> getAllPieces() {
|
||||
ArrayList<Piece> pieces = new ArrayList<>();
|
||||
|
||||
|
|
@ -395,7 +395,7 @@ public class Board implements Cloneable {
|
|||
return pieces;
|
||||
}
|
||||
public boolean isInBounds(int x, int y) {
|
||||
return x >= 0 && x < Width && y >= 0 && y < Height;
|
||||
return x >= 0 && x < Width && y >= 0 && y < Height;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -446,58 +446,88 @@ public class Board implements Cloneable {
|
|||
moveClip.setFramePosition(0);
|
||||
moveClip.start();
|
||||
}
|
||||
|
||||
// // Castling//
|
||||
// private boolean hasMoved = false;
|
||||
//
|
||||
// public boolean hasMoved() {
|
||||
// return hasMoved;
|
||||
// }
|
||||
//
|
||||
// public void setHasMoved(boolean moved) {
|
||||
// this.hasMoved = moved;
|
||||
//
|
||||
//
|
||||
// Piece king = getKing(true);
|
||||
//
|
||||
// if (king != null) {
|
||||
// if (king.hasMoved()) {
|
||||
// System.out.println("King has moved.");
|
||||
// } else {
|
||||
// System.out.println("King didn't moved");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// public Piece getKing(boolean isWhite) {
|
||||
// for (Piece piece : pieces) {
|
||||
// if (piece.getType() == PieceType.King && piece.isWhite() == isWhite) {
|
||||
// return piece;
|
||||
// }
|
||||
// }
|
||||
// return null;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void setTurnNumber(int turnNumber) {
|
||||
this.turnNumber = turnNumber;
|
||||
}
|
||||
}
|
||||
public void setTurnIsWhite(boolean turnIsWhite) {
|
||||
this.turnIsWhite = turnIsWhite;
|
||||
}
|
||||
private Piece enPassantVulnerablePawn = null;
|
||||
|
||||
public Piece getEnPassantVulnerablePawn() {
|
||||
return enPassantVulnerablePawn;
|
||||
}
|
||||
private void updateEnPassantStateAfterMove(Move playedMove, Piece pieceThatMoved) {
|
||||
|
||||
Math.abs(playedMove.getFromRow() - playedMove.getToRow()) == 2) {
|
||||
this.enPassantVulnerablePawn = pieceThatMoved;
|
||||
this.enPassantVulnerablePawn = null;
|
||||
}
|
||||
}
|
||||
|
||||
private Piece enPassantVulnerablePawn = null;
|
||||
|
||||
public Piece getEnPassantVulnerablePawn() {
|
||||
return enPassantVulnerablePawn;
|
||||
}
|
||||
private void updateEnPassantStateAfterMove(Move playedMove, Piece pieceThatMoved) {
|
||||
|
||||
Math.abs(playedMove.getFromRow() - playedMove.getToRow()) == 2) {
|
||||
this.enPassantVulnerablePawn = pieceThatMoved;
|
||||
this.enPassantVulnerablePawn = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void highlightKingInCheck() {
|
||||
// Determine the color of the current player
|
||||
boolean isWhiteTurn = isTurnWhite();
|
||||
// Determine the color of the current player
|
||||
boolean isWhiteTurn = isTurnWhite();
|
||||
|
||||
// Find the current player's king piece
|
||||
Piece king = null;
|
||||
for (Piece piece : pieces) {
|
||||
if (piece.getType() == PieceType.King && piece.isWhite() == isWhiteTurn) {
|
||||
king = piece;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Find the current player's king piece
|
||||
Piece king = null;
|
||||
for (Piece piece : pieces) {
|
||||
if (piece.getType() == PieceType.King && piece.isWhite() == isWhiteTurn) {
|
||||
king = piece;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (king == null) {
|
||||
throw new IllegalStateException("No king found for the current player!");
|
||||
}
|
||||
if (king == null) {
|
||||
throw new IllegalStateException("No king found for the current player!");
|
||||
}
|
||||
|
||||
// Get all legal moves of the opponent's pieces
|
||||
ArrayList<Move> opponentMoves = getAllLegalMoves(!isWhiteTurn);
|
||||
// Get all legal moves of the opponent's pieces
|
||||
ArrayList<Move> opponentMoves = getAllLegalMoves(!isWhiteTurn);
|
||||
|
||||
// Check if any opponent move targets the king's position
|
||||
for (Move move : opponentMoves) {
|
||||
if (move.getToCol() == king.getX() && move.getToRow() == king.getY()) {
|
||||
// If the king is in check, highlight its position
|
||||
highlightedPositions.add(new int[]{king.getX(), king.getY()});
|
||||
return; // No need to continue after finding a check
|
||||
}
|
||||
}
|
||||
// Check if any opponent move targets the king's position
|
||||
for (Move move : opponentMoves) {
|
||||
if (move.getToCol() == king.getX() && move.getToRow() == king.getY()) {
|
||||
// If the king is in check, highlight its position
|
||||
highlightedPositions.add(new int[]{king.getX(), king.getY()});
|
||||
return; // No need to continue after finding a check
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,18 @@ public abstract class Piece {
|
|||
public abstract List<Move> getLegalMoves(Board board, int row, int col);
|
||||
|
||||
public abstract Piece clone();
|
||||
|
||||
private boolean hasMoved = false;
|
||||
|
||||
//
|
||||
public boolean hasMoved() {
|
||||
return hasMoved;
|
||||
}
|
||||
|
||||
public void setHasMoved(boolean moved) {
|
||||
this.hasMoved = moved;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue