public override AbstractState execute(Node node)
 {
     if (!(node.State is TilesState))
         throw new InvalidCastException();
     TilesState state = node.State as TilesState;
     int[,] tiles = state.GetTiles();
     int rowCoord = state.EmptyTileRowCoord;
     int colCoord = state.EmptyTileColumnCoord;
     if (CanShift(ShiftDirectionEnum.DOWN, rowCoord, colCoord))
     {
         tiles[rowCoord, colCoord] = tiles[rowCoord + 1, colCoord];
         tiles[rowCoord + 1, colCoord] = 0;
         state = new TilesState(tiles);
         return state;
     }
     return null;
 }
Beispiel #2
0
        static void Main(string[] args)
        {
            int[,] rootMatrix = new int[,] {{7, 2, 4 },{5, 0, 6 },{8, 3, 1 }};
            TilesState rootState = new TilesState(rootMatrix);

            IList<AbstractAction> actions = new List<AbstractAction>();
            actions.Add(new ShiftEmptyTileLeftAction());
            actions.Add(new ShiftEmptyTileRightAction());
            actions.Add(new ShiftEmptyTileUpAction());
            actions.Add(new ShiftEmptyTileDownAction());

            Node root = new Node(rootState);

            HeuristicCalculator calculator = new TilesHeuristicCalculator();
            NextStatesProvider provider = new NextTileStatesProvider(actions);

            // Console.WriteLine(calculator.Calculate(rootState, new TilesState(new int[,] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } })));

            try
            {
                var path = PathBuilder.BuildNodePathUsingAStarWithoutSystem(root, new Node(new TilesState(new int[,] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } })), calculator, provider);

                //foreach (var node in path)
                 //   Console.WriteLine(node);

                foreach (var action in PathTransformer.TransformNodePathToActionPath(path))
                    Console.WriteLine(action);
            }
            catch (PathNotFoundException ex) { Console.WriteLine(ex.Message); }

            /*

            var uniqueNodes = NodeGraphFactory.CreateGraph(root, actions.ToArray());

            //Console.WriteLine(uniqueNodes.Count);

            try
            {
                var path = PathBuilder.BuildNodePath(root, new Node(new TilesState(new int[,] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } })), uniqueNodes.Count);

                foreach (var action in PathTransformer.TransformNodePathToActionPath(path))
                    Console.WriteLine(action);
            }
            catch (PathNotFoundException ex) { Console.WriteLine(ex.Message); }
            Console.WriteLine("\n-----------\n");
            try
            {
                var path = PathBuilder.BuildNodePath(root, new Node(new TilesState(new int[,] { { 8, 0, 6 }, { 5, 4, 7 }, { 2, 3, 1 } })), uniqueNodes.Count);

                foreach (var action in PathTransformer.TransformNodePathToActionPath(path))
                    Console.WriteLine(action);
            }
            catch (PathNotFoundException ex) { Console.WriteLine(ex.Message); }

            Console.WriteLine("\n-----------\n");

            try
            {
                var path = PathBuilder.BuildNodePath(root, new Node(new TilesState(new int[,] { { 1, 2, 3 }, { 8, 0, 4 }, { 7, 6, 5 } })), uniqueNodes.Count);

                foreach (var action in PathTransformer.TransformNodePathToActionPath(path))
                    Console.WriteLine(action);
            }
            catch (PathNotFoundException ex) { Console.WriteLine(ex.Message); }
            */
            Console.ReadKey();
        }