コード例 #1
0
        private void bwSolver_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            SolutionResults results = e.Result as SolutionResults;

            SolutionProgress = 100;
            Difficulty       = results.Difficulty;
            Message          = string.Join(Environment.NewLine, results.Comments.ToArray());
        }
コード例 #2
0
        SolutionResults SolveCore(BackgroundWorker bw, bool showSolution)
        {
            int             squaresToSolve = _squares.Length;
            SolutionResults result         = new SolutionResults();

            //arbitrary measure of the amount of effort needed to solve the puzzle
            int effortNeeded = 0;

            while (true)
            {
                if (bw != null)
                {
                    bw.ReportProgress(Convert.ToInt32((double)GetSolvedSquareCount() / (double)squaresToSolve * 100));
                }

                if (showSolution)
                {
                    ShowSolution();
                }

                effortNeeded += 1;

                if (PerformBacktracking() == 0)
                {
                    effortNeeded += 3;  //checking for singltons is more difficult

                    if (CheckForSingletons() == 0)
                    {
                        effortNeeded += 12;  //checking for pairs is way more difficult

                        if (CheckForPairs() == 0)
                        {
                            if (!result.Comments.Contains(Resources.AmbiguousSolutionMsg))
                            {
                                result.Comments.Add(Resources.AmbiguousSolutionMsg);
                            }

                            //we're not getting anywhere - make a guess
                            for (int i = 0; i < _squares.Length; i++)
                            {
                                if (!_squares[i].ActualValue.HasValue)
                                {
                                    Debug.Assert(_squares[i].NonDismissedValues.Count > 0);
                                    _squares[i].ActualValue = _squares[i].NonDismissedValues[0];
                                    break;
                                }
                            }
                        }
                    }
                }

                if (GetSolvedSquareCount() == squaresToSolve)
                {
                    break;
                }
            }

            //difficulty of 10 is the max
            if (effortNeeded > 1024)
            {
                effortNeeded = 1024;
            }

            result.Difficulty = Math.Log(effortNeeded, 2);

            return(result);
        }