Esempio n. 1
0
        private RouteCell NextDirection(Route route, Route.Specs routeSpecs)
        {
            List <RouteCell> availableDirections = this.AvailableDirections(route);

            // flatten collection to iterate while removing item
            IEnumerable <RouteCell> returnList = availableDirections.ToList();

            foreach (RouteCell cell in returnList)
            {
                if (cell.Row == this.ReachCell.Row && cell.Column == this.ReachCell.Column)
                {
                    // return reach cell only if available
                    Write.Trace($"reach cell is available direction : {cell}");

                    return(cell);
                }

                if (routeSpecs.HasFlag(Route.Specs.Opposite) && (cell.Direction != this.Directions.VerticalDirection && cell.Direction != this.Directions.HorizontalOpposite))
                {
                    availableDirections.Remove(cell);
                }
                else if (!routeSpecs.HasFlag(Route.Specs.Opposite) && cell.Direction != this.Directions.VerticalDirection && cell.Direction != this.Directions.HorizontalDirection)
                {
                    availableDirections.Remove(cell);
                }
            }

            return(this.FilterAvailableDirections(availableDirections, route, routeSpecs));
        }
Esempio n. 2
0
        private Route GetRoute(IEnumerable <Route.Specs> routesSpecs)
        {
            foreach (Route.Specs currentRouteSpecs in routesSpecs)
            {
                Route route = new Route(this.StartCell);

                Route.Specs routeSpecs = currentRouteSpecs;

                Write.Trace($"map {routeSpecs} route, from {this.StartCell} to {this.ReachCell}");

                while ((route.ReachCell.Row != this.ReachCell.Row || route.ReachCell.Column != this.ReachCell.Column) && route != null)
                {
                    RouteCell nextDirection = this.NextDirection(route, routeSpecs);

                    if (nextDirection != null)
                    {
                        RouteCell nextDirectionCell = nextDirection;

                        route.AddCell(nextDirectionCell, this.Grid);

                        if (route.PacketsCount == route.MaxPackets && routeSpecs.HasFlag(Route.Specs.Route))
                        {
                            routeSpecs &= ~Route.Specs.Route;
                            routeSpecs |= Route.Specs.Free;
                        }
                        else if (route.PacketsCount > route.MaxPackets)
                        {
                            Write.Trace($"break : route.PacketsCount > route.MaxPackets {route}");

                            route.Reset();
                            route = null;

                            break;
                        }
                    }
                    else
                    {
                        Write.Trace($"break : next direction = null {route}");

                        route.Reset();
                        route = null;

                        break;
                    }
                }

                if (route != null)
                {
                    return(route);
                }
            }

            return(null);
        }
Esempio n. 3
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);
            }
        }