public void AddQueue_Should_Handle_Null_Source() { var list = new int[] { 99, 3 }; var queue = new Queue <int>(); queue.Enqueue(list[0]); queue.Enqueue(list[1]); var newQueue = QueueHelper.AddQueue(null, queue); newQueue.Should().BeNull(); }
public void AddQueue_Should_Handle_Null_QueueToAdd() { var list = new int[] { 1, 9, 5 }; var queue = new Queue <int>(); queue.Enqueue(list[0]); queue.Enqueue(list[1]); queue.Enqueue(list[2]); var newQueue = QueueHelper.AddQueue(queue, null); newQueue.Should().StartWith(list); newQueue.Should().EndWith(list); }
public void AddQueue_Should_Return_Correctly() { var list1 = new int[] { 1, 9, 5 }; var queue1 = new Queue <int>(); queue1.Enqueue(list1[0]); queue1.Enqueue(list1[1]); queue1.Enqueue(list1[2]); var list2 = new int[] { 99, 3 }; var queue2 = new Queue <int>(); queue2.Enqueue(list2[0]); queue2.Enqueue(list2[1]); var newQueue = QueueHelper.AddQueue(queue1, queue2); newQueue.Should().StartWith(list1); newQueue.Should().EndWith(list2); }
private NavigationDetails Scout() { // Get the next possible directions that can be taken. // Get the path that has been taken so far. // Put them in a queue. var routes = GetNextRoutes(); Trace($"Found {routes.Length} possible route(s)."); var steps = TraceStepsAndReset(); Trace($"Steps so far: {steps}."); Trace("Adding possible routes in the queue."); var queue = GetRouteQueue(_current, steps, routes); while (queue.Count > 0) { // Get an item from the queue and start navigation from there. var item = queue.Dequeue(); var next = item.NextStep; var previousSteps = item.StepsTaken; Trace($"Next route in queue: {previousSteps} + {next} from ({item.Start.X},{item.Start.Y})."); Trace($"Items in queue: {queue.Count()}."); MarkTrack(Map.EMPTY); _current = item.Start; Trace($"Relocating to route origin."); Move(next); var nextRoutes = GetNextRoutes(); Trace($"Found {routes.Length} possible route(s)."); while (nextRoutes?.Any() == true) { // Continue the navigation using the first Direction in the array (highest preference). // Add the remaining Directions to the front of the queue. var alternateRoutes = nextRoutes.Skip(1).ToArray(); var pathSoFar = $"{previousSteps}{TraceSteps()}"; Trace($"Adding {alternateRoutes.Count()} possible route(s) in the queue."); var alternateQueue = GetRouteQueue(_current, pathSoFar, alternateRoutes); queue = QueueHelper.AddQueue(alternateQueue, queue); Trace($"Items in queue: {queue.Count()}."); Move(nextRoutes.First()); nextRoutes = GetNextRoutes(); Trace($"Found {routes.Length} possible route(s)."); } // At this point, it means the path being traversed either reached the destination Coordinates or a dead end. // Exit the function and return the appropriate response if the destination Coordinates has been reached. // Otherwise, the next item in the queue will be processed and the navigation will be traversed from there. var stepsTaken = TraceStepsAndReset(); Trace($"Steps so far: {steps}."); if (nextRoutes == null) { Trace($"Arrived at destination!"); var pathTaken = $"{previousSteps}{stepsTaken}"; return(new NavigationDetails { Arrived = true, PathTaken = pathTaken }); } } // At this point, every item in the queue has been processed without reaching the destination Coordinates. // Because of this, return the appropriate response. Trace($"Arrived at a deadend."); return(new NavigationDetails { Arrived = false }); }