/// <summary> /// Constructs a new puzzle whose data matches the given array. /// </summary> /// <param name="puzzleMatrix"> /// The data for this Sudoku puzzle. Preset squares should be set, and unset squares should /// be null. A copy of this data is stored in this <c>Puzzle</c>. /// </param> public Puzzle(int?[,] puzzleMatrix) { NumSquares = puzzleMatrix.Length; Size = puzzleMatrix.GetLength(0); if (Size != puzzleMatrix.GetLength(1)) { throw new ArgumentException("Puzzle must be square."); } BoxSize = Size switch { 1 => 1, 4 => 2, 9 => 3, 16 => 4, 25 => 5, _ => throw new ArgumentException("Size must be one of [1, 4, 9, 16, 25]."), }; _squares = (int?[, ])puzzleMatrix.Clone(); _unsetCoordsTracker = new CoordinateTracker(Size); for (int row = 0; row < Size; row++) { for (int col = 0; col < Size; col++) { if (!_squares[row, col].HasValue) { _unsetCoordsTracker.Add(new Coordinate(row, col)); } } } _allPossibleValues = new int[Size]; for (int i = 0; i < Size; i++) { _allPossibleValues[i] = i + 1; } }
private static void _InitUnsetCoordsTrackerAndSquares(CoordinateTracker unsetCoordsTracker, Span <int?[]> squares) { for (int row = 0; row < squares.Length; row++) { squares[row] = new int?[squares.Length]; for (int col = 0; col < squares.Length; col++) { unsetCoordsTracker.Add(new Coordinate(row, col)); } } }
/// <summary> /// Constructs a new puzzle of the given side length. /// </summary> /// <param name="size"> /// The side-length for this Sudoku puzzle. Must be a square of a whole number in the range [1, 25]. /// </param> /// <exception cref="ArgumentException"> /// Thrown if size is not the square of a whole number, or is outside the range [1, 25]. /// </exception> public Puzzle(int size) { Size = size; switch (size) { case 1: NumSquares = 1; BoxSize = 1; break; case 4: NumSquares = 4 * 4; BoxSize = 2; break; case 9: NumSquares = 9 * 9; BoxSize = 3; break; case 16: NumSquares = 16 * 16; BoxSize = 4; break; case 25: NumSquares = 25 * 25; BoxSize = 5; break; default: throw new ArgumentException("Size must be one of [1, 4, 9, 16, 25]."); } _squares = new int?[size, size]; _unsetCoordsTracker = new CoordinateTracker(size); for (int row = 0; row < Size; row++) { for (int col = 0; col < Size; col++) { if (!_squares[row, col].HasValue) { _unsetCoordsTracker.Add(new Coordinate(row, col)); } } } _allPossibleValues = new int[size]; for (int i = 0; i < size; i++) { _allPossibleValues[i] = i + 1; } }
private static void _InitUnsetCoordsTracker(CoordinateTracker unsetCoordsTracker, ReadOnlySpan <int?[]> squares) { for (int row = 0; row < squares.Length; row++) { var squaresRow = squares[row]; for (int col = 0; col < squaresRow.Length; col++) { if (!squaresRow[col].HasValue) { unsetCoordsTracker.Add(new Coordinate(row, col)); } } } }