示例#1
0
        static void Main(string[] args)
        {
            {
                SudokuBlock      sudokuBlock      = new SudokuBlock();
                SudokuState      sudokuState      = new SudokuState();
                SudokuSolver     sudokuSolver     = new SudokuSolver(sudokuState, sudokuBlock);
                SodokuFileReader sodokuFileReader = new SodokuFileReader();
                SudokuASCII      sudokuASCII      = new SudokuASCII();

                string file = "Sudoku.txt";

                var sudokuBoard = sodokuFileReader.ReadFile(file);
                sudokuASCII.DisplayBoard("Unsolved Board", sudokuBoard);

                bool SudokuSolved = sudokuSolver.IsSolved(sudokuBoard);
                sudokuASCII.DisplayBoard("Solved Board", sudokuBoard);
            }
        }
示例#2
0
        /// <summary>
        /// Solve the grid
        /// </summary>
        public void Solve()
        {
            Lock();
            int[,] sodukuGrid = new int[9, 9];
            for (int i = 0; i < 9; ++i)
            {
                for (int j = 0; j < 9; ++j)
                {
                    sodukuGrid[i, j] = this[i, j];
                }
            }
            SudokuSolver solver = new SudokuSolver(sodukuGrid);

            solutions = solver.Solve();

            //Grid is invalid
            if (solutions.Count != 1)
            {
                for (int i = 0; i < 9; ++i)
                {
                    for (int j = 0; j < 9; ++j)
                    {
                        if (this[i, j] == 0)
                        {
                            this[i, j] = -1;
                        }
                    }
                }


                return;
            }

            for (int i = 0; i < 9; ++i)
            {
                for (int j = 0; j < 9; ++j)
                {
                    this[i, j] = solutions[0][i, j];
                }
            }
        }
示例#3
0
        /// <summary>
        /// Solve the grid
        /// </summary>
        /// <returns>List of the solutions</returns>
        public List <int[, ]> Solve()
        {
            List <int[, ]> gridRules = new List <int[, ]>();

            while (IsValid && !IsFilled)
            {
                // Detect the box to be filled
                IEnumerable <BoxInformation> boxesToBeFilled = DetectBoxesToBeFilled().Values;

                // If no boxes, then make hypothesis
                if (boxesToBeFilled.Count() == 0)
                {
                    IEnumerable <BoxInformation> hypothesis = GetHypothesis();
                    for (int n = 1; n < hypothesis.Count(); ++n)

                    {
                        var rule = new SudokuSolver(grid);
                        rule[hypothesis.ElementAt(n).Row, hypothesis.ElementAt(n).Col] = hypothesis.ElementAt(n).Figure;
                        gridRules.AddRange(rule.Solve());
                    }
                    this[hypothesis.ElementAt(0).Row, hypothesis.ElementAt(0).Col] = hypothesis.ElementAt(0).Figure;
                    continue;
                }
                // Fill the boxes
                Parallel.ForEach(boxesToBeFilled, info => {
                    this[info.Row, info.Col] = info.Figure;
                });
            }

            if (IsValid)
            {
                gridRules.Add(grid);
            }

            return(gridRules);
        }