Example #1
0
        private void Update(PathVariant path, Direction direction, int acceleration)
        {
            var newPath  = path.AddMove(direction, acceleration);
            var newPoint = newPath.Point;

            if (newPoint.IsValid(newPath.Speed))
            {
                var dict = currentVariants[newPoint];
                var key  = DirectionSpeed.FromPathVariant(newPath);
                if (!dict.TryGetValue(key, out var old) || newPath.IsBetterThan(old))
                {
                    dict[key]  = newPath;
                    hasChanges = true;
                }
            }
        }
Example #2
0
        public (Direction, int) WhereToGo(Point currentLocation, Direction currentDirection, int currentVelocity)
        {
            currentVariants = Graph.Nodes.Keys
                              .ToDictionary(p => p, p => new Dictionary <DirectionSpeed, PathVariant>());

            var pathToCurrent = new PathVariant()
            {
                Point     = currentLocation,
                Speed     = currentVelocity,
                Direction = currentDirection,
            };

            currentVariants[currentLocation] = new Dictionary <DirectionSpeed, PathVariant>()
            {
                { DirectionSpeed.FromPathVariant(pathToCurrent), pathToCurrent }
            };

            do
            {
                hasChanges = false;
                foreach (var pathToSource in currentVariants.Values.SelectMany(d => d.Values).ToList())
                {
                    for (var acceleration = -30; acceleration <= 30; acceleration += 10)
                    {
                        foreach (Direction dir in Enum.GetValues(typeof(Direction)))
                        {
                            Update(pathToSource, dir, acceleration);
                        }
                    }
                }
            }while (hasChanges);

            var bestPath  = currentVariants[Finish].Values.OrderBy(x => x.TotalTime).First();
            var lastKnown = bestPath.Moves
                            .TakeWhile(p => Graph.Nodes[p.Point] != HexType.Unknown)
                            .Last().Point;

            var result = currentVariants[lastKnown].Values.OrderBy(x => x.TotalTime).First().Moves.First();

            return(result.Direction, result.Acceleration);
        }