Exemple #1
0
        public ChessBoardColorState GetBoardState(ChessPieceColor color)
        {
            ChessBoardColorState boardState = null;

            if (color == ChessPieceColor.White)
            {
                boardState = whiteBoardState;
            }
            else
            {
                boardState = blackBoardState;
            }
            return(boardState);
        }
        public ChessBoardColorState(ChessBoardColorState stateToCopy)
        {
            this.Pieces = stateToCopy.Pieces;

            this.Pawns   = stateToCopy.Pawns;
            this.Knights = stateToCopy.Knights;
            this.Bishops = stateToCopy.Bishops;
            this.Rooks   = stateToCopy.Rooks;
            this.Queens  = stateToCopy.Queens;
            this.King    = stateToCopy.King;

            this.KingsideCastlingPossible  = stateToCopy.KingsideCastlingPossible;
            this.QueensideCastlingPossible = stateToCopy.QueensideCastlingPossible;
        }
Exemple #3
0
        public bool GetPiece(ChessPositionIndex index, out ChessPieceType pieceType, out ChessPieceColor pieceColor)
        {
            bool   res = false;
            UInt64 pos = (UInt64)1 << (int)index;

            pieceType  = ChessPieceType.None;
            pieceColor = ChessPieceColor.None;

            for (ChessPieceColor color = ChessPieceColor.White; color <= ChessPieceColor.Black; color++)
            {
                ChessBoardColorState boardState = GetBoardState(color);

                if ((boardState.Pieces & pos) != 0)
                {
                    pieceColor = color;
                    if ((boardState.Pawns & pos) != 0)
                    {
                        pieceType = ChessPieceType.Pawn;
                    }
                    else if ((boardState.Knights & pos) != 0)
                    {
                        pieceType = ChessPieceType.Knight;
                    }
                    else if ((boardState.Bishops & pos) != 0)
                    {
                        pieceType = ChessPieceType.Bishop;
                    }
                    else if ((boardState.Rooks & pos) != 0)
                    {
                        pieceType = ChessPieceType.Rook;
                    }
                    else if ((boardState.Queens & pos) != 0)
                    {
                        pieceType = ChessPieceType.Queen;
                    }
                    else if ((boardState.King & pos) != 0)
                    {
                        pieceType = ChessPieceType.King;
                    }

                    res = true;
                    break;
                }
            }

            return(res);
        }
Exemple #4
0
        private void MoveRookWhenCastling(ChessPieceColor color, ChessPositionIndex fromIndex, ChessPositionIndex toIndex, ChessMoveInfo moveInfo)
        {
            ulong rookFromPos = Precomputed.IndexToBitBoard[(int)fromIndex];
            ulong rookToPos   = Precomputed.IndexToBitBoard[(int)toIndex];

            ChessBoardColorState boardState = GetBoardState(color);

            boardState.Pieces &= ~rookFromPos;
            boardState.Pieces |= rookToPos;

            boardState.Rooks &= ~rookFromPos;
            boardState.Rooks |= rookToPos;

            if (moveInfo != null)
            {
                moveInfo.SecondaryFromIndex = fromIndex;
                moveInfo.SecondaryToIndex   = toIndex;
            }
        }
Exemple #5
0
        /// <summary>
        /// Makes a copy of a chess board.
        /// </summary>
        /// <param name="boardToCopy"></param>
        public ChessBoard(ChessBoard boardToCopy, bool copyExtra)
        {
            this.Game = boardToCopy.Game;

            this.whiteBoardState = new ChessBoardColorState(boardToCopy.whiteBoardState);
            this.blackBoardState = new ChessBoardColorState(boardToCopy.blackBoardState);

            this.enPassantTarget = boardToCopy.enPassantTarget;
            this.count50MoveRule = boardToCopy.count50MoveRule;

            this.capturedPieces = new List <ChessPiece>();
            if (copyExtra)
            {
                foreach (ChessPiece piece in boardToCopy.capturedPieces)
                {
                    this.capturedPieces.Add(piece);
                }
            }
        }
Exemple #6
0
        public void SetPiece(ChessPositionIndex index, ChessPieceType pieceType, ChessPieceColor pieceColor)
        {
            UInt64 pos = (UInt64)1 << (int)index;

            if (pieceColor == ChessPieceColor.None)
            {
                SetEmpty(index);
            }
            else
            {
                ChessBoardColorState boardState = GetBoardState(pieceColor);

                boardState.Pieces |= pos;
                switch (pieceType)
                {
                case ChessPieceType.Pawn:
                    boardState.Pawns |= pos;
                    break;

                case ChessPieceType.Knight:
                    boardState.Knights |= pos;
                    break;

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

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

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

                case ChessPieceType.King:
                    boardState.King |= pos;
                    break;
                }
            }
        }
Exemple #7
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;
                }
            }
        }
Exemple #8
0
 private void InitBoardState()
 {
     this.whiteBoardState = new ChessBoardColorState();
     this.blackBoardState = new ChessBoardColorState();
 }