Example #1
0
        private void ReconstructPath(PuzzleSolver puzzleSolver)
        {
            HanoiNode currentNode = puzzleSolver.endNode;

            while (currentNode.parent != null)
            {
                ulong tempState1 = currentNode.state;
                ulong tempState2 = currentNode.parent != null ? currentNode.parent.state : 0;
                for (int i = 0; i < diskCount; ++i)
                {
                    if (tempState1 % 10 != tempState2 % 10)
                    {
                        solution.Add(new Move((byte)(tempState2 % 10), (byte)(tempState1 % 10)));
                    }
                    tempState1 /= 10;
                    tempState2 /= 10;
                }
                currentNode = currentNode.parent;
            }
            solution.Reverse();
        }
Example #2
0
        private void StartSolveButton_Clicked(object sender, RoutedEventArgs e)
        {
            uint fsmovecount = FrameStewart.Hanoi(diskCount, pegCount);

            FSmoveCountLabel.Text = $"Frame Stewart: {fsmovecount} moves";

            Action solvingCompleted = () => {
                ChangeFrameworkElementState(true, startVisualizationButton, diskCountInputTextBox, pegCountInputTextBox);
                ChangeFrameworkElementState(false, abortSolveButton);

                Dispatcher.Invoke(() =>
                {
                    solution = new List <Move>();
                    ReconstructPath(puzzleSolver);
                    Print($"{solution.Count} moves\r{puzzleSolver.ElapsedTime} elapsed\rVisited {puzzleSolver.VisitedStates} states\r");
                    PrintMoves();
                    Print("===================\r");
                    BFSmoveCountLabel.Text = $"BFS: {solution.Count} moves";
                    string elapsedTime     = puzzleSolver.ElapsedTime.ToString();
                    elapsedTimeLabel.Text  = $"Elapsed time:   {elapsedTime.Substring(3, Math.Min(13, elapsedTime.Length - 3))}";
                });
            };

            Action solvingAborted = () => {
                Dispatcher.Invoke(() =>
                {
                    Print("Aborted\r");
                    Print("===================\r");
                });
            };

            puzzleSolver = new PuzzleSolverBFS(diskCount, pegCount);
            Print("Starting to solve...\r");
            puzzleSolver.Start(solvingCompleted, solvingAborted);

            ChangeFrameworkElementState(true, abortSolveButton);
            canStartSolving = false;
            ChangeFrameworkElementState(false, startSolveButton, startVisualizationButton, abortVisualizationButton, resetVisualizationButton, diskCountInputTextBox, pegCountInputTextBox);
        }