Class MagicSquaresConstraint
Enforces a constraint that certain regions in a puzzle must be magic squares, i.e. the sums of the values in each of their rows, columns, and (optionally) their diagonals add up to the same value.
Note that this does not enforce uniqueness of values within the magic square as a whole. It does, however, prevent value duplication within each row, column, and/or diagonal. This can be combined with the BoxUniquenessConstraint if you need box-level uniqueness.
Inheritance
Implements
Inherited Members
Namespace: SudokuSpice.ConstraintBased.Constraints
Assembly: SudokuSpice.dll
Syntax
public class MagicSquaresConstraint : IConstraint
Remarks
This makes use of OptionalObjective objects to construct a complicated graph.
For example, in a standard 3x3 magic square for a standard 9x9 Sudoku puzzle, the magic sum (i.e. required sum for each row/column/diagonal) is 15. This can be formed through various combinations, eg:
- 1,5,9
- 1,6,8 ...
For each row or column or diagonal, this looks at the existing values to determine the possible sets. It drops impossible Possibility objects, and groups the remaining possibilities as follows (using the 1,5,9 set as an example):
In this row/column/diagonal, create an optional objective to require that a single 1 is selected from these squares. Repeat for the 5 and the 9.
Then, group each of these optional objectives into another optional objective that requires all of them to be satisfied. This defines an individual possible set for this row/column/diagonal.
Repeat this for all the possible sets on this row/column/diagonal. Reuse groups where possible, for example set 1,6,8 would use the same "1" grouping from set 1,5,8.
Now group all these optional set objectives into a single required objective that can be satisfied by any of these optional sets.
In the end, this results in a single required objective for each row/column/diagonal, enforcing that this row/column/diagonal is composed of one of the possible sets.
Constructors
| Improve this Doc View SourceMagicSquaresConstraint(ReadOnlySpan<Int32>, IEnumerable<Box>, Boolean)
Constructs a constraint that will enforce that the given squares
are
magic squares based on the rows, columns, and, optionally, the diagonals.
Declaration
public MagicSquaresConstraint(ReadOnlySpan<int> possibleValues, IEnumerable<Box> squares, bool includeDiagonals = true)
Parameters
Type | Name | Description |
---|---|---|
System.ReadOnlySpan<System.Int32> | possibleValues | The possible values that can be in the magic squares. |
System.Collections.Generic.IEnumerable<Box> | squares | The locations of the magic squares. |
System.Boolean | includeDiagonals | If true, values along the diagonals of the square must also sum to the magic number. |
Exceptions
Type | Condition |
---|---|
System.ArgumentException | If the any of the given |
Methods
| Improve this Doc View SourceTryConstrain(IReadOnlyPuzzle, ExactCoverGraph)
Adds necessary IObjectives and links to the given
graph
in order to solve the given puzzle
according to this constraint. The details here are implementation-specific.
Declaration
public bool TryConstrain(IReadOnlyPuzzle puzzle, ExactCoverGraph graph)
Parameters
Type | Name | Description |
---|---|---|
IReadOnlyPuzzle | puzzle | The puzzle to solve. |
ExactCoverGraph | graph | The exact-cover graph to constrain. |
Returns
Type | Description |
---|---|
System.Boolean | False if the constraint could not be satisfied by the given puzzle, else true. |
Remarks
This should skip adding objectives that are already satisfied by the given
puzzle
. Instead, it should drop the relevant
Possibilitys that are no longer possible.
Note: See ExactCoverGraph to understand how the graph works.