Exemplo n.º 1
0
        /// <summary>
        ///     Determine if the king can move out of check by capturing a piece or simply moving.
        /// </summary>
        private bool CanKingMoveOrCaptureOutOfCheck(IKing king, IBoardState gameBoardState)
        {
            var canKingMoveOutOfCheck    = false;
            var canKingCaptureOutOfCheck = false;

            // 1.) Can the king move or capture out of check?

            foreach (ChessPosition position in king.ThreatenSet)
            {
                IMove    move    = ModelLocator.CreateMove(king.Location, position);
                ICapture capture = ModelLocator.CreateCapture(king.Location, position);

                canKingMoveOutOfCheck    |= IsMoveLegal(king, move, gameBoardState);
                canKingCaptureOutOfCheck |= IsCaptureLegal(king, capture, gameBoardState);
            }

            return(canKingMoveOutOfCheck || canKingCaptureOutOfCheck);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Does the active player control a piece that can capture the piece threatening the king?
        /// </summary>
        private bool CanThreateningPieceBeCaptured(IPiece threateningPiece)
        {
            foreach (IPiece activePlayerPiece in ActivePlayerPieces)
            {
                activePlayerPiece.GenerateCaptures(GameBoard.State, ActivePlayerBoardState);
                bool canCaptureAt = activePlayerPiece.CanCaptureAt(threateningPiece.Location);

                if (!canCaptureAt)
                {
                    continue;
                }

                ICapture capture = ModelLocator.CreateCapture(activePlayerPiece.Location, threateningPiece.Location);

                bool isCaptureLegal = IsCaptureLegal(activePlayerPiece, capture, GameBoard.State);

                if (isCaptureLegal)
                {
                    return(true);
                }
            }

            return(false);
        }