Exemple #1
0
        IBoard IGame.CreateBoard()
        {
            int defaultBoardHeight = 10;
            int defaultBoardWidth  = 10;

            return(ArrayBoard.Create(defaultBoardHeight, defaultBoardWidth));
        }
Exemple #2
0
        public Board DoMove(Move move)
        {
#if TEST
            this.CheckBoard();
#endif
            //foreach (var pair in PiecesDict)
            var newBoard = new Board();
            newBoard.ArrayBoard          = ArrayBoard.ToArray();
            newBoard.BitBoard            = BitBoard.ToArray();
            newBoard.CastlingPermissions = CastlingPermissions.ToArray();
            newBoard.PieceCounts         = PieceCounts.ToArray();
            newBoard.WhiteMaterial       = WhiteMaterial;
            newBoard.BlackMaterial       = BlackMaterial;
            newBoard.Key = Key;


            var newHistory = new HistoryEntry[History.Length + 1];
            Array.Copy(History, newHistory, History.Length);
            var newEntry = new HistoryEntry(this, move);
            newHistory[newHistory.Length - 1] = newEntry;
            newBoard.History = newHistory;

            newBoard.WhiteToMove = !WhiteToMove;
            newBoard.Key        ^= ZobristKeys.ZWhiteToMove;

            if (EnPassantFile != 0)
            {
                newBoard.Key ^= ZobristKeys.ZEnPassant[EnPassantFileIndex];
            }

            if (move.NullMove)
            {
                newBoard.SyncExtraBitBoards();
#if TEST
                newBoard.CheckBoard();
#endif
                return(newBoard);
            }

            var toPosBitBoard = 1UL << move.To;

            newBoard.ArrayBoard[move.From] = ChessPiece.Empty;
            newBoard.BitBoard[move.Piece] &= ~(1UL << move.From);
            newBoard.Key ^= ZobristKeys.ZPieces[move.From, move.Piece];

            int promotedPiece;
            if (move.PawnPromoteTo.HasValue)
            {
                promotedPiece = move.PawnPromoteTo.Value;
                newBoard.PieceCounts[move.Piece]--;
                newBoard.PieceCounts[promotedPiece]++;
                if (WhiteToMove)
                {
                    newBoard.WhiteMaterial -= EvaluationService.Weights[ChessPiece.WhitePawn];
                    newBoard.WhiteMaterial += EvaluationService.Weights[promotedPiece];
                }
                else
                {
                    newBoard.BlackMaterial -= EvaluationService.Weights[ChessPiece.BlackPawn];
                    newBoard.BlackMaterial += EvaluationService.Weights[promotedPiece];
                }
            }
            else
            {
                promotedPiece = move.Piece;
            }
            newBoard.ArrayBoard[move.To]      = promotedPiece;
            newBoard.BitBoard[promotedPiece] |= toPosBitBoard;
            newBoard.Key ^= ZobristKeys.ZPieces[move.To, promotedPiece];

            if (move.TakesPiece > 0)
            {
                if (!move.EnPassant)
                {
                    newBoard.BitBoard[move.TakesPiece] &= ~toPosBitBoard;
                    newBoard.Key ^= ZobristKeys.ZPieces[move.To, move.TakesPiece];
                }
                newBoard.LastTookPieceHistoryIndex = History.Length;
                newBoard.PieceCounts[move.TakesPiece]--;
                if (WhiteToMove)
                {
                    newBoard.BlackMaterial -= EvaluationService.Weights[move.TakesPiece];
                }
                else
                {
                    newBoard.WhiteMaterial -= EvaluationService.Weights[move.TakesPiece];
                }
            }
            else
            {
                newBoard.LastTookPieceHistoryIndex = LastTookPieceHistoryIndex;
            }

            if (move.EnPassant)
            {
                int killedPawnPos;
                if (move.Piece == ChessPiece.WhitePawn)
                {
                    killedPawnPos = move.To - 8;
                }
                else
                {
                    killedPawnPos = move.To + 8;
                }

                var killedPawnBitBoard = 1UL << killedPawnPos;

                newBoard.BitBoard[move.TakesPiece] &= ~killedPawnBitBoard;
                newBoard.ArrayBoard[killedPawnPos]  = ChessPiece.Empty;
                newBoard.Key ^= ZobristKeys.ZPieces[killedPawnPos, move.TakesPiece];
            }


            if ((move.Piece == ChessPiece.WhitePawn && move.From + 16 == move.To) || (move.Piece == ChessPiece.BlackPawn && move.From - 16 == move.To))
            {
                var fileIndex = move.From % 8;
                newBoard.EnPassantFile      = Files[fileIndex];
                newBoard.EnPassantFileIndex = fileIndex;
                newBoard.Key ^= ZobristKeys.ZEnPassant[fileIndex];
            }
            else
            {
                newBoard.EnPassantFile      = 0;
                newBoard.EnPassantFileIndex = -1;
            }

            if (move.Castle)
            {
                var kingSide           = move.To % 8 > 3;
                var isWhite            = move.Piece == ChessPiece.WhiteKing;
                var castlingRookPos    = (kingSide ? 7 : 0) + (isWhite ? 0 : 56);
                var castlingRookNewPos = (move.From + move.To) / 2;
                var rookPiece          = isWhite ? ChessPiece.WhiteRook : ChessPiece.BlackRook;

                newBoard.ArrayBoard[castlingRookPos]    = ChessPiece.Empty;
                newBoard.ArrayBoard[castlingRookNewPos] = rookPiece;
                newBoard.BitBoard[rookPiece]           &= ~(1UL << castlingRookPos);
                newBoard.BitBoard[rookPiece]           |= 1UL << castlingRookNewPos;
                newBoard.Key ^= ZobristKeys.ZPieces[castlingRookPos, rookPiece];
                newBoard.Key ^= ZobristKeys.ZPieces[castlingRookNewPos, rookPiece];
            }

            if (move.Piece == ChessPiece.WhiteKing)
            {
                newBoard.CastlingPermissions[CastlePermission.WhiteQueenSide] = false;
                newBoard.CastlingPermissions[CastlePermission.WhiteKingSide]  = false;
            }
            else if (move.Piece == ChessPiece.WhiteRook)
            {
                newBoard.CastlingPermissions[CastlePermission.WhiteQueenSide] = CastlingPermissions[CastlePermission.WhiteQueenSide] && move.From != 0;
                newBoard.CastlingPermissions[CastlePermission.WhiteKingSide]  = CastlingPermissions[CastlePermission.WhiteKingSide] && move.From != 7;
            }
            else if (move.Piece == ChessPiece.BlackKing)
            {
                newBoard.CastlingPermissions[CastlePermission.BlackQueenSide] = false;
                newBoard.CastlingPermissions[CastlePermission.BlackKingSide]  = false;
            }
            else if (move.Piece == ChessPiece.BlackRook)
            {
                newBoard.CastlingPermissions[CastlePermission.BlackQueenSide] = CastlingPermissions[CastlePermission.BlackQueenSide] && move.From != 56;
                newBoard.CastlingPermissions[CastlePermission.BlackKingSide]  = CastlingPermissions[CastlePermission.BlackKingSide] && move.From != 63;
            }

            switch (move.To)
            {
            case 0:
                newBoard.CastlingPermissions[CastlePermission.WhiteQueenSide] = false;
                break;

            case 7:
                newBoard.CastlingPermissions[CastlePermission.WhiteKingSide] = false;
                break;

            case 56:
                newBoard.CastlingPermissions[CastlePermission.BlackQueenSide] = false;
                break;

            case 63:
                newBoard.CastlingPermissions[CastlePermission.BlackKingSide] = false;
                break;
            }

            for (var i = 0; i < 4; i++)
            {
                if (CastlingPermissions[i] != newBoard.CastlingPermissions[i])
                {
                    newBoard.Key ^= ZobristKeys.ZCastle[i];
                }
            }

            newBoard.SyncExtraBitBoards();

#if TEST
            newBoard.CheckBoard();
#endif

            return(newBoard);
        }
 private void SetupEmptyBoard()
 {
     _boardHeight = 10;
     _boardWidth  = 10;
     _board       = ArrayBoard.Create(_boardHeight, _boardWidth);
 }