Beispiel #1
0
        public bool TrySolve(ISudokuBoard source, out ISudokuBoard destination)
        {
            var sudokuBoard = source.Clone();

            destination = Solve(sudokuBoard);
            return(destination != null);
        }
Beispiel #2
0
        private static ISudokuBoard Solve(ISudokuBoard sudokuBoard)
        {
            var position = GetUnassignedPosition(sudokuBoard);

            if (position == null)
            {
                return(sudokuBoard);
            }

            var(row, column) = position.Value;

            for (var choice = 1; choice <= 9; choice++)
            {
                if (!IsValidChoiceForPosition(sudokuBoard, (row, column), choice))
                {
                    continue;
                }

                sudokuBoard[row, column] = choice;

                var result = Solve(sudokuBoard);
                if (result != null)
                {
                    return(result);
                }

                sudokuBoard[row, column] = 0;
            }

            return(null);
        }
        private void InitializeRegion(ISudokuPuzzle puzzle, ISudokuPosition position)
        {
            ISudokuPosition currentPosition = new SudokuPosition();
            board = new SimpleSudokuBoard(puzzle.Size);

            for (int i = 0; i < puzzle.Size; i++)
            {
                for (int j = 0; j < puzzle.Size; j++)
                {
                    currentPosition.RowNumber = puzzle.Size * position.RowNumber + i;
                    currentPosition.ColumnNumber = puzzle.Size * position.ColumnNumber + j;
                    board.SetValue(i, j, puzzle.GetValue(currentPosition));
                }
            }
        }
Beispiel #4
0
        private static (int, int)? GetUnassignedPosition(ISudokuBoard sudokuBoard)
        {
            for (var row = 0; row < sudokuBoard.Size; row++)
            {
                for (var column = 0; column < sudokuBoard.Size; column++)
                {
                    if (sudokuBoard[row, column] == 0)
                    {
                        return(row, column);
                    }
                }
            }

            return(null);
        }
 private void InitializePuzzle()
 {
     //board = new int[SquareSize, SquareSize];
     board = new SimpleSudokuBoard(SquareSize);
 }
Beispiel #6
0
 private static bool IsValidChoiceForPosition(ISudokuBoard sudokuBoard, (int, int) position, int choice)