Esempio n. 1
0
        private void FindFailureX10000ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var       stopwatch   = Stopwatch.StartNew();
            const int SearchCount = 10;

            for (int i = 0; i < SearchCount; i++)
            {
                var scramble       = Scrambler.Scamble();
                var solutionFinder = new ShortestSolutionFinder(WellKnownSolvers.Roux);
                var(foundSolution, description, states) = solutionFinder.FindSolution(
                    Rotator.ApplyMoves(Puzzle.Solved, scramble));

                if (!foundSolution)
                {
                    this.txtScrambleMoves.Text = scramble;
                    this.UpdateSolutionUI(description, states);
                    return;
                }
            }
            stopwatch.Stop();

            this.txtScrambleMoves.Text       = string.Empty;
            this.txtSolutionMoves.Text       = string.Empty;
            this.txtSolutionDescription.Text = $"{SearchCount} solutions found in {stopwatch.ElapsedMilliseconds}ms.";
            this.Refresh();
        }
Esempio n. 2
0
        private void cmdSolve_Click(object sender, EventArgs e)
        {
            var solutionFinder = new ShortestSolutionFinder(WellKnownSolvers.Roux);

            var(_, description, states) = solutionFinder.FindSolution(
                Rotator.ApplyMoves(Puzzle.Solved, this.txtScrambleMoves.Text));

            UpdateSolutionUI(description, states);
        }
Esempio n. 3
0
        private void FindAlgorithmSolutionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.txtScrambleMoves.Text       = string.Empty;
            this.txtSolutionMoves.Text       = string.Empty;
            this.txtSolutionDescription.Text = string.Empty;
            this.Refresh();

            var    solver      = WellKnownSolvers.Roux;
            var    algorithm   = solver.Algorithms["PairToRightFront.RightBackUp"];
            var    stopwatch1  = Stopwatch.StartNew();
            Puzzle foundPuzzle = null;

            for (int i = 0; i < 10000; i++)
            {
                var initialPuzzle = Rotator.ApplyMoves(Puzzle.Solved, Scrambler.Scamble());
                foundPuzzle = new AlgorithmUsageFinder(WellKnownSolvers.Roux, algorithm).FindUsage(initialPuzzle);
                if (foundPuzzle != null)
                {
                    break;
                }
            }
            stopwatch1.Stop();

            if (foundPuzzle == null)
            {
                this.txtSolutionDescription.Text = "No usage of this algorithm in this puzzle.";
                return;
            }

            this.txtSolutionDescription.Text = $"Found puzzle for algorithm in {stopwatch1.ElapsedMilliseconds.ToString()}ms";

            var stopwatch2 = Stopwatch.StartNew();
            var solutions  = new SolutionSearch(4, SolutionSearch.AllRouxMoves, algorithm.FinishedState)
                             .Search(foundPuzzle);

            stopwatch2.Stop();

            var shortestLength = solutions.Min(value => value.Count());

            var message = this.txtSolutionDescription.Text + Environment.NewLine
                          + $"Found solutions in {stopwatch2.ElapsedMilliseconds.ToString()}ms\r\n"
                          + string.Join(
                ", ",
                from solution in solutions
                where solution.Count() == shortestLength
                select NotationParser.FormatMoves(solution));

            this.txtSolutionDescription.Text = message;
        }
        protected virtual bool Visit(Puzzle puzzle, Algorithm algorithm, ImmutableArray <NotationMoveType> moves)
        {
            if (this.WalkerStates.Count > this.MaximumDepth)
            {
                this.AtMaximumDepth();
                return(false);
            }

            this.WalkerStates.Add(new SolveWalkerState(puzzle, algorithm, moves));
            try
            {
                this.AlgorithmsTried++;
                this.TotalMoves += moves.Length;
                return(this.Visit(Rotator.ApplyMoves(puzzle, moves)));
            }
            finally
            {
                this.WalkerStates.RemoveAt(this.WalkerStates.Count - 1);
                this.TotalMoves -= moves.Length;
            }
        }