Exemple #1
0
        private static List<Sudoku> LoadSudokuData()
        {
            List<Sudoku> games = new List<Sudoku>();
            if (useExample)
            {
                Sudoku game = new Sudoku("003020600900305001001806400008102900700000008006708200002609500800203009005010300");
                games.Add(game);
            }
            else
            {
                string fileText = System.IO.File.ReadAllText("..\\..\\data096.txt");
                string[] lines = fileText.Split('\n');
                string gameData = "";
                int countlines = 0;
                foreach (string line in lines)
                {
                    if (!line.Contains("Grid"))
                    {
                        gameData += line.Substring(0, 9); // strip off any trailing characters!
                        countlines++;
                    }
                    if (countlines == 9)
                    {
                        Sudoku game = new Sudoku(gameData);
                        games.Add(game);
                        countlines = 0;
                        gameData = "";
                    }
                }

            }
            return games;
        }
Exemple #2
0
        public bool SolveSudoku(int thisProblemNumber, bool debug)
        {
            PrintBig(originalData);

            // we'll start at the beginning and
            // use solvedData as our work area.
            for (int x = 0; x < 9; x++)
            {
                for (int y = 0; y < 9; y++)
                {
                    solvedData[x, y] = new SudokuCell(originalData[x, y].Number, x, y);
                }
            }
            int round = 1;
            int thisRoundNumLeft = -1;
            int lastRoundNumLeft = -1;

            do
            {

                //Check Horizontals
                for (int row = 0; row < 9; row++)
                {
                    // get list of cells for this column
                    List<SudokuCell> cells = GetCellGroupingFor(solvedData[0, row], CellGroupingType.Horizontal);
                    // apply logic to this independant grouping
                    NarrowDownIndependantCellGrouping(cells, CellGroupingType.Horizontal);
                }

                //Check Verticals
                for (int col = 0; col < 9; col++)
                {
                    // get list of cells for this column
                    List<SudokuCell> cells = GetCellGroupingFor(solvedData[col, 0], CellGroupingType.Vertical);
                    // apply logic to this independant grouping
                    NarrowDownIndependantCellGrouping(cells, CellGroupingType.Vertical);
                }

                // Check Square Groups
                for (int gridRow = 0; gridRow < 3; gridRow++)
                {
                    for (int gridCol = 0; gridCol < 3; gridCol++)
                    {
                        // create a list of cells for this group
                        List<SudokuCell> cells = GetCellGroupingFor(solvedData[gridCol * 3, gridRow * 3], CellGroupingType.Square);
                        // apply logic to this independant grouping
                        NarrowDownIndependantCellGrouping(cells, CellGroupingType.Square);
                    }
                }

                // **** NOTE ****
                // SEE: http://www.sudokuessentials.com/sudoku_tips.html
                // **** NOTE ****

                if (debug)
                {
                    Console.WriteLine("After Round {0}:", round);
                    PrintBig(solvedData);
                }

                thisRoundNumLeft = CountNumberLeft(solvedData);

                round++;
                if (thisRoundNumLeft == lastRoundNumLeft)
                {
                    Console.WriteLine("!!!!!!!!!!!!!!!!! Not making any headway on {0} !!!!!!!!", thisProblemNumber);
                    if (debug) PrintBig(solvedData);
                    if (CheckValid())
                    {
                        bool guessedCorrect = false;
                        // now make guesses and solve each guess
                        for (int row = 0; row < 10; row++)
                        {
                            if (guessedCorrect) break;
                            for (int col = 0; col < 10; col++)
                            {
                                if (guessedCorrect) break;
                                if (solvedData[col, row].Number == 0)
                                {
                                    for (int i = 1; i < 10; i++)
                                    {
                                        string theseUsed = solvedData[col, row].Possibilities;
                                        if (theseUsed.Substring(i, 1) != "-")
                                        {
                                            Sudoku newPuzzle = new Sudoku(solvedData);
                                            newPuzzle.originalData[col, row].Number = i;
                                            guessedCorrect = newPuzzle.SolveSudoku(thisProblemNumber, debug);
                                            if (guessedCorrect)
                                            {
                                                solvedData = newPuzzle.solvedData;
                                            }

                                        }
                                        if (guessedCorrect) break;
                                    }
                                }
                            }
                        }
                    }
                    break;
                }
                lastRoundNumLeft = thisRoundNumLeft;
            } while (!IsCompletelySolved);

            if (!debug) PrintBig(solvedData);
            return CheckValid();
        }