Exemple #1
0
        public void TestAdd()
        {
            List <Cell>    cellList   = new List <Cell>(testPath);
            MinHeap <Cell> cellHeap   = new MinHeap <Cell>(cellList);
            Vector3        cellVector = new Vector3(12, 7, 0);
            Cell           newCell    = new Cell(cellVector, new Vector3(10, 10, 0));

            cellHeap.Add(newCell);
            Assert.IsTrue(ValidMinHeap(cellHeap));
        }
        /// <summary>
        /// Method that switfly finds the best path from start to end. Doesn't reverse outcome
        /// </summary>
        /// <returns>The end breadcrump where each .next is a step back)</returns>
        private static SearchNode FindPathReversed(World world, Point3D start, Point3D end)
        {
            SearchNode startNode = new SearchNode(start, 0, 0, null);

            MinHeap openList = new MinHeap();

            openList.Add(startNode);

            int sizeX = world.WorldSizeX;
            int sizeY = world.WorldSizeY;
            int sizeZ = world.WorldSizeZ;

            bool[] brWorld = new bool[sizeX * sizeY * sizeZ];
            brWorld[start.X + (start.Y + start.Z * sizeY) * sizeX] = true;

            while (openList.HasNext())
            {
                SearchNode current = openList.ExtractFirst();

                if (current.position.GetDistanceSquared(end) <= 3)
                {
                    return(new SearchNode(end, current.pathCost + 1, current.cost + 1, current));
                }

                for (int i = 0; i < surrounding.Length; i++)
                {
                    Surr    surr       = surrounding[i];
                    Point3D tmp        = new Point3D(current.position, surr.Point);
                    int     brWorldIdx = tmp.X + (tmp.Y + tmp.Z * sizeY) * sizeX;

                    if (world.PositionIsFree(tmp) && brWorld[brWorldIdx] == false)
                    {
                        brWorld[brWorldIdx] = true;
                        int        pathCost = current.pathCost + surr.Cost;
                        int        cost     = pathCost + tmp.GetDistanceSquared(end);
                        SearchNode node     = new SearchNode(tmp, cost, pathCost, current);
                        openList.Add(node);
                    }
                }
            }
            return(null); //no path found
        }
Exemple #3
0
        public int PopMin(params int[] nums)
        {
            var sut = new MinHeap();

            foreach (var num in nums)
            {
                sut.Add(num);
            }

            return(sut.PopMin());
        }
Exemple #4
0
        public void TestAddMultipleRandom()
        {
            List <Cell>    cellList = new List <Cell>(testPath);
            MinHeap <Cell> cellHeap = new MinHeap <Cell>(cellList);

            for (int i = 0; i < 10; i++)
            {
                int     xRandom    = Random.Range(0, 100);
                int     yRandom    = Random.Range(0, 100);
                Vector3 cellVector = new Vector3(xRandom, yRandom, 0);
                Cell    newCell    = new Cell(cellVector, new Vector3(10, 10, 0));
                cellHeap.Add(newCell);
            }

            Assert.IsTrue(ValidMinHeap(cellHeap));
        }