findSolutions static method

List<Board> findSolutions(
  1. Board puzzle, {
  2. FindSolutionsProgress? progressCallback,
  3. int maxSolutions = 1,
})

Finds solution(s) for a given puzzle board.

If defined, progressCallback will be called to report progress. As puzzle is not guaranteed to be a "canonical" Sudoku board, which must have only one solution, a maxSolutions maximum number of solutions to be found can be specified.

If puzzle is not a solvable board, an ArgumentError is thrown. If maxSolutions is less than 1, an ArgumentError is thrown.

Implementation

static List<Board> findSolutions(final Board puzzle,
    {final FindSolutionsProgress? progressCallback,
    final int maxSolutions = 1}) {
  // Validate the parameters
  if (maxSolutions < 1) {
    throw ArgumentError('maxSolutions must be at least 1');
  }
  _checkSolvable(puzzle);

  // Initialize progress reporting
  if (progressCallback != null) {
    progressCallback(0);
  }

  var solutions = <Board>[];
  _findSolutions(puzzle, progressCallback, maxSolutions, solutions);

  return solutions;
}