Exemple #1
0
        public IActionResult Update([FromBody] byte?[][] jsonBoard)
        {
            SetBlockSize();

            var board   = GetTwoDimentional(jsonBoard);
            var isValid = _sudokuValidator.Validate(board);

            ViewData["IsBoardInvalid"] = !isValid;
            return(Ok(isValid));
        }
        /// <summary>
        /// Валидация и вставка цифры в клетку
        /// </summary>
        /// <param name="cell"></param>
        /// <returns></returns>
        private int?[,] InsertCell(Cell cell)
        {
            int?[,] sudocu = GamesSingleton.Instance.Matrix.Clone() as int?[, ];

            sudokuValidator.Validate(sudocu, cell);

            sudocu[cell.Row, cell.Column] = cell.Value;

            sudokuSolver.Solve(sudocu);

            return(sudocu);
        }
Exemple #3
0
        public IList <byte?[, ]> Solve(byte?[,] board, bool solveAll = false)
        {
            var clonnedBoard   = (byte?[, ])board.Clone();
            var result         = new List <byte?[, ]>();
            var combinedIndex  = 0;
            var isReversed     = false;
            var completed      = false;
            var horizontalSize = board.GetUpperBound(0) + 1;
            var verticalSize   = board.GetUpperBound(1) + 1;
            var cellCount      = horizontalSize * verticalSize;

            while (!completed)
            {
                if (combinedIndex < 0)
                {
                    completed = true;
                    continue;
                }

                if (combinedIndex >= cellCount)
                {
                    result.Add((byte?[, ])clonnedBoard.Clone());

                    if (solveAll)
                    {
                        combinedIndex = cellCount - 1;
                        isReversed    = true;
                    }
                    else
                    {
                        completed = true;
                        continue;
                    }
                }

                var i = combinedIndex / horizontalSize;
                var j = combinedIndex % verticalSize;

                if (board[i, j].HasValue)
                {
                    if (isReversed)
                    {
                        combinedIndex--;
                    }
                    else
                    {
                        combinedIndex++;
                    }

                    continue;
                }

                clonnedBoard[i, j] = clonnedBoard[i, j] ?? 0;
                clonnedBoard[i, j]++;

                while (!_sudokuValidator.Validate(clonnedBoard, i, j) && clonnedBoard[i, j] < 10)
                {
                    clonnedBoard[i, j]++;
                }

                if (clonnedBoard[i, j] == 10)
                {
                    clonnedBoard[i, j] = null;
                    combinedIndex--;
                    isReversed = true;
                }
                else
                {
                    combinedIndex++;
                    isReversed = false;
                }
            }
            return(result);
        }