public static MoveSummary CanKingMove(ChessGame game, Color kingColor, Board board, Square from, Square to) { var isSameColor = board[from].Color == board[to].Color; var isCapturing = board[to].IsNone() ? false : !isSameColor; var canDefaultMove = CanDefaultMove(from, to, isSameColor); if (canDefaultMove) { return(MoveSummaryBuilder.KingSummary(isCapturing, true, false, false, to)); } return (CanCastle(game, kingColor, board, from, to)); }
private static MoveSummary CanCastle(ChessGame game, Color kingColor, Board board, Square from, Square to) { var isHorizontalLine = MoveProperty.IsHorizontalLine(from, to); if (isHorizontalLine) { var isAnyPieceBetween = Checker.IsAnyPieceBetween(board, from, to); if (!isAnyPieceBetween) { var isKingWhite = kingColor.IsWhite(); var hasKingMoved = isKingWhite ? game.HasWhiteKingMoved : game.HasBlackKingMoved; if (hasKingMoved) { return (MoveSummaryBuilder.DefaultMoveSummary(false, false, Square.NoneSquare)); } var kingStartSquare = GetKingStartSquare(isKingWhite); if (kingStartSquare == from) { var isQueensideDirection = IsQueensideDirection(isKingWhite, to); var isKingsideDirection = IsKingsideDirection(isKingWhite, to); Square rookStartSquare; bool hasRookMoved; if (isQueensideDirection) { rookStartSquare = GetQueensideRookStartSquare(isKingWhite); hasRookMoved = isKingWhite ? game.HasWhiteQueensideRookMoved : game.HasBlackQueensideRookMoved; } else if (isKingsideDirection) { rookStartSquare = GetKingsideRookStartSquare(isKingWhite); hasRookMoved = isKingWhite ? game.HasWhiteKingsideRookMoved : game.HasBlackKingsideRookMoved; } else { return(MoveSummaryBuilder.DefaultMoveSummary(false, false, Square.NoneSquare)); } var piece = board[rookStartSquare]; if (piece.IsNone()) { return (MoveSummaryBuilder.DefaultMoveSummary(false, false, Square.NoneSquare)); } var isValid = !hasRookMoved && piece.Color == kingColor && piece.Type == PieceType.Rook; if (isValid) { return(MoveSummaryBuilder. KingSummary(false, true, isQueensideDirection, isKingsideDirection, Square.NoneSquare)); } } } } return(MoveSummaryBuilder.DefaultMoveSummary(false, false, Square.NoneSquare)); }