void solver_OnSolutionStepCompleted(object sender, SolutionStepCompletedEventArgs e)
        {
            if (!e.Finished)
            {
                PictureBox currentStepImg = stepImgs[currentIndex];
                Label      currentStep    = stepLabels[currentIndex];
                if (currentStepImg.InvokeRequired)
                {
                    currentStepImg.Invoke((MethodInvoker) delegate() { currentStepImg.Image = Properties.Resources.ok; });
                }
                if (currentStep.InvokeRequired)
                {
                    currentStep.Invoke((MethodInvoker) delegate() { currentStep.Text = e.Type == SolutionStepType.Standard ? string.Format("{0} moves", e.Algorithm.Moves.Count) : string.Empty; });
                }
                currentIndex++;

                if (currentIndex < stepImgs.Count)
                {
                    currentStepImg = stepImgs[currentIndex];
                    currentStep    = stepLabels[currentIndex];
                    if (currentStepImg.InvokeRequired)
                    {
                        currentStepImg.Invoke((MethodInvoker) delegate() { currentStepImg.Image = Properties.Resources.refresh; });
                    }
                    if (currentStep.InvokeRequired)
                    {
                        currentStep.Invoke((MethodInvoker) delegate() { currentStep.Text = "In progress ..."; });
                    }
                }
            }
            else
            {
                if (lblTimeHeader.InvokeRequired)
                {
                    lblTimeHeader.Invoke((MethodInvoker) delegate() { lblTime.Text = string.Format("{0:f2}s", e.Milliseconds / 1000.0); });
                }
                if (lblMovesHeader.InvokeRequired)
                {
                    lblMovesHeader.Invoke((MethodInvoker) delegate() { lblMoves.Text = string.Format("{0} moves", e.Algorithm.Moves.Count); });
                }
                if (lblHeader.InvokeRequired)
                {
                    lblHeader.Invoke((MethodInvoker) delegate() { lblHeader.Text = "Solution found."; });
                }
                if (btnAdd.InvokeRequired)
                {
                    btnAdd.Invoke((MethodInvoker) delegate() { btnAdd.Enabled = true; });
                }
                solver.OnSolutionStepCompleted -= solver_OnSolutionStepCompleted;
                this.Algorithm = e.Algorithm;
            }
        }
示例#2
0
        private void SolveAsync(Rubik rubik)
        {
            bool solvable = Solvability.FullTest(rubik);

            if (solvable)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Solve(rubik);
                sw.Stop();

                Algorithm = Algorithm.RemoveUnnecessaryMoves(Algorithm);
                var args = new SolutionStepCompletedEventArgs(Name, true, Algorithm, (int)sw.ElapsedMilliseconds);
                OnSolutionStepCompleted?.Invoke(this, args);

                solvingThread.Abort();
            }
            else
            {
                this.BroadcastOnSolutionError(this.Name, "Unsolvable cube");
            }
        }