Example #1
0
        //------------------------------------------
        // Utilise A* http://fr.wikipedia.org/wiki/Algorithme_A*
        // --------------------------------------------

        /// ----------------------------------------------------------------------------------------
        /// <summary>
        /// Calculate the shortest path between the start point and the end point.
        /// </summary>
        /// <remarks> The path is reversed. </remarks>
        /// <returns> The shortest path. </returns>
        /// ----------------------------------------------------------------------------------------
        private List <Point> CalculateBestPath()
        {
            //System.Diagnostics.Stopwatch chrono = new System.Diagnostics.Stopwatch();
            //chrono.Start();


            Map map = new Map(_jeu, _armee.positionCarte, _armee.moveTarget.GetValueOrDefault(), _armee);

            Node.NodeList open          = new Node.NodeList();
            Node.NodeList close         = new Node.NodeList();
            Node[]        possibleNodes = new Node[8];

            Node startNode = new Node(null, map.StartPoint, map);

            open.Add(startNode);
            int          nbIter = 0;
            List <Point> sol    = null;

            while (open.Count > 0)
            {
                Node best = open.RemoveFirst(map); // This is the best node
                if (best.MapPoint == map.EndPoint) // We are finished
                {
                    sol = new List <Point>();      // The solution
                    while (best.Parent != null)
                    {
                        sol.Add(best.MapPoint);
                        best = best.Parent;
                    }
                    // Return the solution when the parent is null (the first point)
                    break;
                }
                close.Add(best);
                this.AddToOpen(open, close, best, possibleNodes, best.GetPossibleNode(map, possibleNodes), map);
                nbIter++;
            }
            //chrono.Stop();
            //_jeu.messageDebug = "nb iter = " + nbIter + " en " + chrono.ElapsedMilliseconds + " ms" + "taille(close) = " + close.Count + " taille(open) = "+ open.Count;
            return(sol);
        }
Example #2
0
 /// ----------------------------------------------------------------------------------------
 /// <summary>
 /// Add a list of nodes to the open list if needed.
 /// </summary>
 /// <param name="current"> The current nodes. </param>
 /// <param name="nodes"> The nodes to add. </param>
 /// ----------------------------------------------------------------------------------------
 private void AddToOpen(Node.NodeList open, Node.NodeList close, Node current, Node[] nodes, int nbNodes, Map map)
 {
     for (int i = 0; i < nbNodes; i++)
     {
         Node node = nodes[i];
         if (!open.Contains(node))
         {
             if (!close.Contains(node))
             {
                 open.Add(node);
             }
         }
         // Else really needed ?
         else
         {
             if (node.CostWillBe(map) < open[node].Cost)
             {
                 node.SetParent(current, map);
             }
         }
     }
 }