예제 #1
0
        public void ApplyMove(ChessPieceColor color, ChessMove move, ChessMoveInfo moveInfo)
        {
            // Make the move...
            ulong fromPos = Precomputed.IndexToBitBoard[(int)move.FromIndex];
            ulong toPos   = Precomputed.IndexToBitBoard[(int)move.ToIndex];

            ulong capturePos = toPos;

            if (moveInfo != null)
            {
                moveInfo.MovedBy = color;
            }

            count50MoveRule++;

            ChessBoardColorState boardState = GetBoardState(color);

            boardState.Pieces &= ~fromPos;  // Move the piece.
            boardState.Pieces |= toPos;     // Move the piece.
            switch (move.PieceType)
            {
            case ChessPieceType.Pawn:
                count50MoveRule = 0;

                boardState.Pawns &= ~fromPos;
                switch (move.PromotionPieceType)
                {
                case ChessPieceType.Knight:
                    boardState.Knights |= toPos;
                    break;

                case ChessPieceType.Bishop:
                    boardState.Bishops |= toPos;
                    break;

                case ChessPieceType.Rook:
                    boardState.Rooks |= toPos;
                    break;

                case ChessPieceType.Queen:
                    boardState.Queens |= toPos;
                    break;

                default:
                    boardState.Pawns |= toPos;
                    break;
                }

                if (color == ChessPieceColor.White)
                {
                    if ((Precomputed.WhitePawnCaptureMoves[(int)move.FromIndex] & toPos & (enPassantTarget << 8)) != 0)
                    {
                        capturePos      = enPassantTarget;
                        enPassantTarget = 0;
                    }
                    else if ((Precomputed.WhitePawnDoubleMoves[(int)move.FromIndex] & toPos) != 0)
                    {
                        enPassantTarget = toPos;
                    }
                    else
                    {
                        enPassantTarget = 0;
                    }
                }
                else
                {
                    if ((Precomputed.BlackPawnCaptureMoves[(int)move.FromIndex] & toPos & (enPassantTarget >> 8)) != 0)
                    {
                        capturePos      = enPassantTarget;
                        enPassantTarget = 0;
                    }
                    else if ((Precomputed.BlackPawnDoubleMoves[(int)move.FromIndex] & toPos) != 0)
                    {
                        enPassantTarget = toPos;
                    }
                    else
                    {
                        enPassantTarget = 0;
                    }
                }

                break;

            case ChessPieceType.Knight:
                enPassantTarget     = 0;
                boardState.Knights &= ~fromPos;
                boardState.Knights |= toPos;
                break;

            case ChessPieceType.Bishop:
                enPassantTarget     = 0;
                boardState.Bishops &= ~fromPos;
                boardState.Bishops |= toPos;
                break;

            case ChessPieceType.Rook:
                enPassantTarget   = 0;
                boardState.Rooks &= ~fromPos;
                boardState.Rooks |= toPos;

                if ((fromPos & Precomputed.IndexToBitBoard[(int)ChessPositionIndex.A1]) != 0)
                {
                    whiteBoardState.QueensideCastlingPossible = false;
                }
                else if ((fromPos & Precomputed.IndexToBitBoard[(int)ChessPositionIndex.H1]) != 0)
                {
                    whiteBoardState.KingsideCastlingPossible = false;
                }
                else if ((fromPos & Precomputed.IndexToBitBoard[(int)ChessPositionIndex.A8]) != 0)
                {
                    blackBoardState.QueensideCastlingPossible = false;
                }
                else if ((fromPos & Precomputed.IndexToBitBoard[(int)ChessPositionIndex.H8]) != 0)
                {
                    blackBoardState.KingsideCastlingPossible = false;
                }

                break;

            case ChessPieceType.Queen:
                enPassantTarget    = 0;
                boardState.Queens &= ~fromPos;
                boardState.Queens |= toPos;
                break;

            case ChessPieceType.King:
                enPassantTarget  = 0;
                boardState.King &= ~fromPos;
                boardState.King |= toPos;

                boardState.KingsideCastlingPossible  = false;
                boardState.QueensideCastlingPossible = false;

                if (move.IsKingsideCastling)
                {
                    if (color == ChessPieceColor.White)
                    {
                        MoveRookWhenCastling(color, ChessPositionIndex.H1, ChessPositionIndex.F1, moveInfo);
                    }
                    else
                    {
                        MoveRookWhenCastling(color, ChessPositionIndex.H8, ChessPositionIndex.F8, moveInfo);
                    }
                }
                else if (move.IsQueensideCastling)
                {
                    if (color == ChessPieceColor.White)
                    {
                        MoveRookWhenCastling(color, ChessPositionIndex.A1, ChessPositionIndex.D1, moveInfo);
                    }
                    else
                    {
                        MoveRookWhenCastling(color, ChessPositionIndex.A8, ChessPositionIndex.D8, moveInfo);
                    }
                }

                break;
            }

            ChessPieceColor      invertedColor      = ChessGame.InvertColor(color);
            ChessBoardColorState invertedBoardState = GetBoardState(invertedColor);

            if ((invertedBoardState.Pieces & capturePos) != 0)   // Check if there is a capture.
            {
                invertedBoardState.Pieces  &= ~capturePos;       // Remove the piece from all black pieces.
                invertedBoardState.Pawns   &= ~capturePos;
                invertedBoardState.Knights &= ~capturePos;
                invertedBoardState.Bishops &= ~capturePos;
                invertedBoardState.Rooks   &= ~capturePos;
                invertedBoardState.Queens  &= ~capturePos;

                move.IsCapture  = true;
                count50MoveRule = 0;
                if (moveInfo != null)
                {
                    moveInfo.CapturedPiecePos = (ChessPositionIndex)Util.fastBitScanForward(capturePos);
                }

                // PP 2012-12-24: when a rook is captured, castling is no longer possible.
                if ((capturePos & Precomputed.IndexToBitBoard[(int)ChessPositionIndex.A1]) != 0)
                {
                    whiteBoardState.QueensideCastlingPossible = false;
                }
                else if ((capturePos & Precomputed.IndexToBitBoard[(int)ChessPositionIndex.H1]) != 0)
                {
                    whiteBoardState.KingsideCastlingPossible = false;
                }
                else if ((capturePos & Precomputed.IndexToBitBoard[(int)ChessPositionIndex.A8]) != 0)
                {
                    blackBoardState.QueensideCastlingPossible = false;
                }
                else if ((capturePos & Precomputed.IndexToBitBoard[(int)ChessPositionIndex.H8]) != 0)
                {
                    blackBoardState.KingsideCastlingPossible = false;
                }
            }
        }
예제 #2
0
 // Constructor
 public DisplayHintEventArgs(ChessMove move)
 {
     this.move = move;
 }