Show / Hide Table of Contents

Class MagicSquaresRule

Enforces a rule 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 other rules if you need proper row, column, box, and/or diagonal uniqueness.

Inheritance
object
MagicSquaresRule
Implements
IRule
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: SudokuSpice.RuleBased.Rules
Assembly: SudokuSpice.dll
Syntax
public class MagicSquaresRule : IRule

Constructors

| Edit this page View Source

MagicSquaresRule(int, IEnumerable<Box>, bool)

Constructs a rule that will enforce that the given squares are magic squares based on the rows, columns, and, optionally, the diagonals.

Unlike other rules, this rule can only be constructed for a specific puzzle size. Attempting to initialize this rule with an incompatible puzzle will fail.

Declaration
public MagicSquaresRule(int size, IEnumerable<Box> squares, bool includeDiagonals)
Parameters
Type Name Description
int size

The size of the puzzle this rule applies to.

IEnumerable<Box> squares

The locations of the magic squares. The squares' size must be the square root of the puzzle size.

bool includeDiagonals

If true, values along the diagonals of the square must also sum to the magic number.

Exceptions
Type Condition
ArgumentException

If the any of the given squares' sizes are not compatible with the given puzzle's size.

Methods

| Edit this page View Source

CopyWithNewReference(IReadOnlyPuzzle?)

Creates a deep copy of this IRule, with any internal IReadOnlyPuzzle references updated to the given puzzle.

Declaration
public IRule CopyWithNewReference(IReadOnlyPuzzle? puzzle)
Parameters
Type Name Description
IReadOnlyPuzzle puzzle

The new puzzle reference to use. To ensure this rule's internal state is correct, this puzzle should contain the same data as the current puzzle referenced by this rule.

Returns
Type Description
IRule
| Edit this page View Source

GetPossibleValues(in Coordinate)

Gets the possible values for the given coordinate based on this rule.

Declaration
public BitVector GetPossibleValues(in Coordinate c)
Parameters
Type Name Description
Coordinate c
Returns
Type Description
BitVector

The possible values represented as a BitVector.

Remarks

When implementing this method, avoid depending on dynamic information in the current puzzle being solved, such as the currently set values. This method must return information that reflects the most recent calls to Update or Revert, as it can be called before or after modifying the underlying puzzle's data.

| Edit this page View Source

Revert(in Coordinate, int)

Undoes an update for the given value at the specified coordinate.

This performs the same internal updates as Revert(in Coordinate, int, CoordinateTracker), but without passing affected coordinates back to the caller. Therefore this is more efficient in cases where the caller already knows all the possible coordinates that could be affected.

Declaration
public void Revert(in Coordinate c, int val)
Parameters
Type Name Description
Coordinate c

The coordinate where a value is being unset.

int val

The value being unset.

Remarks

This method will always be called before reverting the value on the underlying puzzle.

| Edit this page View Source

Revert(in Coordinate, int, CoordinateTracker)

Undoes an update for the given value at the specified coordinate. Tracks affected coordinates in the given tracker.

Declaration
public void Revert(in Coordinate c, int val, CoordinateTracker coordTracker)
Parameters
Type Name Description
Coordinate c

The coordinate where a value is being unset.

int val

The value being unset.

CoordinateTracker coordTracker

The coordinates of unset squares impacted by this change will be tracked in this tracker.

Remarks

This method will always be called before reverting the value on the underlying puzzle.

| Edit this page View Source

TryInit(IReadOnlyPuzzle, BitVector)

Tries to initialize this rule to prepare to solve the given puzzle.

Declaration
public bool TryInit(IReadOnlyPuzzle puzzle, BitVector uniquePossibleValues)
Parameters
Type Name Description
IReadOnlyPuzzle puzzle

The puzzle to be solved.

BitVector uniquePossibleValues

All the unique possible values for this puzzle.

Returns
Type Description
bool

False if the puzzle violates this rule and initialization fails, else true.

Remarks

In general, it doesn't make sense to want to maintain the previous state if this method fails. Therefore, it is not guaranteed that the rule's state is unchanged on failure.

| Edit this page View Source

Update(in Coordinate, int, CoordinateTracker)

Updates possible values based on setting the given coordinate to the given value. Tracks affected coordinates in the given tracker.

Declaration
public void Update(in Coordinate c, int val, CoordinateTracker coordTracker)
Parameters
Type Name Description
Coordinate c

The coordinate to update.

int val

The value to set c to.

CoordinateTracker coordTracker

The coordinates of unset squares impacted by this change will be tracked in this tracker.

Remarks

This method will always be called before setting the value on the underlying puzzle.

Implements

IRule
  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX