private static void PrintSolution(PathFinder pathFinder, INode result)
        {
            int steps = 0;
            INode node = result;

            if (node != null)
            {
                var stack = new Stack<INode>();

                do
                {
                    stack.Push(node);
                } while ((node = node.Parent) != null);

                Debug.WriteLine("8-Puzzle Solved in {0} Cycles", pathFinder.Cycles);
                Debug.WriteLine("-------------------------------------------");

                foreach (EightPuzzleNode solutionNode in stack)
                {
                    string tiles = solutionNode.Tiles
                        .Aggregate("", (current, i) => current + i.ToString());

                    Debug.WriteLine("{0:00} - {1} -  F: {2:00.0}  G: {3:00.0}  H: {4:00.0}",
                                    steps++,
                                    tiles,
                                    solutionNode.F, solutionNode.G, solutionNode.H);
                }
            }
            else
            {
                Debug.WriteLine("No solution");
            }
        }
Пример #2
0
        private static void Main(string[] args)
        {
            do
            {
                var goal = new EightPuzzleNode { Tiles = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 0 } };
                var start = new EightPuzzleNode { Tiles = new int[9] };

                Console.WriteLine("Enter a valid start state (e.g. 867254301");
                string userinput = Console.ReadLine();

                if (userinput != null)
                {
                    int i = 0;

                    foreach (char s in userinput)
                    {
                        int tile;

                        if (!int.TryParse(s.ToString(), out tile)) continue;

                        start.Tiles[i++] = tile;
                    }

                    var pathFinder = new PathFinder(
                        new EightPuzzleSuccessorNodesGenerator(),
                        new EightPuzzleGValueCalculator(),
                        new EightPuzzleManhattanDistanceCalulator());

                    INode result = pathFinder.Execute(start, goal);
                    PrintSolution(result);

                    Console.ReadKey();
                }
            } while (Console.ReadLine() != "exit");
        }
 public void Init()
 {
     _pathFinder = new PathFinder(
         new EightPuzzleSuccessorNodesGenerator(),
         new EightPuzzleGValueCalculator(),
         new EightPuzzleManhattanDistanceCalulator());
 }