Skip to content

Boards

Implementations of a Sudoku board and related objects.

Coordinate dataclass

Represents a coordinate on a board.

Upon creation a Coordinate validates the passed indices. After the validation passes and the Coordinate is created the indices cannot be changed.

Parameters:

Name Type Description Default
row int

Row index of the coordinate. Needs to be between 0 and 8, both included.

required
col int

Col index of the coordinate. Needs to be between 0 and 8, both included.

required

Raises:

Type Description
ValueError

If row or column index are invalid. Indices are invalid in case they are not between zero and 8, both included.

CallType

Bases: Enum

Allowed call types of the Board constructor.

The __init__ method of a Board should only be called by the provided factory methods. The enum is used to describe the call types.

Board

Board(
    values: list[int],
    _call_type: CallType = CallType.NON_PRIVATE,
)

Models a possibly unfinished Sudoku board.

The constructor initializes the class but assumes a valid input. Do not directly instantiate this class. Use one of the factory methods:

  • from_nested_lists
  • from_string
  • from_list

Parameters:

Name Type Description Default
values list[int]

Values to initialize the Board. Values are not checked.

required
_call_type CallType

Indicates if the constructor was called from outside the class. Defaults to CallType.NON_PRIVATE, which indicates it was called called directly.

NON_PRIVATE

Raises:

Type Description
RuntimeError

If constructor is called directly, that is not using one of the listed factory methods

from_nested_lists classmethod

from_nested_lists(values: list[list[int]]) -> Board

Return a new Board based on the passed values.

The passed argument needs to have exactly nine elements. Each of them has to be a list with nine integers between zero and nine, both included. A value of zero marks an empty field.

Parameters:

Name Type Description Default
values list[list[int]]

The values used to create the board

required

Returns:

Type Description
Board

Board holding the values given by values.

Raises:

Type Description
InvalidBoardError

Raised in the following cases:

  • If the outter list has less than nine inner lists.
  • If an inner list has less than nine elements.
  • If the values are not between zero and nine.

from_list classmethod

from_list(values: list[int]) -> Board

Return a new Board based on the passed values.

The passed argument needs to have exactly 81 integers between 0 and 9, both included. A value of zero marks an empty field.

Parameters:

Name Type Description Default
values list[int]

The values used to create the board.

required

Returns:

Type Description
Board

Board holding the values given by values.

Raises:

Type Description
InvalidBoardError

Raised in the following cases:

  • If values does not have exactly 81 elements
  • If elements of values are not between 0 and 9, both included.

from_string classmethod

from_string(values: str) -> Board

Return a new Board based on the passed values.

The passed string needs to have exactly 81 characters. Each character being an integer.

Parameters:

Name Type Description Default
values str

The values used to create the board

required

Returns:

Type Description
Board

Board holding the values given by values after converting to integer

Raises:

Type Description
InvalidBoardError

Raised in the following cases:

  • If the passed string does not have exactly 81 characters
  • If there is a character that is not convertible to an integer
  • If the converted characters are not integers between zero and nine

available_col_values

available_col_values(column_index: int) -> frozenset[int]

Return the allowed but unused values for the column given by column_index.

Parameters:

Name Type Description Default
column_index int

The column index specifying the column for which the values should be retrieved. The index is zero based, hence the allowed values are between zero and eight, both included.

required

Returns:

Type Description
frozenset[int]

Values available for the column identified by the passed column_index

Raises:

Type Description
InvalidIndexError

If column_index is less than zero or greater than eight.

available_row_values

available_row_values(row_index: int) -> frozenset[int]

Return the allowed but unused values for the row given by row_index.

Parameters:

Name Type Description Default
row_index int

The row index specifying the row for which the values should be retrieved. The index is zero based, hence the allowed values are between zero and eight, both included.

required

Returns:

Type Description
frozenset[int]

Values available for the row indentified by the passed row_index

Raises:

Type Description
InvalidIndexError

If row_index is less than zero or greater or equal eight.

available_square_values

available_square_values(
    coordinate: Coordinate,
) -> frozenset[int]

Return the allowed but unused values of the 3x3 square that contains coordinate.

The board is separated in in 3x3 squares starting from Coordinate(0, 0). Every three rows and columns a new square starts.

Parameters:

Name Type Description Default
coordinate Coordinate

Coordinate used to determine the square for which the values should be returned.

required

Returns:

Type Description
frozenset[int]

Values available for the 3x3 square the given coordinate is located in.

is_valid

is_valid(coordinate: Coordinate, value: int) -> bool

Check that value is valid for coordinate based on the current state of the board.

A value is seen as valid if all of the following cases hold:

  • Value is between 1 and 9 both included.
  • Value is not yet present in the row the cell belongs to. This does not include zeros. The value zero is allowed multiple times.
  • Value is not yet present in the column the cell belongs to. This does not include zeros. The value zero is allowed multiple times.
  • Value is not yet present in the 3x3 square the cell is in. This does not include zeros. The value zero is allowed multiple times.

Parameters:

Name Type Description Default
coordinate Coordinate

Position on the board.

required
value int

Value for position given by coordinate.

required

Returns:

Type Description
bool

True if value is valid for coordinate otherwise False.