Example #1
0
 // Constructor
 public AStar(World w, aNode start, aNode goal)
 {
     openList = new OpenList<aNode>();
     closedList = new ArrayList();
     startNode = start;
     goalNode = goal;
     thisWorld = w;
 }
Example #2
0
        public static void Main(string[] args)
        {
            World world = new World ();
            bool dataValid;
            int[] matchesArray = new int[2];
            int userBeginX, userBeginY, userGoalX, userGoalY;
            char newIOPos, newWorld;
            bool firstRun = true;

            do {
                world.generateUnpathableBlocks ();
                do {
                    Console.Clear();
                    if (firstRun) {
                        Console.Write(world.worldPrint () + "\n");
                        Console.WriteLine("Welcome! This is a demo of the A* pathfinding algorithm.");
                        Console.WriteLine("(Grid Positions can be entered as (1,1) or 1,1 or 1 1.)\n");
                    } else
                        Console.Write(world.worldPrint() + "\n\n\n");

                    MatchCollection matches = null;
                    do { // for data validation
                        Console.Write ("Enter (x,y) for the start position: ");
                        dataValid = false;
                        String temp = Console.ReadLine ();
                        matches = Regex.Matches (temp, @"\d+");

                        if (matches.Count == 2) {
                            dataValid = true;
                            int val = 0;
                            for (int i = 0; i < 2 && dataValid; i++) {
                                matchesArray[i] =
                                    ((Int32.TryParse(matches[i].Value, out val)) &&
                                                   val <= 15 && val > 0) ? val : -1;
                                dataValid = matchesArray[i] == -1 ? false: true;
                            }
                        }
                        dataValid = world.blockIsPathable(matchesArray[0] - 1, matchesArray[1] - 1);

                        if (!dataValid)
                            Console.WriteLine("The coordinates you entered were not accepted.");
                    } while (!dataValid);
                    userBeginX = matchesArray[0];
                    userBeginY = matchesArray[1];
                    matchesArray [0] = matchesArray [1] = 0;

                    // goal entry
                    do { // for data validation
                        Console.Write ("Enter (x,y) for the goal position: ");
                        dataValid = false;
                        String temp = Console.ReadLine ();
                        matches = Regex.Matches (temp, @"\d+");

                        if (matches.Count == 2) {
                            dataValid = true;
                            int val = 0;
                            for (int i = 0; i < 2 && dataValid; i++) {
                                matchesArray[i] =
                                    ((Int32.TryParse(matches[i].Value, out val)) &&
                                        val <= 15 && val > 0) ? val : -1;
                                dataValid = matchesArray[i] == -1 ? false: true;
                            }
                        }
                        dataValid = world.blockIsPathable(matchesArray[0] - 1, matchesArray[1] - 1) && dataValid;

                        if (!dataValid)
                            Console.WriteLine("The coordinates you entered were not accepted.");
                    } while (!dataValid);
                    userGoalX = matchesArray [0];
                    userGoalY = matchesArray [1];

                    // initialize the start node and goal node
                    aNode beginNode = new aNode (userBeginX-1, userBeginY-1);
                    aNode goalNode 	= new aNode (userGoalX-1, userGoalY-1);

                    // find the solution
                    AStar aStarInstance = new AStar (world, beginNode, goalNode);
                    bool solutionExists = aStarInstance.AStarSearch ();

                    if (solutionExists && aStarInstance.generatedPath.Count > 1) { // print solution
                        Console.Clear();
                        Console.Write(world.worldPrint(aStarInstance.generatedPath, beginNode, goalNode));
                        Console.WriteLine("\nThe path was found!");
                        Console.WriteLine("Here is the path from start to finish:");
                        foreach (aNode n in aStarInstance.generatedPath)
                            Console.Write("({0},{1}) ", n.x+1, n.y+1);
                        Console.Write("\n");
                    } else if (solutionExists && aStarInstance.generatedPath.Count == 1) {
                        Console.Clear();
                        Console.Write(world.worldPrint(aStarInstance.generatedPath));
                        Console.WriteLine("\nLooks like you didn't go anywhere...");
                        Console.WriteLine("Here is where you started and ended:");
                        foreach (aNode n in aStarInstance.generatedPath)
                            Console.Write("({0},{1}) ", n.x+1, n.y+1);
                        Console.Write("\n");
                    } else {
                        Console.Clear();
                        Console.Write("\n\n" + world.worldPrint(beginNode, goalNode));
                        Console.WriteLine("\nNo path found.\n");
                        Console.Write("\n");
                    }

                    // same board, new positions?
                    Console.Write("\n Would you like to try new start and goal positions? : [YyNn] ");
                    do {
                        try {
                            newIOPos = Console.ReadLine()[0];
                        } catch {
                            dataValid = false;
                            newIOPos = 'f';
                            continue;
                        }
                        if (newIOPos != 'Y' && newIOPos != 'y' && newIOPos != 'N' && newIOPos != 'n')
                        {
                            dataValid = false;
                            Console.Write("Key invalid. (Try Y or N)");
                        }
                        else
                        {
                            dataValid = true;
                            if(newIOPos == 'y' || newIOPos == 'Y') {
                                Console.Clear();
                                Console.Write("\n\n\n");
                            }
                        }
                    } while (!dataValid);
                } while (newIOPos == 'y' || newIOPos == 'Y');

                // new board and positions?
                Console.Write(" Would you like to try with a new board? : [YyNn] ");
                do {
                    try {
                        newWorld = Console.ReadLine()[0];
                    } catch {
                        dataValid = false;
                        newWorld = 'f';
                        continue;
                    }
                    if (newWorld != 'Y' && newWorld != 'y' && newWorld != 'N' && newWorld != 'n')
                    {
                        dataValid = false;
                        Console.Write("Key invalid. (Try Y or N)");
                    }
                    else
                    {
                        dataValid = true;
                    }
                } while (!dataValid);
            } while (newWorld == 'y' || newWorld == 'Y');
            Console.WriteLine ("Thanks for the visit. \nPress any key...");
            Console.Read ();
        }