Ejemplo n.º 1
0
        // Algorithm A* for finding path from initial state to final state of a board
        public State AStarSolve()
        {
            priorityQueue.Clear();
            priorityQueue.Add(this.initialState);
            while (priorityQueue.Count > 0)
            {
                this.currentState = priorityQueue.RemoveFirst();

                if (this.currentState.IsFinalState())
                {
                    return(this.currentState);
                }

                if (!visited.Contains(this.currentState))
                {
                    visited.Add(this.currentState);
                }

                State movementUp    = Move.Up(this.currentState);
                State movementDown  = Move.Down(this.currentState);
                State movementLeft  = Move.Left(this.currentState);
                State movementRight = Move.Right(this.currentState);
                this.AddToQueue(movementUp);
                this.AddToQueue(movementDown);
                this.AddToQueue(movementLeft);
                this.AddToQueue(movementRight);
            }
            return(null);
        }
Ejemplo n.º 2
0
 public void RemoveAllSystems()
 {
     foreach (Systeme s in systemList)
     {
         RemoveSystem(s);
     }
     systemList.Clear();
 }
 public void Clear()
 {
     byInsertion.Clear();
     byOrder.Clear();
     byOrderReversed.Clear();
 }
Ejemplo n.º 4
0
 public void Clear()
 {
     byInsertion.Clear();
     byAscending.Clear();
     byDescending.Clear();
 }
        private void ComputeNextActions()
        {
            float goalDestinationX = player.Location.X + 983;
            HashSet<int> visited = new HashSet<int>();

            orderedSet = new OrderedBag<PlayerNode>((a, b) => a.Destination((int)goalDestinationX).CompareTo(b.Destination((int)goalDestinationX)));

            PlayerNode initialNode = new PlayerNode(null, sceneTime, 0, PlayerAction.None, player.Clone(), 0);
            orderedSet.Add(initialNode);
            visited.Add((int)initialNode.PlayerElement.Location.X << 10 + (int)initialNode.PlayerElement.Location.Y);

            PlayerNode bestNode = initialNode;
            bool foundBest = false;

            while (orderedSet.Count > 0 && !foundBest)
            {
                PlayerNode current = orderedSet.RemoveFirst();

                if (current.PlayerElement.IsOnGround() && bestNode.Destination((int)goalDestinationX) > current.Destination((int)goalDestinationX))
                {
                    bestNode = current;
                }

                foreach (PlayerAction action in possibleActions)
                {
                    if (action == PlayerAction.Jump && !current.PlayerElement.IsOnGround())
                    {
                        continue;
                    }

                    PlayerElement newPlayer = current.PlayerElement.Clone();
                    newPlayer.IsPlayerAGoat = true;
                    switch (action)
                    {
                        case PlayerAction.None:
                            newPlayer.Stop();
                            break;
                        case PlayerAction.MoveRight:
                            newPlayer.MoveForward();
                            break;
                        case PlayerAction.MoveLeft:
                            newPlayer.MoveBackward();
                            break;
                        case PlayerAction.Jump:
                            newPlayer.Jump();
                            break;
                    }

                    GameTime newTime = IncrementTime(current.Time);
                    newPlayer.Update(newTime);
                    Parallel.ForEach(timeDependentElements, element => element.Update(newTime));
                    HandleInteraction(newPlayer);
                    if (newPlayer.IsDead)
                    {
                        continue;
                    }

                    int hash = ((int)(newPlayer.Location.X * 10) << 7) + (int)newPlayer.Location.Y;
                    if (!visited.Add(hash))
                    {
                        continue;
                    }

                    PlayerNode newNode = new PlayerNode(current, newTime, current.Moves + 1, action, newPlayer, current.Jumps + (action == PlayerAction.Jump ? 1 : 0));
                    if (newPlayer.Location.X > goalDestinationX && newPlayer.IsOnGround() || newPlayer.HasWon)
                    {
                        bestNode = newNode;
                        foundBest = true;
                        break;
                    }
                    orderedSet.Add(newNode);
                }
            }

            orderedSet.Clear();
            if (bestNode == initialNode)
            {
                pendingActions.Enqueue(PlayerAction.None);
            }
            else
            {
                GetActionsFromNode(bestNode);
            }
        }
Ejemplo n.º 6
0
        //private static void InsertOpenNode(PathNode node)
        //{
        //	for (var i = 0; i < openList.Count; i++)
        //	{
        //		if (node.F < openList[i].F)
        //		{
        //			openList.Insert(i, node);
        //			return;
        //		}
        //	}

        //	openList.Add(node);
        //}

        private static PathNode BuildPath(MapWalkData walkData, Position start, Position target, int maxLength, int range)
        {
            if (nodeCache == null)
            {
                BuildCache();
            }

            cachePos         = MaxCacheSize;
            Pathfinder.range = range;

            //openList.Clear();
            openBag.Clear();
            openListPos.Clear();
            closedListPos.Clear();
            nodeLookup.Clear();

            var current = NextPathNode(null, start, CalcDistance(start, target));

            openBag.Add(current);
            //openList.Add(current);
            AddLookup(start, current);

            while (openBag.Count > 0 && !closedListPos.Contains(target))
            {
                //current = openList[0];
                current = openBag[0];
                openBag.RemoveFirst();
                //openList.RemoveAt(0);
                openListPos.Remove(current.Position);
                closedListPos.Add(current.Position);

                if (current.Steps > maxLength || current.Steps + current.Distance / 2 > maxLength)
                {
                    continue;
                }

                for (var x = -1; x <= 1; x++)
                {
                    for (var y = -1; y <= 1; y++)
                    {
                        if (x == 0 && y == 0)
                        {
                            continue;
                        }

                        var np = current.Position;
                        np.X += x;
                        np.Y += y;

                        if (np.X < 0 || np.Y < 0 || np.X >= walkData.Width || np.Y >= walkData.Height)
                        {
                            continue;
                        }

                        if (openListPos.Contains(np))
                        {
                            //the open list contains the neighboring cell. Check if the path from this node is better or not
                            var oldNode  = GetNode(np);
                            var dir      = (np - current.Position).GetDirectionForOffset();
                            var distance = CalcDistance(np, target);
                            var newF     = current.Score + 1 + distance + (dir.IsDiagonal() ? 0.4f : 0f);
                            if (newF < oldNode.F)
                            {
                                oldNode.Set(current, np, CalcDistance(np, target));                                 //swap the old parent to us if we're better
                            }

                            continue;
                        }

                        if (closedListPos.Contains(np))
                        {
                            continue;
                        }


                        if (!walkData.IsCellWalkable(np))
                        {
                            continue;
                        }

                        if (x == -1 && y == -1)
                        {
                            if (!walkData.IsCellWalkable(current.Position.X - 1, current.Position.Y) ||
                                !walkData.IsCellWalkable(current.Position.X, current.Position.Y - 1))
                            {
                                continue;
                            }
                        }

                        if (x == -1 && y == 1)
                        {
                            if (!walkData.IsCellWalkable(current.Position.X - 1, current.Position.Y) ||
                                !walkData.IsCellWalkable(current.Position.X, current.Position.Y + 1))
                            {
                                continue;
                            }
                        }

                        if (x == 1 && y == -1)
                        {
                            if (!walkData.IsCellWalkable(current.Position.X + 1, current.Position.Y) ||
                                !walkData.IsCellWalkable(current.Position.X, current.Position.Y - 1))
                            {
                                continue;
                            }
                        }

                        if (x == 1 && y == 1)
                        {
                            if (!walkData.IsCellWalkable(current.Position.X + 1, current.Position.Y) ||
                                !walkData.IsCellWalkable(current.Position.X, current.Position.Y + 1))
                            {
                                continue;
                            }
                        }

                        if (np.SquareDistance(target) <= range)
                        {
                            Profiler.Event(ProfilerEvent.PathFoundIndirect);
                            return(NextPathNode(current, np, 0));
                        }

                        var newNode = NextPathNode(current, np, CalcDistance(np, target));
                        //openList.Add(newNode);

                        //InsertOpenNode(newNode);

                        openBag.Add(newNode);
                        //openListPos.Add(np);
                        AddLookup(np, newNode);
                        closedListPos.Add(np);

                        //openList.Sort((a, b) => a.F.CompareTo(b.F));
                    }
                }
            }

            Profiler.Event(ProfilerEvent.PathNotFound);

            return(null);
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            //Bag bagOfOrders = new Bag<Order>();

            var           dictOrdersByCustomers = new Dictionary <string, OrderedBag <Order> >();
            var           numOfCommands         = int.Parse(Console.ReadLine());
            var           bagOfAllItems         = new Bag <Order>();
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < numOfCommands; i++)
            {
                var command = Console.ReadLine();
                //AddOrder name; price; consumer
                if (command.StartsWith("AddOrder"))
                {
                    var newCommands = command.Replace("AddOrder ", "").Trim();

                    var orderInfo = newCommands.Split(';').ToArray();
                    var name      = orderInfo[0];
                    var price     = double.Parse(orderInfo[1]);
                    var consumer  = orderInfo[2];
                    var order     = new Order(name, price, consumer);
                    bagOfAllItems.Add(order);

                    if (!dictOrdersByCustomers.ContainsKey(consumer))
                    {
                        dictOrdersByCustomers.Add(consumer, new OrderedBag <Order>());
                    }
                    dictOrdersByCustomers[consumer].Add(order);
                    sb.AppendLine("Order added");
                }

                else if (command.StartsWith("DeleteOrders"))
                {
                    var name = command.Replace("DeleteOrders ", "").Trim();

                    if (!dictOrdersByCustomers.ContainsKey(name))
                    {
                        sb.AppendLine("No orders found");
                    }
                    else
                    {
                        sb.AppendLine($"{dictOrdersByCustomers[name].Count} orders deleted");
                        foreach (var item in dictOrdersByCustomers[name])
                        {
                            bagOfAllItems.Remove(item);
                        }
                        dictOrdersByCustomers.Remove(name);
                    }
                }

                else if (command.StartsWith("FindOrdersByConsumer"))
                {
                    var name = command.Replace("FindOrdersByConsumer ", "").Trim();

                    if (!dictOrdersByCustomers.ContainsKey(name))
                    {
                        sb.AppendLine("No orders found");
                    }
                    else
                    {
                        foreach (var item in dictOrdersByCustomers[name])
                        {
                            sb.AppendLine(item.ToString());
                        }
                    }
                }
                //FindOrdersByPriceRange fromPrice; toPrice
                else if (command.StartsWith("FindOrdersByPriceRange"))
                {
                    var bagByPrice      = new OrderedBag <Order>();
                    var name            = command.Replace("FindOrdersByPriceRange ", "").Trim();
                    var splittedCommand = name.Split(';').ToArray();

                    var under = double.Parse(splittedCommand[0]);
                    var over  = double.Parse(splittedCommand[1]);
                    foreach (var item in bagOfAllItems)
                    {
                        if (item.Price >= under && item.Price <= over)
                        {
                            bagByPrice.Add(item);
                        }
                    }
                    if (bagByPrice.Count == 0)
                    {
                        sb.AppendLine("No orders found");
                    }
                    else
                    {
                        foreach (var item in bagByPrice)
                        {
                            sb.AppendLine(item.ToString());
                        }
                        bagByPrice.Clear();
                    }
                }
            }
            Console.WriteLine(sb.ToString());
        }
Ejemplo n.º 8
0
 public void Clear()
 {
     laps.Clear();
 }
Ejemplo n.º 9
0
 public void Clear()
 {
     list.Clear();
     bag.Clear();
     bagByDesc.Clear();
 }
Ejemplo n.º 10
0
 ///<summary>Clears the open node list</summary>
 public void ClearAll()
 {
     _openNodes.Clear();
 }
Ejemplo n.º 11
0
 public void Clear()
 {
     byInsertionOrder.Clear();
     byValue.Clear();
     byValueReversed.Clear();
 }
Ejemplo n.º 12
0
 public void Clear()
 {
     itemsList.Clear();
     treeWithDuplicates.Clear();
     reversedTreeWithDuplicates.Clear();
 }
Ejemplo n.º 13
0
 public void Clear()
 {
     insertionOrder.Clear();
     acscendingOrder.Clear();
     descendingOrder.Clear();
 }