Ejemplo n.º 1
0
        private static bool VerifyBoard(BoardState board)
        {
            if (board.Material[Color.White] != board.CalculateMaterial(Color.White) ||
                board.Material[Color.Black] != board.CalculateMaterial(Color.Black))
            {
                return(false);
            }

            if (board.Hash != ZobristHashing.CalculateHash(board))
            {
                return(false);
            }

            if (board.PawnHash != ZobristHashing.CalculatePawnHash(board))
            {
                return(false);
            }

            if (board.Position[Color.White][GamePhase.Opening] != board.CalculatePosition(Color.White, GamePhase.Opening) ||
                board.Position[Color.White][GamePhase.Ending] != board.CalculatePosition(Color.White, GamePhase.Ending) ||
                board.Position[Color.Black][GamePhase.Opening] != board.CalculatePosition(Color.Black, GamePhase.Opening) ||
                board.Position[Color.Black][GamePhase.Ending] != board.CalculatePosition(Color.Black, GamePhase.Ending))
            {
                return(false);
            }

            var pieceTable = new int[64];

            board.CalculatePieceTable(pieceTable);

            for (var fieldIndex = 0; fieldIndex < 64; fieldIndex++)
            {
                if (board.PieceTable[fieldIndex] != pieceTable[fieldIndex])
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        public static BoardState Parse(string fen)
        {
            var split          = fen.Split(' ');
            var boardState     = split[0];
            var colorState     = split[1];
            var castlingState  = split[2];
            var enPassantState = split[3];
            var halfmoveClock  = split.Length > 4 ? int.Parse(split[4]) : 0;
            var movesCount     = split.Length > 5 ? int.Parse(split[5]) : 0;

            var result       = new BoardState();
            var currentColor = ParseCurrentColor(colorState);

            ParseBoardState(boardState, result);
            ParseCastlingState(castlingState, result);
            ParseEnPassantState(enPassantState, result);

            result.Material[Color.White] = result.CalculateMaterial(Color.White);
            result.Material[Color.Black] = result.CalculateMaterial(Color.Black);

            result.Position[Color.White][GamePhase.Opening] = result.CalculatePosition(Color.White, GamePhase.Opening);
            result.Position[Color.White][GamePhase.Ending]  = result.CalculatePosition(Color.White, GamePhase.Ending);
            result.Position[Color.Black][GamePhase.Opening] = result.CalculatePosition(Color.Black, GamePhase.Opening);
            result.Position[Color.Black][GamePhase.Ending]  = result.CalculatePosition(Color.Black, GamePhase.Ending);

            result.CalculatePieceTable(result.PieceTable);
            result.MaterialAtOpening = result.CalculateMaterialAtOpening();

            result.MovesCount             = movesCount;
            result.IrreversibleMovesCount = halfmoveClock;
            result.ColorToMove            = currentColor;
            result.Hash     = ZobristHashing.CalculateHash(result);
            result.PawnHash = ZobristHashing.CalculatePawnHash(result);

            return(result);
        }