コード例 #1
0
 private void moveToClosedList(Hex hex)
 {
     openList.Remove(hex);
     addToClosedList(hex);
 }
コード例 #2
0
        public bool findPath(Hex start, Hex end)
        {
            // If start and end are the same there is no path!
            if (start == end)
            {
                return(false);
            }

            // Make sure we have valid hexes
            if (start == null || end == null)
            {
                return(false);
            }

            // Clear the board and all paths
            reset();

            // Add starting hex to the open list
            addToOpenList(start);

            // Try to find the path until the target hex (end) is found
            while (closedList.Contains(end) != true)
            {
                bool setFirst  = true;
                int  lowestF   = 0;
                Hex  lowestHex = null;

                // Find the lowest F in openlist
                foreach (Hex selectHex in openList)
                {
                    if (setFirst)
                    {
                        lowestF  = selectHex.Path.F;
                        setFirst = false;
                    }

                    if (selectHex.Path.F <= lowestF)
                    {
                        lowestF   = selectHex.Path.F;
                        lowestHex = selectHex;
                    }
                }

                if (setFirst)
                {
                    // Open list is empty
                    // No possible route can be found
                    return(false);
                }

                // Now add the lowest F to the closed list and remove it from the open list
                moveToClosedList(lowestHex);

                // Add the neighbor hexes to the open list
                List <Hex> neighbors = board.getNeighboringCells(lowestHex, 1);
                foreach (Hex nodeHex in neighbors)
                {
                    if (!isAccessible(nodeHex, start))
                    {
                        addToClosedList(nodeHex);
                    }
                    else
                    {
                        if (!closedList.Contains(nodeHex))
                        {
                            // Not in the closed list, so check if its in the open list meaning we already
                            // have processed it's path parameters
                            if (openList.Contains(nodeHex))
                            {
                                // This node is already in our open list.  Check to see if this is a better move
                                if (nodeHex.Path.G < lowestHex.Path.G)
                                {
                                    lowestHex.Path.Parent = nodeHex;
                                    lowestHex.Path.G      = nodeHex.Path.G + 10;
                                    lowestHex.Path.F      = lowestHex.Path.G + lowestHex.Path.H;
                                }
                            }
                            else
                            {
                                // Not in closed list or open list, so add to open list.
                                addToOpenList(nodeHex);

                                // Set parent
                                nodeHex.Path.Parent = lowestHex;

                                // Update H, G, F
                                int H = board.getHexDistance(nodeHex, end) * 10;
                                int G = lowestHex.Path.G + 10;
                                int F = H + G;
                                nodeHex.Path.update(H, G, F);
                            }
                        }
                    }
                }
            }

            // Get Path
            Hex hex = end;

            do
            {
                path.Add(hex);
                hex = hex.Path.Parent;
            }while (hex != null);

            path.Reverse();

            return(path.Count() > 0);
        }