示例#1
0
        public void ComputeSets_ForNonUniqueValues_Throws()
        {
            var values = new int[4];

            values.AsSpan().Fill(3);
            Assert.Throws <ArgumentException>(() => MagicSquares.ComputeSets(values));
        }
示例#2
0
 /// <summary>
 /// Constructs a constraint that will enforce that the given <paramref name="squares"/> are
 /// magic squares based on the rows, columns, and, optionally, the diagonals.
 /// </summary>
 /// <param name="possibleValues">
 /// The possible values that can be in the magic squares.
 /// </param>
 /// <param name="squares">
 /// The locations of the magic squares.
 /// </param>
 /// <param name="includeDiagonals">
 /// If true, values along the diagonals of the square must also sum to the magic number.
 /// </param>
 /// <exception cref="ArgumentException">
 /// If the any of the given <paramref name="squares"/>' sizes are not compatible with the
 /// length of <paramref name="possibleValues"/>.
 /// </exception>
 public MagicSquaresConstraint(ReadOnlySpan <int> possibleValues, IEnumerable <Box> squares, bool includeDiagonals = true)
 {
     _size             = possibleValues.Length;
     _magicSquares     = squares.ToArray();
     _squareSize       = Boxes.IntSquareRoot(_size);
     _includeDiagonals = includeDiagonals;
     if (_magicSquares.Any(
             b =>
             b.TopLeft.Row < 0 || b.TopLeft.Column < 0 ||
             b.TopLeft.Row + b.Size > _size || b.TopLeft.Column + b.Size > _size ||
             b.Size != _squareSize))
     {
         throw new ArgumentException(
                   $"Based on the {nameof(possibleValues)}, {nameof(squares)} must fit in a puzzle of size {_size} and have size {_squareSize}.");
     }
     _allPossibleValues = new BitVector();
     for (int i = 0; i < possibleValues.Length; ++i)
     {
         if (_allPossibleValues.IsBitSet(possibleValues[i]))
         {
             throw new ArgumentException("Values must be unique.");
         }
         _allPossibleValues.SetBit(possibleValues[i]);
     }
     _possibleSets = MagicSquares.ComputeSets(possibleValues, _squareSize, _allPossibleValues);
 }
示例#3
0
        public void ComputeSets_ForNonSquareSizes_Throws(int size)
        {
            var values = new int[size];

            for (int i = 0; i < size; ++i)
            {
                values[i] = i + 1;
            }
            Assert.Throws <ArgumentException>(() => MagicSquares.ComputeSets(values));
        }