setAt method

void setAt({
  1. required int row,
  2. required int col,
  3. required int value,
})

Sets the value at the specified row and column on the Sudoku board.

If the row or col is out of range, a RangeError is thrown. If the value is out of range, a RangeError is thrown. If setting the value would invalidate the board, an ArgumentError is thrown.

Implementation

void setAt({required int row, required int col, required int value}) {
  _checkRowCol(row, col);
  if (value < 0 || value > Board.maxValue) {
    throw RangeError(
        'value must be between 0 and ${Board.maxValue}. 0 is the blank value');
  }
  var boardWithValue = Board.clone(this);
  boardWithValue._values[row][col] = value;
  if (boardWithValue.isValid) {
    // The addition of the value doesn't invalidate the board.
    _values[row][col] = value;
  } else {
    // The addition of the value invalidates the board.
    throw ArgumentError(
        'Cannot set ($row, $col) to "$value" as it would invalidate the board');
  }
}