Example #1
0
        private bool ApplySolvers()
        {
            bool squaresSolved = false;

            foreach (BaseGroupSolver sudokuSolver in solvers)
            {
                // Calling solve 27 times for all the rows, columns, and blocks.
                for (int c = 0; c < 9; c++)
                {
                    if (sudokuSolver.Solve(SudokuBoard.GetColumn(c), GroupKind.Column) == SolveResult.SquaresSolved)
                    {
                        squaresSolved = true;
                    }
                }
                for (int r = 0; r < 9; r++)
                {
                    if (sudokuSolver.Solve(SudokuBoard.GetRow(r), GroupKind.Row) == SolveResult.SquaresSolved)
                    {
                        squaresSolved = true;
                    }
                }
                for (int r = 0; r < 3; r++)
                {
                    for (int c = 0; c < 3; c++)
                    {
                        if (sudokuSolver.Solve(SudokuBoard.GetBlock(r * 3, c * 3), GroupKind.Block) == SolveResult.SquaresSolved)
                        {
                            squaresSolved = true;
                        }
                    }
                }
            }

            return(squaresSolved);
        }
Example #2
0
        private void ShowNotesForSquareAt(ISudokuSquare square)
        {
            ISudokuSquare[] column = SudokuBoard.GetColumn(square.Column);
            ISudokuSquare[] row    = SudokuBoard.GetRow(square.Row);
            ISudokuSquare[] block  = SudokuBoard.GetBlock(square.Row, square.Column);

            List <char> availableChars = new List <char>();

            foreach (char item in tbxAvailableCharacter.Text)
            {
                availableChars.Add(item);
            }

            RemoveCharactersFromGroup(availableChars, row);
            RemoveCharactersFromGroup(availableChars, column);
            RemoveCharactersFromGroup(availableChars, block);

            square.SetNotes(string.Join(", ", availableChars));
        }
Example #3
0
        bool CheckForConflicts(int r, int c, bool setHasConflictedProperty = true)
        {
            ISudokuSquare thisSquare = SudokuBoard.squares[r, c];
            string        text       = thisSquare.GetText();

            if (string.IsNullOrWhiteSpace(text))
            {
                return(false);
            }

            ISudokuSquare[] column = SudokuBoard.GetColumn(c);
            ISudokuSquare[] row    = SudokuBoard.GetRow(r);
            ISudokuSquare[] block  = SudokuBoard.GetBlock(r, c);

            bool isConflicted = false;

            for (int rowIndex = 0; rowIndex < 9; rowIndex++)
            {
                if (rowIndex != r && column[rowIndex].GetText() == text)
                {
                    if (setHasConflictedProperty)
                    {
                        thisSquare.HasConflict       = true;
                        column[rowIndex].HasConflict = true;
                    }
                    isConflicted = true;
                }
            }

            for (int colIndex = 0; colIndex < 9; colIndex++)
            {
                if (colIndex != c && row[colIndex].GetText() == text)
                {
                    if (setHasConflictedProperty)
                    {
                        thisSquare.HasConflict    = true;
                        row[colIndex].HasConflict = true;
                    }
                    isConflicted = true;
                }
            }

            for (int squareIndex = 0; squareIndex < 9; squareIndex++)
            {
                GetSquarePosition(block[squareIndex], out int blockRow, out int blockColumn);
                if (blockRow == r && blockColumn == c)
                {
                    continue;
                }

                if (block[squareIndex].GetText() == text)
                {
                    if (setHasConflictedProperty)
                    {
                        thisSquare.HasConflict         = true;
                        block[squareIndex].HasConflict = true;
                    }
                    isConflicted = true;
                }
            }

            return(isConflicted);
        }