Example #1
0
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            //readSudoku(@"C:\Users\obaysan\Desktop\sudoku.txt");

            DateTime beginning = DateTime.Now;

            SudokuGenerator gen = new SudokuGenerator();
            Cell[,] sudoku = gen.generateSudoku(DifficultyLevel.levels.veteran);
            SudokuSolver ss = new SudokuSolver();
            ss.initialize(sudoku);

            Console.WriteLine("INITIAL STATE");
            ss.printSudoku();
            ss.solveIt();
            Console.WriteLine("-------------------------");
            Console.WriteLine("SOLVED");
            ss.printSudoku();

            DateTime end = DateTime.Now;
            long elapsedTicks = end.Ticks - beginning.Ticks;
            TimeSpan elapsedSpan = new TimeSpan(elapsedTicks);
            Console.WriteLine("Elapsed Time: " + elapsedSpan.Milliseconds + "ms");

            Console.ReadKey();
        }
Example #2
0
        static void readSudoku(string filePath)
        {
            TextReader reader = new StreamReader(filePath);

            string puzzle = reader.ReadToEnd();
            puzzle = puzzle.Trim('\r', ' ');
            string[] lines = puzzle.Split('\n');

            SudokuSolver ss = new SudokuSolver();

            ss.initialize(lines);
            if (ss.solveIt())
                ss.printSudoku();
        }
Example #3
0
        /*
         * Difficuly Level
         */
        public Cell[,] generateSudoku(DifficultyLevel.levels level)
        {
            int willOpen = 0;

            switch (level)
            {
                case DifficultyLevel.levels.easy:
                    willOpen = 30;
                    break;
                case DifficultyLevel.levels.normal:
                    willOpen = 25;
                    break;
                case DifficultyLevel.levels.harder:
                    willOpen = 20;
                    break;
                case DifficultyLevel.levels.veteran:
                    willOpen = 15;
                    break;
            }

            for (int i = 0; i < 9; i++)
                for (int j = 0; j < 9; j++)
                {
                    sudoku[i, j] = new Cell();
                    tmp[i, j] = new Cell();
                }

            SudokuSolver ss = new SudokuSolver();
            ss.initialize(sudoku);
            Random rand = new Random();

            while (true)
            {

                for (int i = 0; i < willOpen; )
                {
                    int row = rand.Next(0, 8);
                    int col = rand.Next(0, 8);

                    if (sudoku[row, col].value == 0)
                    {
                        int value = rand.Next(1, 9);
                        while (!isValid(row, col, value))
                            value = rand.Next(1, 9);

                        Cell cell = new Cell();
                        cell.row = row;
                        cell.column = col;
                        cell.value = value;
                        points.Add(cell);
                        sudoku[row, col].value = value;
                        i++;
                    }
                }

                if (ss.solveIt())
                    break;
                else
                {
                    clearSudoku();
                    ss.initialize(sudoku);
                }

            }

            maskIt();
            return sudoku;
        }