Example #1
0
        private SnakeAction Think(GameBoard gameBoard, Stopwatch stopwatch)
        {
            var start = gameBoard.GetMyHead();

            if (start == null)
            {
                return(new SnakeAction(false, Direction.Stop));
            }
            var startElement = gameBoard.GetElementAt(start.Value);

            if (startElement == HeadSleep || startElement == HeadDead)
            {
                return(new SnakeAction(false, Direction.Stop));
            }


            var isFury = gameBoard.AmIEvil();

            var pathFinder = new PathFinder(gameBoard, isFury);

            // find all apples/coins
            var routes = pathFinder.FindRoutes(start.Value,
                                               (elem, point, requestRoute) => ConsiderGoal(elem, point, requestRoute, isFury));

            // find weighted routes
            var max = routes.OrderByDescending(r => r.Length).First();

            Console.WriteLine($"Max: {max.Length}");
            var weighted = FindWeightedRoutes(routes, pathFinder, gameBoard, max);

            // avoid deadend
            var possible = weighted.Where(w => !w.Properties.IsDeadEnd).ToList();

            if (possible.Count == 0)
            {
                Console.WriteLine("DEADEND!!!!");
                // choose the longest: in the future it will be changed
                return(RouteToAction(weighted.OrderByDescending(r => r.Route.Length).First(), start.Value));
            }


            // make a decision
            var best = Decide(possible, max, isFury, start.Value, pathFinder);

            return(RouteToAction(best, start.Value));
        }
        private static SnakeAction DoRun(GameBoard gameBoard)
        {
            if (!gameBoard.AmIEvil())
            {
                restEvelRound = 0;
            }
            else if (restEvelRound == 0)
            {
                restEvelRound = MaxEvelRounds;
            }

            if (restEvelRound > 0)
            {
                restEvelRound--;
            }


            var head = gameBoard.GetMyHead();

            if (head == null)
            {
                prevDirection = Direction.Right;
                return(new SnakeAction(IsEnemyInTheTail(gameBoard), prevDirection));
            }

            var huntingElements = gameBoard.FindAllElements(goodElements);
            var hunting         = huntingElements
                                  .OrderBy(element => Math.Abs(element.X - head.Value.X) + Math.Abs(element.Y - head.Value.Y))
                                  .FirstOrDefault(element => isNotDeadEnd2(gameBoard, element));

            if (hunting == null)
            {
                prevDirection = GetDefaultDirection(gameBoard, head.Value);
                return(new SnakeAction(IsEnemyInTheTail(gameBoard), prevDirection));
            }

            prevDirection = GetDirectionToTarget(gameBoard, head.Value, hunting);
            return(new SnakeAction(IsEnemyInTheTail(gameBoard), prevDirection));
        }