コード例 #1
0
ファイル: Route.cs プロジェクト: freakydinde/SoatChallenge
        /// <summary>add a wait move to route</summary>
        public void AddWait()
        {
            Write.Trace($"adding a wait cell");

            this.cells.Insert(0, RouteCell.WaitCell(this.StartCell));

            foreach (Packet packet in this.Packets)
            {
                packet.Distance++;
            }

            Write.Trace($"{this}");
        }
コード例 #2
0
        private RouteCell FilterAvailableDirections(IEnumerable <RouteCell> nextDirections, Route route, Route.Specs routeSpecs)
        {
            IEnumerable <RouteCell> returnList = null;

            // filter available directions according to route specs
            if (nextDirections.Any())
            {
                if (routeSpecs.HasFlag(Route.Specs.Route))
                {
                    // return packet cells if available, otherwise route cells
                    returnList = from i in nextDirections where i.IsPacket == true && i.WillBreakDelivery == false select i;

                    if (!returnList.Any())
                    {
                        returnList = from i in nextDirections where i.IsRoute == true && i.IsPacket == false select i;
                    }

                    if (!returnList.Any())
                    {
                        returnList = from i in nextDirections where i.IsStartRoute == true && i.IsPacket == false select i;
                    }
                }
                else if (routeSpecs.HasFlag(Route.Specs.Free))
                {
                    // return free cells if available, otherwise route cells
                    returnList = from i in nextDirections where i.IsFree == true select i;

                    if (!returnList.Any())
                    {
                        returnList = from i in nextDirections where i.IsPacket == false || i.WillBreakDelivery == false select i;
                    }
                }
                else if (routeSpecs.HasFlag(Route.Specs.All))
                {
                    // return all availables directions
                    returnList = from i in nextDirections where i.IsPacket == false || i.WillBreakDelivery == false select i;
                }

                if ((returnList == null || !returnList.Any()) && (routeSpecs.HasFlag(Route.Specs.Dodge) == true || routeSpecs.HasFlag(Route.Specs.Wait)))
                {
                    if (routeSpecs.HasFlag(Route.Specs.Dodge) == true)
                    {
                        RouteCell packetCell = (from i in nextDirections where i.WillBreakDelivery == true orderby i.Distance select i).FirstOrDefault();

                        // try to dodge cell or wait if distance gap is lower than 3 step
                        return(this.DodgeCell(packetCell, nextDirections, ref route, routeSpecs));
                    }
                    else if (routeSpecs.HasFlag(Route.Specs.Wait))
                    {
                        // insert a wait step on route start
                        return(RouteCell.WaitCell(this.StartCell));
                    }
                }
                else
                {
                    if (routeSpecs.HasFlag(Route.Specs.Alternative) && returnList.Count() > 1)
                    {
                        // Alternative
                        returnList = returnList.Skip(1);
                    }
                    else
                    {
                        // Route | Free | All following specs
                        returnList = returnList.Take(1);
                    }
                }

                return(returnList.FirstOrDefault());
            }
            else
            {
                return(null);
            }
        }
コード例 #3
0
        // Ooii
        private RouteCell DodgeCell(RouteCell packetCell, IEnumerable <RouteCell> nextDirections, ref Route route, Route.Specs routeSpecs)
        {
            Packet packet = this.Grid.GetPacket(packetCell);

            IEnumerable <RouteCell> alternativeCells = from i in this.AvailableDirections(route) where !nextDirections.Where(x => x.Row != i.Row && x.Column != i.Column).Any() select i;

            Write.Trace($"packet {packet} route.Distance + 1 {route.Distance + 1} - packet.Distance {packet.Distance}) > 2");

            if (packet.Distance - (route.Distance + 1) > 2)
            {
                route.AddCell(packetCell, this.Grid);

                RouteCell routeCell      = this.NextDirection(route, Route.Specs.All);
                RouteCell otherRouteCell = this.NextDirection(route, Route.Specs.All | Route.Specs.Alternative);

                route.RemoveLastCell();

                List <Path> paths = new List <Path>();

                if (routeCell != null)
                {
                    paths.Add(new Path(routeCell, this.Grid, alternativeCells.ElementAt(0)));
                    paths.Add(new Path(routeCell, this.Grid, alternativeCells.ElementAt(1)));
                }

                if (otherRouteCell != null)
                {
                    paths.Add(new Path(otherRouteCell, this.Grid, alternativeCells.ElementAt(0)));
                    paths.Add(new Path(otherRouteCell, this.Grid, alternativeCells.ElementAt(1)));
                }

                if (paths.Any())
                {
                    List <Route> routes = new List <Route>();

                    foreach (Path path in paths)
                    {
                        Route newRoute = path.MapRoute(new List <Route.Specs>()
                        {
                            Route.Specs.All, Route.Specs.All | Route.Specs.Alternative
                        });

                        if (newRoute != null)
                        {
                            routes.Add(newRoute);
                        }
                    }

                    Route selectedRoute = (from i in routes orderby i.Distance select i).FirstOrDefault();

                    if (selectedRoute != null)
                    {
                        route.AddRoute(selectedRoute);
                        return(this.NextDirection(route, routeSpecs));
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                // insert a wait step on route start
                return(RouteCell.WaitCell(this.StartCell));
            }
        }