Exemple #1
0
    private int Evaluate(HexAIMove move, Game game)
    {
        int  score   = random.Next(0, 25); // avoid making every run the same
        var  state   = game.GetCurrentBoardState();
        Team ourTeam = state.currentMove;
        Team enemy   = ourTeam.Enemy();

        (BoardState newState, List <Promotion> newPromotions) = move.Speculate(game);

        bool weAreChecking = MoveValidator.IsChecking(ourTeam, newState, newPromotions);
        bool enemyHasMoves = MoveValidator.HasAnyValidMoves(enemy, newPromotions, newState, state);

        if (weAreChecking && !enemyHasMoves)
        {
            return(int.MaxValue);
        }

        if (weAreChecking)
        {
            score += CheckBonus;
        }

        if (!enemyHasMoves)
        {
            score -= StalematePenalty;
        }

        if (move.moveType == MoveType.Attack)
        {
            var attacker = HexachessagonEngine.GetRealPiece(move.start, state, game.promotions);
            var victim   = HexachessagonEngine.GetRealPiece(move.target, state, game.promotions);
            score += AttackBonus;
            score += GetPieceValue(victim) - GetPieceValue(attacker);
        }
        else if (move.moveType == MoveType.EnPassant)
        {
            score += EnPassantBonus;
        }
        else if (move.moveType == MoveType.Move)
        {
            var mover = state.allPiecePositions[move.start];
            if (mover.piece.IsPawn())
            {
                int ranksForward = move.target.GetNumber() - move.start.GetNumber();
                if (ourTeam == Team.Black)
                {
                    ranksForward = -ranksForward;
                }

                ranksForward *= 5;

                score *= ranksForward;
            }
        }
        else
        {
            // Defend is pretty worthless for a bloodthirsty AI
            score -= 10;
        }

        return(score);
    }