コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SudokuMove"/> class.
        /// </summary>
        /// <param name="row">The zero-based row of the <see cref="SudokuPuzzle"/> affected by the <see cref="SudokuMove"/>.</param>
        /// <param name="column">The zero-based column of the <see cref="SudokuPuzzle"/> affected by the <see cref="SudokuMove"/>.</param>
        /// <param name="number">The number used in the <see cref="SudokuMove"/>.</param>
        /// <param name="pattern">The pattern used in the <see cref="SudokuMove"/>.</param>
        /// <param name="possible">An array containing the numbers that were possible instantly before the <see cref="SudokuMove"/> move was made.</param>
        /// <exception cref="ArgumentException">The length of <c><paramref name="possible"/></c> is equal to 0, or any of its items are less than 0 - or - <c><paramref name="pattern"/></c> is equal to <see cref="SudokuPattern.None"/>.</exception>
        /// <exception cref="ArgumentNullException"><c><paramref name="possible"/></c> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><c><paramref name="row"/></c>, <c><paramref name="column"/></c> or <c><paramref name="number"/></c> is less than 0.</exception>
        public SudokuMove(int row, int column, int number, SudokuPattern pattern, int[] possible)
        {
            if (row < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(row));
            }

            if (column < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(column));
            }

            if (number < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(number));
            }

            if (pattern == SudokuPattern.None)
            {
                throw new ArgumentException(nameof(pattern));
            }

            this._Columns  = new int[] { column };
            this._Numbers  = new int[] { number };
            this._Possible = possible;
            this._Rows     = new int[] { row };
            this.Pattern   = pattern;
        }
コード例 #2
0
        private bool CheckMoveMade(int row, int column, int number, SudokuPattern pattern)
        {
            if (this.Moves.Count == 0)
            {
                return(false);
            }

            int count = 0;

            foreach (SudokuMove move in this)
            {
                count++;
                int nextCount = 0;

                foreach (SudokuMove nextMove in this)
                {
                    nextCount++;
                    if (Object.ReferenceEquals(move, nextMove))
                    {
                        continue;
                    }

                    if (SudokuSolver.Compare(move.Rows, nextMove.Rows) && SudokuSolver.Compare(move.Columns, nextMove.Columns) && SudokuSolver.Compare(move.Numbers, nextMove.Numbers) && move.Pattern == nextMove.Pattern)
                    {
                        return(true);
                    }
                }
            }

            foreach (SudokuMove move in this)
            {
                if (move.Pattern == pattern && move.Rows.Contains(row) && move.Columns.Contains(column) && move.Numbers.Contains(number))
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #3
0
        public void TestConstructor()
        {
            const SudokuPattern BAD_PATTERN  = SudokuPattern.None;
            const SudokuPattern GOOD_PATTERN = SudokuPattern.ClaimingCandidate;
            const int           BAD_NUMBER   = -1;
            const int           GOOD_NUMBER  = SudokuPuzzle.MaximumSupportedSize;

            int[] badArray   = new int[] { -1 };
            int[] emptyArray = new int[0];
            int[] goodArray  = new int[] { 1, GOOD_NUMBER };
            Assert.ThrowsException <ArgumentNullException>(() => new SudokuMove(null, null, null, GOOD_PATTERN, null));
            Assert.ThrowsException <ArgumentNullException>(() => new SudokuMove(goodArray, null, null, GOOD_PATTERN, null));
            Assert.ThrowsException <ArgumentNullException>(() => new SudokuMove(goodArray, goodArray, null, GOOD_PATTERN, null));
            Assert.ThrowsException <ArgumentNullException>(() => new SudokuMove(goodArray, goodArray, goodArray, GOOD_PATTERN, null));
            Assert.ThrowsException <ArgumentException>(() => new SudokuMove(badArray, badArray, badArray, BAD_PATTERN, badArray));
            Assert.ThrowsException <ArgumentException>(() => new SudokuMove(goodArray, badArray, badArray, BAD_PATTERN, badArray));
            Assert.ThrowsException <ArgumentException>(() => new SudokuMove(goodArray, goodArray, badArray, BAD_PATTERN, badArray));
            Assert.ThrowsException <ArgumentException>(() => new SudokuMove(goodArray, goodArray, goodArray, BAD_PATTERN, badArray));
            Assert.ThrowsException <ArgumentException>(() => new SudokuMove(emptyArray, emptyArray, emptyArray, BAD_PATTERN, emptyArray));
            Assert.ThrowsException <ArgumentException>(() => new SudokuMove(goodArray, emptyArray, emptyArray, BAD_PATTERN, emptyArray));
            Assert.ThrowsException <ArgumentException>(() => new SudokuMove(goodArray, goodArray, emptyArray, BAD_PATTERN, emptyArray));
            Assert.ThrowsException <ArgumentException>(() => new SudokuMove(goodArray, goodArray, goodArray, BAD_PATTERN, emptyArray));
            Assert.ThrowsException <ArgumentException>(() => new SudokuMove(goodArray, goodArray, new int[] { GOOD_NUMBER, 1, 1 }, GOOD_PATTERN, goodArray));
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => new SudokuMove(BAD_NUMBER, BAD_NUMBER, BAD_NUMBER, BAD_PATTERN, goodArray));
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => new SudokuMove(GOOD_NUMBER, BAD_NUMBER, BAD_NUMBER, BAD_PATTERN, goodArray));
            Assert.ThrowsException <ArgumentOutOfRangeException>(() => new SudokuMove(GOOD_NUMBER, GOOD_NUMBER, BAD_NUMBER, BAD_PATTERN, goodArray));
            Assert.ThrowsException <ArgumentException>(() => new SudokuMove(GOOD_NUMBER, GOOD_NUMBER, GOOD_NUMBER, BAD_PATTERN, goodArray));

            try
            {
                new SudokuMove(goodArray, goodArray, goodArray, GOOD_PATTERN, goodArray);
            }

            catch (Exception ex)
            {
                Assert.Fail($"Throw exception: {ex}");
            }
        }
コード例 #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SudokuMove"/> class.
        /// </summary>
        /// <param name="rows">An array containing the zero-based rows of the <see cref="SudokuPuzzle"/> affected by the <see cref="SudokuMove"/>.</param>
        /// <param name="columns">An array containing the zero-based columns of the <see cref="SudokuPuzzle"/> affected by the <see cref="SudokuMove"/>.</param>
        /// <param name="numbers">An array containing the numbers used in the <see cref="SudokuMove"/>.</param>
        /// <param name="pattern">The pattern used in the <see cref="SudokuMove"/>.</param>
        /// <param name="possible">An array containing the numbers that were possible instantly before the <see cref="SudokuMove"/> move was made.</param>
        /// <exception cref="ArgumentException">The lengths of <c><paramref name="rows"/></c>, <c><paramref name="columns"/></c>, <c><paramref name="numbers"/></c> or <c><paramref name="possible"/></c> are equal to 0, or any of their items are less than 0 - or - the length of <c><paramref name="numbers"/></c> is greater than the length of <c><paramref name="possible"/></c> - or - <c><paramref name="pattern"/></c> is equal to <see cref="SudokuPattern.None"/></exception>
        /// <exception cref="ArgumentNullException"><c><paramref name="rows"/></c>, <c><paramref name="columns"/></c>, <c><paramref name="numbers"/></c> or <c><paramref name="possible"/></c> is <c>null</c>.</exception>
        public SudokuMove(int[] rows, int[] columns, int[] numbers, SudokuPattern pattern, int[] possible)
        {
            if (rows is null)
            {
                throw new ArgumentNullException(nameof(rows));
            }

            if (columns is null)
            {
                throw new ArgumentNullException(nameof(columns));
            }

            if (numbers is null)
            {
                throw new ArgumentNullException(nameof(numbers));
            }

            if (pattern == SudokuPattern.None)
            {
                throw new ArgumentException(nameof(pattern));
            }

            if (possible is null)
            {
                throw new ArgumentNullException(nameof(possible));
            }

            if (rows.Length == 0)
            {
                throw new ArgumentException(nameof(rows));
            }

            if (columns.Length == 0)
            {
                throw new ArgumentException(nameof(columns));
            }

            if (numbers.Length == 0)
            {
                throw new ArgumentException(nameof(numbers));
            }

            if (possible.Length == 0)
            {
                throw new ArgumentException(nameof(possible));
            }

            for (int i = 0; i < rows.Length; i++)
            {
                if (rows[i] < 0)
                {
                    throw new ArgumentException(nameof(rows));
                }
            }

            for (int i = 0; i < columns.Length; i++)
            {
                if (columns[i] < 0)
                {
                    throw new ArgumentException(nameof(columns));
                }
            }

            for (int i = 0; i < possible.Length; i++)
            {
                if (possible[i] < 0)
                {
                    throw new ArgumentException(nameof(possible));
                }
            }

            if (possible.Length < numbers.Length)
            {
                throw new ArgumentException(nameof(numbers));
            }

            this._Columns  = columns;
            this._Numbers  = numbers;
            this._Possible = possible;
            this._Rows     = rows;
            this.Pattern   = pattern;
        }