move function updated
This commit is contained in:
parent
2d3b816551
commit
ebcda9c623
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
|
|
@ -1,5 +1,384 @@
|
|||
package backend;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Move {
|
||||
|
||||
}
|
||||
private int fromX;
|
||||
private int fromY;
|
||||
private int toX;
|
||||
private int toY;
|
||||
private Piece movingPiece;
|
||||
private Piece capturedPiece;
|
||||
private Board board;
|
||||
|
||||
public Move(int fromX, int fromY, int toX, int toY, Board board) {
|
||||
this.fromX = fromX;
|
||||
this.fromY = fromY;
|
||||
this.toX = toX;
|
||||
this.toY = toY;
|
||||
this.board = board;
|
||||
this.movingPiece = board.getPieceAt(fromX, fromY);
|
||||
this.capturedPiece = board.getPieceAt(toX, toY);
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
// Check if the moving piece exists
|
||||
if (movingPiece == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if the move is in the set of valid moves for this piece
|
||||
Set<Position> validMoves = getValidDestinations(movingPiece, board);
|
||||
return validMoves.contains(new Position(toX, toY));
|
||||
}
|
||||
|
||||
public void execute() {
|
||||
// Remove any piece at the destination (normal capture)
|
||||
if (capturedPiece != null) {
|
||||
board.removePiece(toX, toY);
|
||||
}
|
||||
|
||||
// Move the piece (remove from original position, add to new position)
|
||||
board.removePiece(fromX, fromY);
|
||||
board.setPiece(movingPiece.isWhite(), movingPiece.getType(), toX, toY);
|
||||
|
||||
// Advance turn
|
||||
board.advanceTurn();
|
||||
}
|
||||
|
||||
public boolean putsOwnKingInCheck() {
|
||||
// Create a temporary board to simulate the move
|
||||
Board tempBoard = new Board(board);
|
||||
|
||||
// Find the piece in the temp board
|
||||
Piece tempPiece = tempBoard.getPieceAt(fromX, fromY);
|
||||
if (tempPiece == null) return true; // Safety check
|
||||
|
||||
// Remove any piece at destination (normal capture)
|
||||
tempBoard.removePiece(toX, toY);
|
||||
|
||||
// Move the piece
|
||||
tempBoard.removePiece(fromX, fromY);
|
||||
tempBoard.setPiece(tempPiece.isWhite(), tempPiece.getType(), toX, toY);
|
||||
|
||||
// Check if king is in check after move
|
||||
return isKingInCheck(tempBoard, tempPiece.isWhite());
|
||||
}
|
||||
|
||||
public boolean putsOpponentInCheck() {
|
||||
// Create a temporary board to simulate the move
|
||||
Board tempBoard = new Board(board);
|
||||
|
||||
// Find the piece in the temp board
|
||||
Piece tempPiece = tempBoard.getPieceAt(fromX, fromY);
|
||||
if (tempPiece == null) return false; // Safety check
|
||||
|
||||
// Remove any piece at destination (normal capture)
|
||||
tempBoard.removePiece(toX, toY);
|
||||
|
||||
// Move the piece
|
||||
tempBoard.removePiece(fromX, fromY);
|
||||
tempBoard.setPiece(tempPiece.isWhite(), tempPiece.getType(), toX, toY);
|
||||
|
||||
// Check if opponent's king is in check after move
|
||||
return isKingInCheck(tempBoard, !tempPiece.isWhite());
|
||||
}
|
||||
|
||||
public boolean putsOpponentInCheckmate() {
|
||||
// First, check if the move puts the opponent in check
|
||||
if (!putsOpponentInCheck()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create a temporary board to simulate the move
|
||||
Board tempBoard = new Board(board);
|
||||
|
||||
// Find the piece in the temp board
|
||||
Piece tempPiece = tempBoard.getPieceAt(fromX, fromY);
|
||||
if (tempPiece == null) return false; // Safety check
|
||||
|
||||
// Remove any piece at destination (normal capture)
|
||||
tempBoard.removePiece(toX, toY);
|
||||
|
||||
// Move the piece
|
||||
tempBoard.removePiece(fromX, fromY);
|
||||
tempBoard.setPiece(tempPiece.isWhite(), tempPiece.getType(), toX, toY);
|
||||
|
||||
boolean opponentColor = !tempPiece.isWhite();
|
||||
|
||||
// Try all possible moves for all opponent pieces
|
||||
for (Piece p : tempBoard.getPieces()) {
|
||||
if (p.isWhite() != opponentColor) continue; // Skip pieces of the wrong color
|
||||
|
||||
Set<Position> possibleMoves = getValidDestinations(p, tempBoard);
|
||||
for (Position pos : possibleMoves) {
|
||||
// Create a temporary move
|
||||
Move testMove = new Move(p.getX(), p.getY(), pos.x, pos.y, tempBoard);
|
||||
|
||||
// Check if this move would get the opponent out of check
|
||||
if (!testMove.putsOwnKingInCheck()) {
|
||||
return false; // Opponent has at least one valid move
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we get here, opponent has no valid moves
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Set<Position> getValidDestinations(Piece piece, Board board) {
|
||||
Set<Position> moves = new HashSet<>();
|
||||
|
||||
switch (piece.getType()) {
|
||||
case Pawn:
|
||||
addPawnMoves(moves, piece, board);
|
||||
break;
|
||||
case Rook:
|
||||
addRookMoves(moves, piece, board);
|
||||
break;
|
||||
case Knight:
|
||||
addKnightMoves(moves, piece, board);
|
||||
break;
|
||||
case Bishop:
|
||||
addBishopMoves(moves, piece, board);
|
||||
break;
|
||||
case Queen:
|
||||
addRookMoves(moves, piece, board);
|
||||
addBishopMoves(moves, piece, board);
|
||||
break;
|
||||
case King:
|
||||
addKingMoves(moves, piece, board);
|
||||
break;
|
||||
}
|
||||
|
||||
return moves;
|
||||
}
|
||||
|
||||
public static Set<Position> getValidMoves(Piece piece, Board board) {
|
||||
// Get all possible moves without considering check
|
||||
Set<Position> candidateMoves = getValidDestinations(piece, board);
|
||||
|
||||
// Filter out moves that would leave king in check
|
||||
Set<Position> legalMoves = new HashSet<>();
|
||||
for (Position pos : candidateMoves) {
|
||||
Move move = new Move(piece.getX(), piece.getY(), pos.x, pos.y, board);
|
||||
if (!move.putsOwnKingInCheck()) {
|
||||
legalMoves.add(pos);
|
||||
}
|
||||
}
|
||||
|
||||
return legalMoves;
|
||||
}
|
||||
|
||||
public static boolean isKingInCheck(Board board, boolean isWhiteKing) {
|
||||
// Find the king's position
|
||||
Piece king = null;
|
||||
for (Piece p : board.getPieces()) {
|
||||
if (p.getType() == PieceType.King && p.isWhite() == isWhiteKing) {
|
||||
king = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (king == null) return false;
|
||||
|
||||
// Check if any opponent piece can attack the king
|
||||
for (Piece p : board.getPieces()) {
|
||||
if (p.isWhite() == isWhiteKing) continue; // Skip pieces of same color
|
||||
|
||||
// Get raw moves without check validation
|
||||
Set<Position> attackMoves = getValidDestinations(p, board);
|
||||
|
||||
// If any piece can move to king's position, king is in check
|
||||
if (attackMoves.contains(new Position(king.getX(), king.getY()))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void addPawnMoves(Set<Position> validMoves, Piece piece, Board board) {
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
|
||||
int direction = isWhite ? -1 : 1; // White pawns move up, black pawns move down
|
||||
|
||||
// Forward movement
|
||||
int newY = y + direction;
|
||||
if (newY >= 0 && newY < board.getHeight()) {
|
||||
// Single square forward
|
||||
if (board.getPieceAt(x, newY) == null) {
|
||||
validMoves.add(new Position(x, newY));
|
||||
|
||||
// Initial double move
|
||||
int startRow = isWhite ? 6 : 1;
|
||||
if (y == startRow) {
|
||||
int doubleY = newY + direction;
|
||||
if (doubleY >= 0 && doubleY < board.getHeight() && board.getPieceAt(x, doubleY) == null) {
|
||||
validMoves.add(new Position(x, doubleY));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Diagonal captures
|
||||
for (int dx = -1; dx <= 1; dx += 2) {
|
||||
int newX = x + dx;
|
||||
if (newX >= 0 && newX < board.getWidth()) {
|
||||
Piece targetPiece = board.getPieceAt(newX, newY);
|
||||
if (targetPiece != null && targetPiece.isWhite() != isWhite) {
|
||||
validMoves.add(new Position(newX, newY));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void addRookMoves(Set<Position> validMoves, Piece piece, Board board) {
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
|
||||
// Directions: horizontal and vertical
|
||||
int[][] directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
|
||||
|
||||
for (int[] dir : directions) {
|
||||
int dx = dir[0];
|
||||
int dy = dir[1];
|
||||
|
||||
int newX = x + dx;
|
||||
int newY = y + dy;
|
||||
|
||||
while (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
|
||||
Piece targetPiece = board.getPieceAt(newX, newY);
|
||||
|
||||
if (targetPiece == null) {
|
||||
// Empty square
|
||||
validMoves.add(new Position(newX, newY));
|
||||
} else {
|
||||
// Occupied square
|
||||
if (targetPiece.isWhite() != isWhite) {
|
||||
// Capture opponent's piece
|
||||
validMoves.add(new Position(newX, newY));
|
||||
}
|
||||
break; // Cannot move beyond a piece
|
||||
}
|
||||
|
||||
newX += dx;
|
||||
newY += dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void addKnightMoves(Set<Position> validMoves, Piece piece, Board board) {
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
|
||||
// All possible knight moves: L-shapes
|
||||
int[][] knightMoves = {
|
||||
{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
|
||||
{1, -2}, {1, 2}, {2, -1}, {2, 1}
|
||||
};
|
||||
|
||||
for (int[] move : knightMoves) {
|
||||
int newX = x + move[0];
|
||||
int newY = y + move[1];
|
||||
|
||||
if (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
|
||||
Piece targetPiece = board.getPieceAt(newX, newY);
|
||||
|
||||
if (targetPiece == null || targetPiece.isWhite() != isWhite) {
|
||||
// Empty square or can capture opponent's piece
|
||||
validMoves.add(new Position(newX, newY));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void addBishopMoves(Set<Position> validMoves, Piece piece, Board board) {
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
|
||||
// Directions: diagonals
|
||||
int[][] directions = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
|
||||
|
||||
for (int[] dir : directions) {
|
||||
int dx = dir[0];
|
||||
int dy = dir[1];
|
||||
|
||||
int newX = x + dx;
|
||||
int newY = y + dy;
|
||||
|
||||
while (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
|
||||
Piece targetPiece = board.getPieceAt(newX, newY);
|
||||
|
||||
if (targetPiece == null) {
|
||||
// Empty square
|
||||
validMoves.add(new Position(newX, newY));
|
||||
} else {
|
||||
// Occupied square
|
||||
if (targetPiece.isWhite() != isWhite) {
|
||||
// Capture opponent's piece
|
||||
validMoves.add(new Position(newX, newY));
|
||||
}
|
||||
break; // Cannot move beyond a piece
|
||||
}
|
||||
|
||||
newX += dx;
|
||||
newY += dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void addKingMoves(Set<Position> validMoves, Piece piece, Board board) {
|
||||
int x = piece.getX();
|
||||
int y = piece.getY();
|
||||
boolean isWhite = piece.isWhite();
|
||||
|
||||
// All adjacent squares
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
if (dx == 0 && dy == 0) continue; // Skip the current position
|
||||
|
||||
int newX = x + dx;
|
||||
int newY = y + dy;
|
||||
|
||||
if (newX >= 0 && newX < board.getWidth() && newY >= 0 && newY < board.getHeight()) {
|
||||
Piece targetPiece = board.getPieceAt(newX, newY);
|
||||
|
||||
if (targetPiece == null || targetPiece.isWhite() != isWhite) {
|
||||
// Empty square or can capture opponent's piece
|
||||
validMoves.add(new Position(newX, newY));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class Position {
|
||||
int x;
|
||||
int y;
|
||||
|
||||
public Position(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) return true;
|
||||
if (obj == null || getClass() != obj.getClass()) return false;
|
||||
Position position = (Position) obj;
|
||||
return x == position.x && y == position.y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 31 * x + y;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue