diff --git a/src/Board.java b/src/Board.java index 51833ab..2bf3dba 100644 --- a/src/Board.java +++ b/src/Board.java @@ -128,7 +128,15 @@ public class Board { public void move(Move move) { if (move == null) return; - move(move.from, move.to); + // White pieces disappear if new move isn't created, don't know why + Move copiedMove = new Move(new BoardCoordinate(move.from.x, move.from.y), new BoardCoordinate(move.to.x, move.to.y), get(move.to.x, move.to.y)); + copiedMove.submove = move.submove; + moveHistory.add(copiedMove); + // moveHistory.add(move); + set(move.to, get(move.from)); + set(move.from, null); + move(move.submove); + if (move.submove != null) moveHistory.pop(); } public void undoMove() { @@ -178,7 +186,7 @@ public class Board { ArrayList legalMoves = piece.getLegalMoves(dragging, this); for (Move legalMove : legalMoves) { if (newCoordinate.equals(legalMove.to)) { - move(dragging, newCoordinate); + move(legalMove); setLastMovedPieceAsMoved(); checkForCheckmate(); if (!isGameOver) { diff --git a/src/ChessAI.java b/src/ChessAI.java index 8fe8ce5..bf20af5 100644 --- a/src/ChessAI.java +++ b/src/ChessAI.java @@ -1,5 +1,5 @@ public class ChessAI { - private static final int MAX_DEPTH = 4; + private static final int MAX_DEPTH = 3; public static Move findBestMove(Board board) { int bestScore = Integer.MIN_VALUE; diff --git a/src/King.java b/src/King.java index 95ed8f2..36e62b4 100644 --- a/src/King.java +++ b/src/King.java @@ -20,7 +20,41 @@ public class King extends Piece { possibleMoves.add(new Move(position, new BoardCoordinate(position.x + 1, position.y))); possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y + 1))); possibleMoves.add(new Move(position, new BoardCoordinate(position.x, position.y - 1))); - Piece rightRook = board.get(position.x + 3, position.y); + + { + BoardCoordinate rightRookPosition = new BoardCoordinate(position.x + 3, position.y); + Piece rightRook = board.get(rightRookPosition); + if (!moved && + rightRook instanceof Rook && + !rightRook.moved && + board.get(position.x + 1, position.y) == null && + board.get(position.x + 2, position.y) == null && + !isInCheck(board) + ) { + // TODO: Does not take into account squares in castling path being threatened + Move rightCastle = new Move(position, new BoardCoordinate(position.x + 2, position.y)); + rightCastle.submove = new Move(rightRookPosition, new BoardCoordinate(rightRookPosition.x - 2, rightRookPosition.y)); + possibleMoves.add(rightCastle); + } + } + + { + BoardCoordinate leftRookPosition = new BoardCoordinate(position.x - 4, position.y); + Piece leftRook = board.get(leftRookPosition); + if (!moved && + leftRook instanceof Rook && + !leftRook.moved && + board.get(position.x - 1, position.y) == null && + board.get(position.x - 2, position.y) == null && + board.get(position.x - 3, position.y) == null && + !isInCheck(board) + ) { + // TODO: Does not take into account squares in castling path being threatened + Move leftCastle = new Move(position, new BoardCoordinate(position.x - 2, position.y)); + leftCastle.submove = new Move(leftRookPosition, new BoardCoordinate(leftRookPosition.x + 3, leftRookPosition.y)); + possibleMoves.add(leftCastle); + } + } return possibleMoves; } diff --git a/src/Piece.java b/src/Piece.java index 9309ce8..c770688 100644 --- a/src/Piece.java +++ b/src/Piece.java @@ -50,12 +50,18 @@ public abstract class Piece { } boolean isInCheck(Move move, Board board) { - boolean isInCheck = false; board.move(move); + boolean isInCheck = isInCheck(board); + if (move != null) board.undoMove(); + return isInCheck; + } + + boolean isInCheck(Board board) { + boolean isInCheck = false; outer: for (int y = 0; y < Board.BOARD_SIZE; y++) { for (int x = 0; x < Board.BOARD_SIZE; x++) { Piece piece = board.get(x, y); - if (piece == null || piece.black == black) continue; + if (piece == null || piece.black == black || piece instanceof King) continue; ArrayList legalMoves = piece.getLegalMoves(new BoardCoordinate(x, y), board, false); for (Move legalMove : legalMoves) { Piece pieceAtMove = board.get(legalMove.to); @@ -66,7 +72,6 @@ public abstract class Piece { } } } - board.undoMove(); return isInCheck; }