Exemple #1
0
        private void WorkWithPriorityQ(int maxSize)
        {
            ShowName("PriorityQ");
            PriorityQ <long> prioQ = new PriorityQ <long>(maxSize);
            Random           rnd   = new Random();

            for (int i = 0; i < maxSize; i++)
            {
                prioQ.Insert(rnd.Next(0, 2000));
            }
            try
            {
                prioQ.Insert(6);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Remove(prioQ);

            try
            {
                prioQ.Remove();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            ShowEndMessage();
        }
Exemple #2
0
        static void Main(string[] args)
        {
            PriorityQ mypQ = new PriorityQ(4);

            mypQ.Insert(2);
            mypQ.Insert(6);
            mypQ.Insert(5);

            int rem1 = mypQ.Remove();
            int rem2 = mypQ.Remove();
            int rem3 = mypQ.Remove();

            Console.WriteLine("Values out from PQ are: {0},{1},{2}", rem1, rem2, rem3);

            Console.Read();
        }
Exemple #3
0
        public void AddThreeItems()
        {
            var priorityQueue = new PriorityQ <Data>();

            priorityQueue.Enqueue(new Data(message: "Priority High", priority: 10));
            priorityQueue.Enqueue(new Data("Low Priority", priority: 0));
            priorityQueue.Enqueue(new Data("Priority Medium", priority: 5));

            priorityQueue.Enqueue(new Data(message: "Priority High", priority: 10));
            priorityQueue.Enqueue(new Data("Low Priority", priority: 0));
            priorityQueue.Enqueue(new Data("Priority Medium", priority: 5));

            var message = priorityQueue.Dequeue();

            Assert.AreEqual(10, message.Priority);

            message = priorityQueue.Dequeue();
            Assert.AreEqual(10, message.Priority);

            priorityQueue.Enqueue(new Data(message: "Priority High", priority: 10));
            message = priorityQueue.Dequeue();
            Assert.AreEqual(10, message.Priority);
        }
Exemple #4
0
    private List <Ground> FindPath(Vector2 startIndex, Vector2 targetIndex)
    {
        // get the source & destination tile
        Ground source      = GetTileFromIndex(startIndex);
        Ground destination = GetTileFromIndex(targetIndex);
        // set the heap
        PriorityQ <Ground> openSet   = new PriorityQ <Ground>(tilemap.GetLength(0) * tilemap.GetLength(1));
        HashSet <Ground>   closedSet = new HashSet <Ground>();

        openSet.Add(source);

        while (openSet.getSize() > 0)
        {
            Ground current = openSet.ExtractFirstItem();
            closedSet.Add(current);

            // end
            if (current == destination)
            {
                return(GetPath(source, destination));
            }
            // examine each neighbour
            foreach (Ground neighbourTile in GetNeighbourTiles(current))
            {
                // tile is occupied
                if (neighbourTile != destination)
                {
                    if (neighbourTile.isOccupied == true || closedSet.Contains(neighbourTile))
                    {
                        continue;
                    }
                }
                // destination tile is occupied by a building
                else
                {
                    if (neighbourTile.hasUnit == false && neighbourTile.isOccupied == true || closedSet.Contains(neighbourTile))
                    {
                        continue;
                    }
                }
                // get the neighbout tile having the least cost
                int movementCost = current.gCost + GetDistance(current, neighbourTile);
                if (movementCost < neighbourTile.gCost || !openSet.IsItemInQ(neighbourTile))
                {
                    Ground neighbour = neighbourTile;
                    neighbour.gCost      = movementCost;
                    neighbour.hCost      = GetDistance(neighbourTile, destination);
                    neighbour.hCost      = GetDistance(neighbourTile, destination);
                    neighbour.parentTile = current;

                    if (!openSet.IsItemInQ(neighbourTile))
                    {
                        openSet.Add(neighbourTile);
                    }
                    else
                    {
                        openSet.Update(neighbourTile);
                    }
                }
            }
        }
        return(null);
    }