Пример #1
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 GivenSimpleState_ShouldFindSolution()
        {
            // arrange
            var start = new EightPuzzleNode {Tiles = new[] {4, 1, 3, 0, 2, 6, 7, 5, 8}};

            // act
            INode result = _pathFinder.Execute(start, Goal);

            // assert
            PrintSolution(_pathFinder, result);

            Assert.IsTrue(Goal.HasEqualState(result));
        }
        private static int GetEmptyTilePosition(EightPuzzleNode node)
        {
            int emptyTilePos = -1;

            for (int i = 0; i < 9; i++)
            {
                if (node.Tiles[i] == 0)
                {
                    emptyTilePos = i;
                    break;
                }
            }

            return emptyTilePos;
        }
Пример #4
0
        private static int GetEmptyTilePosition(EightPuzzleNode node)
        {
            int emptyTilePos = -1;

            for (int i = 0; i < 9; i++)
            {
                if (node.Tiles[i] == 0)
                {
                    emptyTilePos = i;
                    break;
                }
            }

            return(emptyTilePos);
        }