Ejemplo n.º 1
0
        public void Draw(SpriteBatch spriteBatch, GraphicsDevice graphics)
        {
            // Draw grid
            spriteBatch.Draw(
                texture: GridBackgroundTexture,
                sourceRectangle: null,
                effects: SpriteEffects.None,
                origin: Vector2.Zero,
                rotation: 0f,
                destinationRectangle: gridBackgroundRect,
                color: Color.DarkSlateGray,
                layerDepth: 0);

            // Draw all cells
            for (int i = 0; i < 120; i++)
            {
                for (int j = 0; j < 160; j++)
                {
                    Cells[i, j].Draw(spriteBatch);
                }
            }

            if (AlgorithmResults != null)
            {
                AlgorithmResults.Draw(spriteBatch);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Runs the algorithm
        /// </summary>
        /// <returns>The results of the algorithm</returns>
        public AlgorithmResults Run()
        {
            while (this.open.Count > 0)
            {
                this.currentNode = PickLowestHeuristicValue();
                if (notDiscoveredYet.Contains(currentNode))
                {
                    notDiscoveredYet.Remove(currentNode);
                }

                // run the algorithm
                if (Done())
                {
                    // The algorithm is Done.
                    TracePath(goalNode);

                    AlgorithmResults info = new AlgorithmResults();
                    info.SetBestPathCost((int)goalNode.cost);
                    info.SetNodesExpanded(closed.Count);
                    info.SetSolutionPath(solutionPath);
                    return(info);
                }
                else
                {
                    closed.Add(currentNode);
                    open.Remove(currentNode);
                    ArrayList neighbors = GetNeighbourNodes(currentNode);
                    for (int i = 0; i < neighbors.Count; i++)
                    {
                        Node neighbourNode = (Node)neighbors[i];
                        if (closed.Contains(neighbourNode))
                        {
                            continue;
                        }

                        neighbourNode.updateNodeIfNeeded(currentNode, currentNode.cost + CalculateNodeCost(currentNode, neighbourNode));
                        neighbourNode.heuristic = CalculateHeuristic(neighbourNode);
                        if (!open.Contains(neighbourNode))
                        {
                            open.Add(neighbourNode);
                        }
                    }
                }
            }

            AlgorithmResults nfo = new AlgorithmResults();

            nfo.SetBestPathCost(-1);
            nfo.SetNodesExpanded(closed.Count);
            nfo.SetSolutionPath(solutionPath);
            return(nfo);
        }
Ejemplo n.º 3
0
        public void Update(GameTime gameTime, MouseState mouseState)
        {
            for (int i = 0; i < 120; i++)
            {
                for (int j = 0; j < 160; j++)
                {
                    Cells[i, j].Update(gameTime, mouseState);
                }
            }

            if (AlgorithmResults != null)
            {
                AlgorithmResults.Update();
            }
        }
        protected AlgorithmResults getResults(CellData[,] finalCellData)
        {
            AlgorithmResults ar = new AlgorithmResults();


            Vector2 s = vectorOf(goal);

            while (!s.Equals(vectorOf(start)))
            {
                ar.Path.Add(cellAt(s));
                s = finalCellData[(int)s.X, (int)s.Y].Parent.Value;
            }


            ar.Path.Add(start);

            for (int i = 0; i < NUM_GRID_ROW; i++)
            {
                for (int j = 0; j < NUM_GRID_COL; j++)
                {
                    cellGrid[i, j].H = finalCellData[i, j].H;
                    cellGrid[i, j].G = finalCellData[i, j].G;
                    cellGrid[i, j].F = finalCellData[i, j].H + finalCellData[i, j].G;
                }
            }

            cellGrid[start.X, start.Y].G = 0;
            cellGrid[start.X, start.Y].F = cellGrid[start.X, start.Y].H;

            ar.PathLength = goal.G;

            ar.NodesExpanded = nodesExpanded;
            ar.ElapsedTime   = DateTime.Now.Subtract(startTime);

            //using (System.IO.Stream stream = new System.IO.MemoryStream())
            //{
            //    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            //    formatter.Serialize(stream, this);
            //    ar.MemUsed = stream.Length;
            //}

            ar.Success = true;

            return(ar);
        }
        protected AlgorithmResults getResults()
        {
            AlgorithmResults ar = new AlgorithmResults();

            ar.Success     = true;
            ar.ElapsedTime = elapsedTime;

            Cell s = goal;

            bool addedParent = false;

            do
            {
                ar.Path.Add(s);
                if (s.Equals(start))
                {
                    addedParent = true;
                }

                s = s.Parent;
            } while (!addedParent);



            ar.NodesExpanded = nodesExpanded;
            ar.PathLength    = goal.G;


            //using (System.IO.Stream stream = new System.IO.MemoryStream())
            //{
            //    System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            //    formatter.Serialize(stream, this);
            //    ar.MemUsed = stream.Length;
            //}

            return(ar);
        }