Пример #1
0
        private void Calculate(EightPuzzleNode start)
        {
            try
            {
                for (var i = 0; i <= 8; i++)
                {
                    if (start.Tiles.Contains(i))
                    {
                        continue;
                    }
                    throw new Exception("Fields filled improperly");
                }
                if (!start.IsSolvable)
                {
                    throw new Exception("Puzzle is unsolvable");
                }
                dataGridView1.ClearSelection();

                var goal = new EightPuzzleNode {
                    Tiles = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 0 }
                };

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

                INode result = pathFinder.Execute(start, goal);
                PrintSolution(result, dataGridView1);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
        protected static void AddSuccessor(EightPuzzleNode node,
                                           ICollection<INode> result,
                                           int swapTile)
        {
            var newState = node.Tiles.Clone() as int[];
            if (newState == null) return;
            newState[node.EmptyTileIndex] = newState[swapTile];
            newState[swapTile] = 0;

            if (!IsEqualToParentState(node.Parent, newState))
                result.Add(new EightPuzzleNode {Tiles = newState, Parent = node});
        }
        private static int FindTileCurrentIndex(int goalNumber, EightPuzzleNode current)
        {
            for (int j = 0; j < 9; j++)
            {
                if (current.Tiles[j] == goalNumber)
                {
                    return j;
                }
            }

            return -1;
        }
        private static int FindTileCurrentIndex(int goalNumber, EightPuzzleNode current)
        {
            for (int j = 0; j < 9; j++)
            {
                if (current.Tiles[j] == goalNumber)
                {
                    return(j);
                }
            }

            return(-1);
        }
Пример #5
0
        private static void PrintSolution(INode node, DataGridView dg)
        {
            if (node != null)
            {
                var stack = new Stack <INode>();

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

                EightPuzzleNode check = (EightPuzzleNode)stack.First();
                if (check.Tiles.SequenceEqual(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 0 }))
                {
                    MessageBox.Show("No need to solve solved puzzle");
                    return;
                }

                //var step = 0;

                foreach (EightPuzzleNode solutionNode in stack)
                {
                    dg.EditMode = DataGridViewEditMode.EditProgrammatically;

                    string tiles = solutionNode.Tiles
                                   .Aggregate("", (current, i) => current + i.ToString());

                    for (var i = 0; i < 9; i++)
                    {
                        var col = i % 3;
                        var row = i / 3;
                        dg.BeginEdit(true);
                        dg.NotifyCurrentCellDirty(false);
                        dg[col, row].Value = tiles[i] != '0' ? (object)(int)Char.GetNumericValue(tiles[i]) : null;
                        dg.EndEdit();
                        dg.NotifyCurrentCellDirty(false);
                    }

                    new ManualResetEvent(false).WaitOne(500);
                    dg.EditMode = DataGridViewEditMode.EditOnKeystroke;
                }

                MessageBox.Show(stack.Count - 1 + " steps");
            }
            else
            {
                MessageBox.Show("No solution");
            }
        }
Пример #6
0
        public void GivenHardestState_ShouldFindSolution()
        {
            // arrange
            var start = new EightPuzzleNode {
                Tiles = new[] { 8, 6, 7, 2, 5, 4, 3, 0, 1 }
            };

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

            // assert
            PrintSolution(_pathFinder, result);

            Assert.IsTrue(Goal.HasEqualState(result));
        }
Пример #7
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");
        }
        protected static void AddSuccessor(EightPuzzleNode node,
                                           ICollection <INode> result,
                                           int swapTile)
        {
            var newState = node.Tiles.Clone() as int[];

            if (newState == null)
            {
                return;
            }
            newState[node.EmptyTileIndex] = newState[swapTile];
            newState[swapTile]            = 0;

            if (!IsEqualToParentState(node.Parent, newState))
            {
                result.Add(new EightPuzzleNode {
                    Tiles = newState, Parent = node
                });
            }
        }