Esempio n. 1
0
        internal static void AssertMagicSquaresSatisfied(
            IReadOnlyPuzzle puzzle, int expectedSum, bool verifyDiagonals)
        {
            int boxSize = Boxes.IntSquareRoot(puzzle.Size);
            var boxes   = new Box[puzzle.Size];

            for (int boxIdx = 0; boxIdx < boxes.Length; ++boxIdx)
            {
                boxes[boxIdx] = new Box(Boxes.GetStartingBoxCoordinate(boxIdx, boxSize), boxSize);
            }
            AssertMagicSquaresSatisfied(puzzle, boxes, expectedSum, verifyDiagonals);
        }
Esempio n. 2
0
        private static bool _TryAppendRequirementsInBox(
            int box, int boxSize, IReadOnlyPuzzle puzzle, ExactCoverGraph graph)
        {
            Coordinate startCoord = Boxes.GetStartingBoxCoordinate(box, boxSize);
            var        endCoord   = new Coordinate(
                startCoord.Row + boxSize, startCoord.Column + boxSize);
            Span <Coordinate> boxCoordinates = stackalloc Coordinate[puzzle.Size];
            int i = 0;

            for (int row = startCoord.Row; row < endCoord.Row; row++)
            {
                for (int col = startCoord.Column; col < endCoord.Column; col++)
                {
                    boxCoordinates[i++] = new Coordinate(row, col);
                }
            }
            return(ConstraintUtil.TryImplementUniquenessConstraintForSquares(puzzle, boxCoordinates, graph));
        }
Esempio n. 3
0
        internal static void AssertStandardPuzzleSolved(IReadOnlyPuzzle puzzle)
        {
            Assert.Equal(0, puzzle.NumEmptySquares);
            var alreadyFound = new HashSet <int>(puzzle.Size);

            for (int row = 0; row < puzzle.Size; row++)
            {
                alreadyFound.Clear();
                for (int col = 0; col < puzzle.Size; col++)
                {
                    Assert.True(alreadyFound.Add(puzzle[row, col].Value), $"Value at ({row}, {col}) clashed with another value in that row!");
                }
            }
            for (int col = 0; col < puzzle.Size; col++)
            {
                alreadyFound.Clear();
                for (int row = 0; row < puzzle.Size; row++)
                {
                    Assert.True(alreadyFound.Add(puzzle[row, col].Value), $"Value at ({row}, {col}) clashed with another value in that col!");
                }
            }
            int boxSize = Boxes.IntSquareRoot(puzzle.Size);

            for (int box = 0; box < puzzle.Size; box++)
            {
                alreadyFound.Clear();
                (int startRow, int startCol) = Boxes.GetStartingBoxCoordinate(box, boxSize);
                for (int row = startRow; row < startRow + boxSize; row++)
                {
                    for (int col = startCol; col < startCol + boxSize; col++)
                    {
                        Assert.True(alreadyFound.Add(puzzle[row, col].Value), $"Value at ({row}, {col}) clashed with another value in that box!");
                    }
                }
            }
        }
Esempio n. 4
0
 public void GetStartingBoxCoordinate_SucceedsForValidValues(int boxSize, int box, int row, int col)
 {
     Assert.Equal(new Coordinate(row, col), Boxes.GetStartingBoxCoordinate(box, boxSize));
 }