Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Board board = new Board(6, 6);
            Monster human = new Monster("Human", 'h');
            Monster dog = new Monster("Dog", 'd');
            Coordinate humanLoc = new Coordinate(1, 1);
            Coordinate dogLoc = new Coordinate(4, 4);
            board.AddMonster(humanLoc, human);
            board.AddMonster(dogLoc, dog);
            Console.Out.WriteLine(board);
            Console.Out.WriteLine("Shortest path from 1,1 to 4,4:");
            List<Coordinate> path = board.GetShortestPath(humanLoc, dogLoc);
            Console.Out.WriteLine(path);

            Console.Read();
        }
Exemplo n.º 2
0
        public static List<Coordinate> GetShortestPath(Board board, Coordinate start, Coordinate goal)
        {
            HashSet<Coordinate> visited = new HashSet<Coordinate>();
            Dictionary<Coordinate, Coordinate> parents = new Dictionary<Coordinate, Coordinate>();
            Dictionary<Coordinate, double> gScore = new Dictionary<Coordinate, double>();
            HeapPriorityQueue<Coordinate> fScoreQueue = new HeapPriorityQueue<Coordinate>(board.MaxSize());
            parents[start] = start;
            gScore.Add(start, 0);
            fScoreQueue.Enqueue(start, gScore[start] + Heuristic(start, goal));
            while (fScoreQueue.Count() != 0)
            {
                Coordinate current = fScoreQueue.Dequeue();
                //Console.WriteLine("Current = " + current.ToString());
                if (current.Equals(goal))
                {
                    Console.WriteLine("FOUND GOAL!!!");
                    return ReconstructPath(parents, goal);
                }

                visited.Add(current);
                List<Coordinate> neighbors = board.GetNeighbors(current);
                foreach (Coordinate neighbor in neighbors)
                {
                    if (visited.Contains(neighbor)) continue;
                    if (!board.GetSquare(neighbor).IsTraversable()) continue;
                    double newGScore = gScore[current] + Distance(current, neighbor);
                    if (!fScoreQueue.Contains(neighbor))
                    {
                        parents[neighbor] = current;
                        gScore[neighbor] = newGScore;
                        fScoreQueue.Enqueue(neighbor, newGScore + Heuristic(neighbor, goal));
                    }
                    else if (newGScore < gScore[neighbor])
                    {
                        parents[neighbor] = current;
                        gScore[neighbor] = newGScore;
                        fScoreQueue.UpdatePriority(neighbor, newGScore + Heuristic(neighbor, goal));
                    }

                }
            }

            return null;
        }
Exemplo n.º 3
0
        /*
        static void TestPathFind()
        {
            Board board = new Board(6, 6);
            Coordinate humanLoc = new Coordinate(1, 1);
            Coordinate dogLoc = new Coordinate(4, 4);
            Creature human = new Creature(humanLoc, 'h');
            Creature dog = new Creature(dogLoc, 'd');
            board.AddCreature(humanLoc, human);
            board.AddCreature(dogLoc, dog);
            Console.WriteLine(board);
            Console.WriteLine("Shortest path from 1,1 to 4,4:");
            List<Coordinate> path = board.GetShortestPath(humanLoc, dogLoc);
            foreach (Coordinate coord in path)
            {
                Console.WriteLine(coord);
                board.AddMonster(coord, new Monster("Path", '*'));
            }
            Console.WriteLine(board);
        }
        */
        static void TestFileRead()
        {
            string filename = "Resources\\Boards\\8x8.txt";
            Board board = new Board(filename);
            Console.WriteLine(board);
            Coordinate start = new Coordinate(1, 1);
            Coordinate end = new Coordinate(8, 7);
            List<Coordinate> path = board.GetShortestPath(start, end);
            foreach (Coordinate coord in path)
            {
                Console.WriteLine(coord);
                board.AddItem(coord, new Path(coord));
            }
            Console.WriteLine(board);

            filename = "Resources\\Boards\\15x15H.txt";
            board = new Board(filename);
            Console.WriteLine(board);
            start = new Coordinate(15, 1);
            end = new Coordinate(15, 14);
            path = board.GetShortestPath(start, end);
            foreach (Coordinate coord in path)
            {
                Console.WriteLine(coord);
                board.AddItem(coord, new Path(coord));
            }

            Console.WriteLine(board);
            filename = "Resources\\Boards\\maze.txt";
            board = new Board(filename);
            Console.WriteLine(board);
            start = new Coordinate(0, 5);
            end = new Coordinate(12,7);
            path = board.GetShortestPath(start, end);
            foreach (Coordinate coord in path)
            {
                Console.WriteLine(coord);
                board.AddItem(coord, new Path(coord));
            }
            Console.WriteLine(board);
        }