Ejemplo n.º 1
0
        private List <Point> reconstructPath(AStarCell cell)
        {
            List <Point> path = new List <Point>();

            while (!(cell.getPreviousCell() == null))
            {
                path.Insert(0, cell.getPoint());
                cell = cell.getPreviousCell();
            }
            path.Insert(0, cell.getPoint());
            return(path);
        }
Ejemplo n.º 2
0
        public List <Point> calcShortestPath(int startX, int startY, int goalX, int goalY)
        {
            AStarCell startCell = map.getCell(startX, startY);
            AStarCell goalCell  = map.getCell(goalX, goalY);

            if (startCell == null || startCell.isObstacle())
            {
                return(null);
            }

            //Check if the goal cell is also an obstacle (if it is, it is impossible to find a path there)
            if (goalCell == null || goalCell.isObstacle())
            {
                return(null);
            }

            startCell.reset();
            startCell.setDistanceFromStart(0);
            closedList.Clear();
            openList.Clear();
            openList.Add(startCell);

            //while we haven't reached the goal yet
            while (openList.Count() != 0)
            {
                //get the first Cell from non-searched Cell list, sorted by lowest distance from our goal as guessed by our heuristic
                AStarCell current = openList[0];

                // check if our current Cell location is the goal Cell. If it is, we are done.
                if (current.getX() == goalX && current.getY() == goalY)
                {
                    return(reconstructPath(current));
                }

                //move current Cell to the closed (already searched) list
                openList.Remove(current);
                closedList.Add(current);

                //go through all the current Cells neighbors and calculate if one should be our next step
                foreach (AStarCell neighbor in current.getNeighborList())
                {
                    bool neighborIsBetter;

                    //if we have already searched this Cell, don't bother and continue to the next one
                    if (closedList.Contains(neighbor))
                    {
                        continue;
                    }

                    //also just continue if the neighbor is an obstacle
                    if (!neighbor.isObstacle())
                    {
                        // calculate how long the path is if we choose this neighbor as the next step in the path
                        float neighborDistanceFromStart = (current.getDistanceFromStart() + map.getDistanceBetween(current, neighbor));

                        //add neighbor to the open list if it is not there
                        if (!openList.Contains(neighbor))
                        {
                            neighbor.reset();
                            openList.Add(neighbor);
                            neighborIsBetter = true;
                            //if neighbor is closer to start it could also be better
                        }
                        else if (neighborDistanceFromStart < current.getDistanceFromStart())
                        {
                            neighborIsBetter = true;
                        }
                        else
                        {
                            neighborIsBetter = false;
                        }
                        // set neighbors parameters if it is better
                        if (neighborIsBetter)
                        {
                            neighbor.setPreviousCell(current);
                            neighbor.setDistanceFromStart(neighborDistanceFromStart);
                            neighbor.setHeuristicDistanceFromGoal(heuristic.getEstimatedDistanceToGoal(neighbor.getPoint(), goalCell.getPoint()));

                            // csharp List.Sort use QuickSort, which is unstable,
                            // but in java implement ArrayList.sort use MergeSort, which is stable,
                            // so here use MergeSort to generate the same result as in java implement
                            MergeSortClass.MergeSort(openList);
                        }
                    }
                }
            }
            return(null);
        }