private void SolveByBruteForce(SudokuBoard board) { var listOfAttempts = new List <Attempt>(); foreach (var sudokuRow in board.Rows) { foreach (var sudokuRowCell in sudokuRow.Cells.Where(c => !c.IsFilled)) { var column = board.Columns.FirstOrDefault(c => c.ColumnNumber == sudokuRowCell.ColumnNumber); var squareNumber = SudokuBoard.CalculateSquareNumber(sudokuRowCell.RowNumber, sudokuRowCell.ColumnNumber); var square = board.Squares.FirstOrDefault(s => s.SquareNumber == squareNumber); var unusedNumber = 0; for (int i = 1; i <= board.NumbersPerUnit; i++) { if (!sudokuRow.HasNumberBeenUsed(i) && !column.HasNumberBeenUsed(i) && !square.HasNumberBeenUsed(i) && !DoesAttemptExist(sudokuRowCell, i, listOfAttempts)) { unusedNumber = i; } } if (unusedNumber == 0) { continue; } //Try setting the number of the cell to the first unused number listOfAttempts.Add(new Attempt(sudokuRowCell, unusedNumber)); var attemptBoard = board.Clone(); var attemptCell = attemptBoard.GetCell(sudokuRowCell.ColumnNumber, sudokuRowCell.RowNumber); attemptCell.Number = unusedNumber; SolveBoard(attemptBoard); if (attemptBoard.IsSolved) { sudokuRowCell.Number = unusedNumber; SolveBoard(board); return; } } } }