コード例 #1
0
        public SudokuBoard()
        {
            InitializeComponent();

            for (int i = 0; i < _squares.Length; i++)
            {
                _squares[i]          = new SudokuSquare();
                _squares[i].Board    = this;
                _squares[i].Position = new Point(i - Convert.ToInt32(i / 9) * 9, Convert.ToInt32(i / 9));
            }
        }
コード例 #2
0
        int GetSolvedSquareCount()
        {
            int solvedSquares = 0;

            for (int i = 0; i < _squares.Length; i++)
            {
                SudokuSquare square = _squares[i];
                if (square.ActualValue.HasValue)
                {
                    solvedSquares++;
                }
            }
            return(solvedSquares);
        }
コード例 #3
0
        Dictionary <int, List <SudokuSquare> > ExtractPossibilities(ConstraintType constraint, int i)
        {
            Dictionary <int, List <SudokuSquare> > possibilities = new Dictionary <int, List <SudokuSquare> >();

            for (int j = 0; j < 9; j++)
            {
                SudokuSquare square = null;

                switch (constraint)
                {
                case ConstraintType.Column:
                    square = GetSquare(new Point(i, j));
                    break;

                case ConstraintType.Row:
                    square = GetSquare(new Point(j, i));
                    break;

                case ConstraintType.SuperCell:
                    square = GetSquare(Utils.CalculatePoint(i, j));
                    break;
                }

                if (!square.ActualValue.HasValue)
                {
                    List <int> candidates = square.NonDismissedValues;
                    foreach (int candidate in candidates)
                    {
                        if (possibilities.ContainsKey(candidate))
                        {
                            possibilities[candidate].Add(square);
                        }
                        else
                        {
                            possibilities.Add(candidate, new List <SudokuSquare>(new SudokuSquare[] { square }));
                        }
                    }
                }
            }
            return(possibilities);
        }
コード例 #4
0
        public int PerformBacktracking()
        {
            int effectedSquares = 0;

            for (int i = 0; i < _squares.Length; i++)
            {
                SudokuSquare square   = _squares[i];
                Point        position = new Point(i - Convert.ToInt32(i / 9) * 9, Convert.ToInt32(i / 9));

                if (!square.ActualValue.HasValue)
                {
                    List <int> validValues = new List <int>();
                    List <int> candidates  = square.NonDismissedValues;
                    for (int j = 0; j < candidates.Count; j++)
                    {
                        if (IsValueValid(position, candidates[j], false))
                        {
                            validValues.Add(candidates[j]);
                        }
                        else
                        {
                            square.UpdateDismissedValue(candidates[j], true); //cache for later
                            effectedSquares++;
                        }
                    }

                    if (validValues.Count == 1)
                    {
                        square.ActualValue = validValues[0];
                        effectedSquares++;
                    }
                }
            }

            return(effectedSquares);
        }