public Position EvaluateBestPosition()
    {
        var random = new Random();

        var randomPositions = Enumerable.Range(1, 10)
                              .Select(i => Map.GetRandomWaterPosition());

        var bestPosition     = Position.None;
        var bestFilledRegion = new HashSet <Position>();

        foreach (var position in randomPositions)
        {
            if (bestFilledRegion.Contains(position) == true)
            {
                //Do nothings
            }
            else
            {
                var noVisitedPositions = new HashSet <Position>();
                var fillEngine         = new FloodFillEngine(noVisitedPositions);
                var filledRegion       = fillEngine.Run(position);

                if (filledRegion.Count > bestFilledRegion.Count)
                {
                    bestPosition     = position;
                    bestFilledRegion = filledRegion;
                }
            }
        }

        return(bestPosition);
    }
Exemplo n.º 2
0
    private Action SelectMoveAction()
    {
        var fromPosition      = MySubmarine.Position;
        var possibleMoves     = GetPossibleDirectionsForMove(fromPosition);
        var possibleMoveCount = possibleMoves.Count;

        if (possibleMoveCount == 0)
        {
            return(MySubmarine.SurfaceMySubmarine());
        }

        if (possibleMoveCount == 1)
        {
            var possibleMove = possibleMoves.Single();
            return(MySubmarine.MoveMySubmarine(possibleMove, SelectPowerToCharge()));
        }

        var visitedPositions = MySubmarine.VisitedPositions;
        var rankedMoves      = new Dictionary <int, List <Tuple <Position, Direction> > >();

        foreach (var possibleMove in possibleMoves)
        {
            var floodFillEngine = new FloodFillEngine(visitedPositions);
            var filledRegion    = floodFillEngine.Run(possibleMove.Item1);
            var score           = filledRegion.Count;

            if (rankedMoves.ContainsKey(score) == false)
            {
                rankedMoves[score] = new List <Tuple <Position, Direction> >();
            }

            rankedMoves[score].Add(new Tuple <Position, Direction>(possibleMove.Item1, possibleMove.Item2));
        }

        var bestMoves = rankedMoves.OrderByDescending(kvp => kvp.Key).First().Value;
        var bestMove  = GetBestMoveByStealth(bestMoves);

        return(MySubmarine.MoveMySubmarine(bestMove, SelectPowerToCharge()));
    }