示例#1
0
        /// <summary>add a new routeCell to this route</summary>
        /// <param name="routeCell">route cell to add</param>
        /// <param name="grid">grid containing routeCells</param>
        public void AddCell(RouteCell routeCell, Grid grid)
        {
            if (routeCell != null && grid != null)
            {
                if (routeCell.Direction == Drone.Direction.Stay)
                {
                    this.cells.Insert(0, routeCell);

                    foreach (Packet routePacket in this.packets)
                    {
                        routePacket.Distance++;
                    }
                }
                else
                {
                    this.cells.Add(routeCell);
                    this.ReachCell = new Cell(routeCell.Row, routeCell.Column);

                    Packet packet = grid.GetPacket(routeCell);

                    // in bubbling mode, packet from previous route on startcell of this route should be Assigned so won't be add to this route packets
                    if (packet != null && packet.CurrentState == Packet.State.Pending)
                    {
                        packet.CurrentState = Packet.State.Willing;
                        packet.Distance     = this.CellDistance(routeCell);

                        this.packets.Add(packet);
                    }
                }

                this.Distance++;
                this.MaxPackets = Drone.MaxPacketsToReachDistance(this.Distance);
            }
        }
示例#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);
        }
示例#3
0
        /// <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}");
        }
示例#4
0
        /// <summary>Move drone on the grid</summary>
        /// <param name="destination">move direction, up, down, left, right</param>
        private void Move(RouteCell destination)
        {
            Write.Trace($"moving drone {this.Id} to {destination}");

            this.Position = new Cell(destination.Row, destination.Column);

            if (this.grid.SetPacketState(this.Position, Packet.State.Delivered))
            {
                Write.Trace($"{this.Position} packet delivered");
            }

            this.moves.Add((int)destination.Direction);
        }
示例#5
0
        /// <summary>remove last cell of route cells list</summary>
        public void RemoveLastCell()
        {
            RouteCell cell = this.cells.Last();

            Write.Trace($"Removing cell : {cell}");

            this.cells.Remove(cell);

            cell = this.cells.Last();

            this.ReachCell = new Cell(cell.Row, cell.Column);
            this.Distance--;
            this.MaxPackets = Drone.MaxPacketsToReachDistance(this.Distance);
        }
示例#6
0
 /// <summary>Gets route cell distance</summary>
 /// <param name="routeCell">input cell</param>
 /// <returns>cell distance as int</returns>
 public int CellDistance(RouteCell routeCell)
 {
     return(this.cells.IndexOf(routeCell) + 1);
 }
示例#7
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);
            }
        }
示例#8
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));
            }
        }