AStartTest.AStar.FindPath C# (CSharp) Method

FindPath() public method

public FindPath ( ) : List
return List
        public List<Tile> FindPath()
        {
            if (startTile == goalTile)
            {
                return new List<Tile>();
            }
            Node startNode = new Node(startTile);
            Node currentNode = startNode;
            List<Tile> neighborTiles;
            List<Tile> path = new List<Tile>();

            //Add current node to open nodes
            openNodes.Add(startNode);
            openDict.Add(startNode.tile.ID, startNode.tile);

            do
            {
                //Switch the lowest cost node to the closed list
                currentNode = GetLowestCostNodeFromOpenNodes();

                closedNodes.Add(currentNode);
                closedDict.Add(currentNode.tile.ID, currentNode.tile);
                openNodes.Remove(currentNode);
                openDict.Remove(currentNode.tile.ID);

                //Find walkable neighbor tiles not on the closed list

                neighborTiles = GetWalkableNeighborsNotOnClosedList(currentNode);

                //Handle if neighbor node is on the open list already
                //and add to open list
                AddNeighborNodesToOpenList(currentNode, neighborTiles);
            } while (openNodes.Count > 0 && currentNode.tile != goalTile);

            if (openNodes.Count == 0 && currentNode.tile != goalTile)
                return new List<Tile>();

            path.Add(currentNode.tile);
            do
            {
                for (int i = 0; i < closedNodes.Count; i++)
                {
                    if (closedNodes[i].tile == currentNode.parentTile)
                    {
                        currentNode = closedNodes[i];
                        break;
                    }
                }
                //currentNode = closedNodes.Find(new Predicate<Node>(delegate(Node node)
                //{
                //    return node.tile == currentNode.parentTile;
                // }));
                path.Add(currentNode.tile);

            } while (currentNode.parentTile != null);

            path.Reverse();

            return path;
        }

Usage Example

Exemplo n.º 1
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            aStar = new AStar(tileMap);
            List<Tile> path = aStar.FindPath();

            int index = r.Next(path.Count);

            if (path.Count > 0)
            {
                while (path[index].TileType == TileType.Goal || path[index].TileType == TileType.Start ||
                    index == path.Count - 2 || index == 1)
                {
                    index = r.Next(path.Count);
                }
                blocked.Add(path[index]);
                panel33_MouseDown(path[index].Panel, null);
                ExecuteAStar();
            }
            else
            {
                int count = 0;
                int index2;
                int minRemove = blocked.Count / 3;
                while (path.Count == 0 || count < minRemove)
                {
                    index2 = r.Next(blocked.Count);
                    panel33_MouseDown(blocked[index2].Panel, null);
                    blocked.RemoveAt(index2);

                    ExecuteAStar();
                    aStar = new AStar(tileMap);
                    path = aStar.FindPath();

                    count++;

                }

            }
        }
All Usage Examples Of AStartTest.AStar::FindPath