예제 #1
0
        internal static void EvaluateBoardScore(Board board)
        {
            if ((int)board.EnPassantPosition == 0 && !board.EndGamePhase)
            {
                int?nullable1 = Zobrist.SearchEval(board.ZobristHash);
                if (nullable1.HasValue)
                {
                    int?nullable2 = nullable1;
                    int num       = 0;
                    if ((nullable2.GetValueOrDefault() == num ? (!nullable2.HasValue ? 1 : 0) : 1) != 0)
                    {
                        board.Score = nullable1.Value;
                        return;
                    }
                }
            }
            board.Score = 0;
            bool insufficientMaterial = true;

            if (board.StaleMate || (int)board.FiftyMove >= 50 || (int)board.RepeatedMove >= 3)
            {
                return;
            }
            if (board.BlackMate)
            {
                board.Score = (int)short.MaxValue;
            }
            else if (board.WhiteMate)
            {
                board.Score = -32767;
            }
            else
            {
                if (board.BlackCheck)
                {
                    board.Score += 70;
                    if (board.EndGamePhase)
                    {
                        board.Score += 10;
                    }
                }
                else if (board.WhiteCheck)
                {
                    board.Score -= 70;
                    if (board.EndGamePhase)
                    {
                        board.Score -= 10;
                    }
                }
                if (board.BlackCastled)
                {
                    board.Score -= 50;
                }
                if (board.WhiteCastled)
                {
                    board.Score += 50;
                }
                if (board.WhoseMove == ChessPieceColor.White)
                {
                    board.Score += 10;
                }
                else
                {
                    board.Score -= 10;
                }
                byte bishopCount1 = 0;
                byte bishopCount2 = 0;
                byte knightCount1 = 0;
                byte knightCount2 = 0;
                byte num          = 0;
                Evaluation.blackPawnCount = new short[8];
                Evaluation.whitePawnCount = new short[8];
                for (byte position = 0; (int)position < 64; ++position)
                {
                    Square square = board.Squares[(int)position];
                    if (square.Piece != null)
                    {
                        if (square.Piece.PieceColor == ChessPieceColor.White)
                        {
                            board.Score += Evaluation.EvaluatePieceScore(square, position, board.EndGamePhase, ref knightCount2, ref bishopCount2, ref insufficientMaterial);
                            if (square.Piece.PieceType == ChessPieceType.King && (int)position != 59 && (int)position != 60)
                            {
                                int pawnPos1 = (int)position - 8;
                                board.Score += Evaluation.CheckPawnWall(board, pawnPos1, (int)position);
                                int pawnPos2 = (int)position - 7;
                                board.Score += Evaluation.CheckPawnWall(board, pawnPos2, (int)position);
                                int pawnPos3 = (int)position - 9;
                                board.Score += Evaluation.CheckPawnWall(board, pawnPos3, (int)position);
                            }
                        }
                        else if (square.Piece.PieceColor == ChessPieceColor.Black)
                        {
                            board.Score -= Evaluation.EvaluatePieceScore(square, position, board.EndGamePhase, ref knightCount1, ref bishopCount1, ref insufficientMaterial);
                            if (square.Piece.PieceType == ChessPieceType.King && (int)position != 3 && (int)position != 4)
                            {
                                int pawnPos1 = (int)position + 8;
                                board.Score -= Evaluation.CheckPawnWall(board, pawnPos1, (int)position);
                                int pawnPos2 = (int)position + 7;
                                board.Score -= Evaluation.CheckPawnWall(board, pawnPos2, (int)position);
                                int pawnPos3 = (int)position + 9;
                                board.Score -= Evaluation.CheckPawnWall(board, pawnPos3, (int)position);
                            }
                        }
                        if (square.Piece.PieceType == ChessPieceType.Knight)
                        {
                            ++num;
                            if ((int)num > 1)
                            {
                                insufficientMaterial = false;
                            }
                        }
                        if ((int)bishopCount1 + (int)bishopCount2 > 1)
                        {
                            insufficientMaterial = false;
                        }
                        else if ((int)bishopCount1 + (int)knightCount1 > 1)
                        {
                            insufficientMaterial = false;
                        }
                        else if ((int)bishopCount2 + (int)knightCount2 > 1)
                        {
                            insufficientMaterial = false;
                        }
                    }
                }
                if (insufficientMaterial)
                {
                    board.Score                = 0;
                    board.StaleMate            = true;
                    board.InsufficientMaterial = true;
                }
                else
                {
                    if (board.EndGamePhase)
                    {
                        if (board.BlackCheck)
                        {
                            board.Score += 10;
                        }
                        else if (board.WhiteCheck)
                        {
                            board.Score -= 10;
                        }
                    }
                    else
                    {
                        if (!board.WhiteCanCastle && !board.WhiteCastled)
                        {
                            board.Score -= 50;
                        }
                        if (!board.BlackCanCastle && !board.BlackCastled)
                        {
                            board.Score += 50;
                        }
                    }
                    if ((int)Evaluation.blackPawnCount[0] >= 1 && (int)Evaluation.blackPawnCount[1] == 0)
                    {
                        board.Score += 12;
                    }
                    if ((int)Evaluation.blackPawnCount[1] >= 1 && (int)Evaluation.blackPawnCount[0] == 0 && (int)Evaluation.blackPawnCount[2] == 0)
                    {
                        board.Score += 14;
                    }
                    if ((int)Evaluation.blackPawnCount[2] >= 1 && (int)Evaluation.blackPawnCount[1] == 0 && (int)Evaluation.blackPawnCount[3] == 0)
                    {
                        board.Score += 16;
                    }
                    if ((int)Evaluation.blackPawnCount[3] >= 1 && (int)Evaluation.blackPawnCount[2] == 0 && (int)Evaluation.blackPawnCount[4] == 0)
                    {
                        board.Score += 20;
                    }
                    if ((int)Evaluation.blackPawnCount[4] >= 1 && (int)Evaluation.blackPawnCount[3] == 0 && (int)Evaluation.blackPawnCount[5] == 0)
                    {
                        board.Score += 20;
                    }
                    if ((int)Evaluation.blackPawnCount[5] >= 1 && (int)Evaluation.blackPawnCount[4] == 0 && (int)Evaluation.blackPawnCount[6] == 0)
                    {
                        board.Score += 16;
                    }
                    if ((int)Evaluation.blackPawnCount[6] >= 1 && (int)Evaluation.blackPawnCount[5] == 0 && (int)Evaluation.blackPawnCount[7] == 0)
                    {
                        board.Score += 14;
                    }
                    if ((int)Evaluation.blackPawnCount[7] >= 1 && (int)Evaluation.blackPawnCount[6] == 0)
                    {
                        board.Score += 12;
                    }
                    if ((int)Evaluation.whitePawnCount[0] >= 1 && (int)Evaluation.whitePawnCount[1] == 0)
                    {
                        board.Score -= 12;
                    }
                    if ((int)Evaluation.whitePawnCount[1] >= 1 && (int)Evaluation.whitePawnCount[0] == 0 && (int)Evaluation.whitePawnCount[2] == 0)
                    {
                        board.Score -= 14;
                    }
                    if ((int)Evaluation.whitePawnCount[2] >= 1 && (int)Evaluation.whitePawnCount[1] == 0 && (int)Evaluation.whitePawnCount[3] == 0)
                    {
                        board.Score -= 16;
                    }
                    if ((int)Evaluation.whitePawnCount[3] >= 1 && (int)Evaluation.whitePawnCount[2] == 0 && (int)Evaluation.whitePawnCount[4] == 0)
                    {
                        board.Score -= 20;
                    }
                    if ((int)Evaluation.whitePawnCount[4] >= 1 && (int)Evaluation.whitePawnCount[3] == 0 && (int)Evaluation.whitePawnCount[5] == 0)
                    {
                        board.Score -= 20;
                    }
                    if ((int)Evaluation.whitePawnCount[5] >= 1 && (int)Evaluation.whitePawnCount[4] == 0 && (int)Evaluation.whitePawnCount[6] == 0)
                    {
                        board.Score -= 16;
                    }
                    if ((int)Evaluation.whitePawnCount[6] >= 1 && (int)Evaluation.whitePawnCount[5] == 0 && (int)Evaluation.whitePawnCount[7] == 0)
                    {
                        board.Score -= 14;
                    }
                    if ((int)Evaluation.whitePawnCount[7] >= 1 && (int)Evaluation.whitePawnCount[6] == 0)
                    {
                        board.Score -= 12;
                    }
                    if ((int)Evaluation.blackPawnCount[0] >= 1 && (int)Evaluation.whitePawnCount[0] == 0)
                    {
                        board.Score -= (int)Evaluation.blackPawnCount[0];
                    }
                    if ((int)Evaluation.blackPawnCount[1] >= 1 && (int)Evaluation.whitePawnCount[1] == 0)
                    {
                        board.Score -= (int)Evaluation.blackPawnCount[1];
                    }
                    if ((int)Evaluation.blackPawnCount[2] >= 1 && (int)Evaluation.whitePawnCount[2] == 0)
                    {
                        board.Score -= (int)Evaluation.blackPawnCount[2];
                    }
                    if ((int)Evaluation.blackPawnCount[3] >= 1 && (int)Evaluation.whitePawnCount[3] == 0)
                    {
                        board.Score -= (int)Evaluation.blackPawnCount[3];
                    }
                    if ((int)Evaluation.blackPawnCount[4] >= 1 && (int)Evaluation.whitePawnCount[4] == 0)
                    {
                        board.Score -= (int)Evaluation.blackPawnCount[4];
                    }
                    if ((int)Evaluation.blackPawnCount[5] >= 1 && (int)Evaluation.whitePawnCount[5] == 0)
                    {
                        board.Score -= (int)Evaluation.blackPawnCount[5];
                    }
                    if ((int)Evaluation.blackPawnCount[6] >= 1 && (int)Evaluation.whitePawnCount[6] == 0)
                    {
                        board.Score -= (int)Evaluation.blackPawnCount[6];
                    }
                    if ((int)Evaluation.blackPawnCount[7] >= 1 && (int)Evaluation.whitePawnCount[7] == 0)
                    {
                        board.Score -= (int)Evaluation.blackPawnCount[7];
                    }
                    if ((int)Evaluation.whitePawnCount[0] >= 1 && (int)Evaluation.blackPawnCount[1] == 0)
                    {
                        board.Score += (int)Evaluation.whitePawnCount[0];
                    }
                    if ((int)Evaluation.whitePawnCount[1] >= 1 && (int)Evaluation.blackPawnCount[1] == 0)
                    {
                        board.Score += (int)Evaluation.whitePawnCount[1];
                    }
                    if ((int)Evaluation.whitePawnCount[2] >= 1 && (int)Evaluation.blackPawnCount[2] == 0)
                    {
                        board.Score += (int)Evaluation.whitePawnCount[2];
                    }
                    if ((int)Evaluation.whitePawnCount[3] >= 1 && (int)Evaluation.blackPawnCount[3] == 0)
                    {
                        board.Score += (int)Evaluation.whitePawnCount[3];
                    }
                    if ((int)Evaluation.whitePawnCount[4] >= 1 && (int)Evaluation.blackPawnCount[4] == 0)
                    {
                        board.Score += (int)Evaluation.whitePawnCount[4];
                    }
                    if ((int)Evaluation.whitePawnCount[5] >= 1 && (int)Evaluation.blackPawnCount[5] == 0)
                    {
                        board.Score += (int)Evaluation.whitePawnCount[5];
                    }
                    if ((int)Evaluation.whitePawnCount[6] >= 1 && (int)Evaluation.blackPawnCount[6] == 0)
                    {
                        board.Score += (int)Evaluation.whitePawnCount[6];
                    }
                    if ((int)Evaluation.whitePawnCount[7] >= 1 && (int)Evaluation.blackPawnCount[7] == 0)
                    {
                        board.Score += (int)Evaluation.whitePawnCount[7];
                    }
                    if ((int)board.EnPassantPosition != 0 || board.EndGamePhase || board.Score == 0)
                    {
                        return;
                    }
                    Zobrist.AddEntryEval(board.ZobristHash, board.Score);
                }
            }
        }