示例#1
0
        public void testSolvePuzzle()
        {
            //TODO: More test cases!

            // A solvable puzzle
            int[,] puzzle = {{9,0,0,8,4,0,3,7,0},
                             {1,3,7,0,0,0,0,0,2},
                             {0,0,0,3,0,0,6,0,9},
                             {0,7,0,1,0,8,0,3,6},
                             {0,0,8,0,0,0,7,0,0},
                             {6,4,0,7,0,9,0,5,0},
                             {8,0,3,0,0,5,0,0,0},
                             {4,0,0,0,0,0,9,2,3},
                             {0,2,6,0,1,3,0,0,4}};

            int[,] answer = {{9,6,2,8,4,1,3,7,5},
                             {1,3,7,5,9,6,8,4,2},
                             {5,8,4,3,7,2,6,1,9},
                             {2,7,9,1,5,8,4,3,6},
                             {3,5,8,2,6,4,7,9,1},
                             {6,4,1,7,3,9,2,5,8},
                             {8,9,3,4,2,5,1,6,7},
                             {4,1,5,6,8,7,9,2,3},
                             {7,2,6,9,1,3,5,8,4}};

            //Sudoku. puzzle;
            SudokuSolver solver = new SudokuSolver(puzzle);
            bool result = solver.SolvePuzzle();
            Assert.IsTrue(result);
        }
示例#2
0
文件: Program.cs 项目: bobmatt/Sudoku
        static void Main(string[] args)
        {
            // Traditional 9x9 sudoku
            SudokuSolver solver = new SudokuSolver("C:\\SudokuData\\sudoku1.txt");
            var result = solver.SolvePuzzle();
            var validAnswer = solver.ValidatePuzzle("C:\\SudokuData\\sudoku1_answer.txt");
            MessageBox.Show("Puzzle solved:  " + result.ToString() + "\n" +
                "Result valid:  " + validAnswer.ToString() + "\n\n" +
                solver.PuzzleText());

            // 4x4 sudoku
            solver = new SudokuSolver("C:\\SudokuData\\sudoku2.txt");
            result = solver.SolvePuzzle();
            validAnswer = solver.ValidatePuzzle("C:\\SudokuData\\sudoku2_answer.txt");
            MessageBox.Show("Puzzle solved:  " + result.ToString() + "\n" +
                "Result valid:  " + validAnswer.ToString() + "\n\n" +
                solver.PuzzleText());

            // 16x16 sudoku
            solver = new SudokuSolver("C:\\SudokuData\\sudoku3.txt");
            result = solver.SolvePuzzle();
            validAnswer = solver.ValidatePuzzle("C:\\SudokuData\\sudoku3_answer.txt");
            MessageBox.Show("Puzzle solved:  " + result.ToString() + "\n" +
                "Result valid:  " + validAnswer.ToString() + "\n\n" +
                solver.PuzzleText());
        }
示例#3
0
        static void Main(string[] args)
        {
            string dir   = args.First();
            var    files = Directory.EnumerateFiles(dir).Where(file => file.EndsWith(".txt", true, CultureInfo.InvariantCulture));

            foreach (string file in files)
            {
                string outputString = "";
                try
                {
                    SudokuBoard  board  = new SudokuBoard(9);
                    SudokuParser parser = new SudokuParser();
                    parser.ParserText(File.ReadAllText(file), board);
                    SudokuSolver solver = new SudokuSolver();
                    solver.SolvePuzzle(board);
                    file.Substring(0, file.Length - 4);
                    outputString = board.ToString();
                }
                catch (Exception e) when((e is ParseException) || (e is SolveException))
                {
                    outputString = e.Message;
                }
                File.WriteAllText($"{file.Substring(0,file.Length-4)}.sln.txt", outputString);
            }
        }