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
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 |
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
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 |
Raises:
| Type | Description |
|---|---|
InvalidBoardError
|
Raised in the following cases:
|
from_list
classmethod
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 |
Raises:
| Type | Description |
|---|---|
InvalidBoardError
|
Raised in the following cases:
|
from_string
classmethod
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 |
Raises:
| Type | Description |
|---|---|
InvalidBoardError
|
Raised in the following cases:
|
available_col_values
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 |
available_row_values
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 |
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 |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|