Exemple #1
0
        /// <summary>
        /// Create a puzzle solution out of the Dlx result.
        /// </summary>
        /// <param name="rows">The remaining Dlx nodes containing the solution values.</param>
        private void SolutionFound(IEnumerable <DlxNode> rows)
        {
            var size        = _puzzleSize.ToInt32();
            var newSolution = new Dictionary <PuzzleCoordinate, int>();

            foreach (var rowNode in rows)
            {
                if (rowNode != null)
                {
                    var value = rowNode.Coordinate.Row - 1;
                    var digit = value % size + 1;
                    value /= size;
                    var col = value % size;
                    value /= size;
                    var row = value % size;

                    var coord = new PuzzleCoordinate(row, col);
                    newSolution[coord] = digit;
                }
            }

            var solvedPuzzle = new Puzzle(_puzzleSize);

            foreach (var coord in newSolution.Keys.OrderBy(k => k))
            {
                solvedPuzzle[coord] = Convert.ToByte(newSolution[coord]);
            }

            SolutionList.Add(solvedPuzzle);
        }
        /// <summary>
        /// How many rows/columns in a box?
        /// </summary>
        /// <param name="puzzleSize">The size opf the overall puzzle</param>
        /// <returns>The square root of the PuzzleSize</returns>
        public static int BoxSize(this PuzzleSize puzzleSize)
        {
            if (puzzleSize == PuzzleSize.Undefined)
            {
                throw new ArgumentOutOfRangeException(nameof(puzzleSize), "Must be defined");
            }

            var boxSize = (int)Math.Sqrt(puzzleSize.ToInt32());

            return(boxSize);
        }
Exemple #3
0
        public PuzzleSet(PuzzleSize size)
        {
            if (!new[]
            {
                PuzzleSize.FourByFour,
                PuzzleSize.NineByNine,
                PuzzleSize.SixteenBySixteen
            }.Contains(size))
            {
                throw new ArgumentOutOfRangeException(nameof(size), $"Must be a defined {nameof(PuzzleSize)} value");
            }

            MaxValue = size.ToInt32();
            Bits     = new BitArray(MaxValue);
        }
        private void AssertIfNotFullSet(IEnumerable <PuzzleCoordinateAndValue> sudokuSet, PuzzleSize puzzleSize)
        {
            var maxVal = puzzleSize.ToInt32();

            var sudokuArray = sudokuSet.ToArray();

            var setValues = sudokuArray.Select(s => s.Value)
                            .Where(v => v.HasValue)
                            .Select(v => v.Value)
                            .OrderBy((v => v))
                            .Distinct()
                            .ToList();

            Assert.Equal(maxVal, setValues.Count);
            Assert.Equal(1, setValues.First());
            Assert.Equal(maxVal, setValues.Last());
        }
Exemple #5
0
        public PuzzleSet(PuzzleSize size, [NotNull] IEnumerable <byte> values)
        {
            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            if (!new[]
            {
                PuzzleSize.FourByFour,
                PuzzleSize.NineByNine,
                PuzzleSize.SixteenBySixteen
            }.Contains(size))
            {
                throw new ArgumentOutOfRangeException(nameof(size), $"Must be a defined {nameof(PuzzleSize)} value");
            }

            MaxValue = size.ToInt32();
            Bits     = new BitArray(MaxValue);

            AddRange(values);
        }
 public static IEnumerable <byte> ValidCellValues(this PuzzleSize puzzleSize)
 {
     return(Enumerable.Range(1, puzzleSize.ToInt32()).Select(Convert.ToByte));
 }