public void TestSolvingSpecificPuzzles() { string badPuzzlePath = @"Input\Puzzle-25x25-0902.txt"; string multipleSolutionsPuzzlePath = @"Input\Puzzle-16x16-0902.txt"; string goodPuzzlePath = @"Input\Puzzle-16x16-0301.txt"; SudokuFiler filer = null; try { filer = new SudokuFiler(badPuzzlePath); } catch (Exception) { } Assert.IsTrue(filer == null); filer = new SudokuFiler(multipleSolutionsPuzzlePath); SudokuPuzzle puzzle = filer.CreatePuzzle(); SudokuSolver.SudokuSolver.SudokuSolver solver = new SudokuSolver.SudokuSolver.SudokuSolver(); Assert.IsTrue(solver.Solve(puzzle).Count == 2); filer = new SudokuFiler(goodPuzzlePath); puzzle = filer.CreatePuzzle(); solver = new SudokuSolver.SudokuSolver.SudokuSolver(); Assert.IsTrue(solver.Solve(puzzle).Count == 1); }
/// <summary> /// Output the solutions to the given <see cref="SudokuPuzzle"/> to the given file path /// </summary> /// <param name="filepath">The file to which the solutions will be written</param> /// <param name="filer">Used to convert <see cref="SudokuPuzzle"/>s into lists of strings</param> /// <param name="puzzleToSolve">The <see cref="SudokuPuzzle"/> being solved</param> /// <param name="solutions">A list of the solutions to the <see cref="SudokuPuzzle"/> being solved</param> private static void OutputSolutions(string filepath, SudokuFiler filer, SudokuPuzzle puzzleToSolve, List <SudokuPuzzle> solutions) { if (solutions.Count == 0) { List <string> unsolvablePuzzle = filer.ConvertToStrings(puzzleToSolve); unsolvablePuzzle.Add(string.Empty); unsolvablePuzzle.Add("Unsolvable Puzzle"); WriteToFile(filepath, unsolvablePuzzle); } else if (solutions.Count == 1) { List <string> puzzleOutput = filer.ConvertToStrings(puzzleToSolve); List <string> solution = filer.ConvertToStrings(solutions[0]); puzzleOutput.Add(string.Empty); puzzleOutput.Add("Solved"); puzzleOutput.AddRange(solution); WriteToFile(filepath, puzzleOutput); } else { List <string> puzzleOutput = filer.ConvertToStrings(puzzleToSolve); puzzleOutput.Add(string.Empty); puzzleOutput.Add("Multiple Solutions"); foreach (SudokuPuzzle solution in solutions) { puzzleOutput.Add(string.Empty); puzzleOutput.AddRange(filer.ConvertToStrings(solution)); } WriteToFile(filepath, puzzleOutput); } }
/// <summary> /// The main entry point of the program /// </summary> /// <param name="args">An array of command line arguments</param> public static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Error: Missing file paths"); } else if (!File.Exists(args[0])) { Console.WriteLine($"Error: Could not find file {args[0]}"); } else { try { SudokuFiler filer = new SudokuFiler(args[0]); SudokuPuzzle puzzleToSolve = filer.CreatePuzzle(); SudokuSolver.SudokuSolver solver = new SudokuSolver.SudokuSolver(); List <SudokuPuzzle> solutions = solver.Solve(puzzleToSolve); OutputSolutions(args[1], filer, puzzleToSolve, solutions); } catch (Exception) { string[] sudokuFile = File.ReadAllLines(args[0]); List <string> lines = new List <string>(); foreach (string line in sudokuFile) { lines.Add(line); } lines.Add(string.Empty); lines.Add("Bad Puzzle"); WriteToFile(args[1], lines); } } }
public void TestSudokuFiler() { string inputFilePath = "TestPuzzleInput.txt"; SudokuFiler filer = new SudokuFiler(inputFilePath); SudokuPuzzle puzzle = filer.CreatePuzzle(); List <string> stringEquivalent = filer.ConvertToStrings(); string[] fileData = File.ReadAllLines(inputFilePath); Assert.IsTrue(stringEquivalent.Count == fileData.Length); for (int i = 0; i < fileData.Length; ++i) { Assert.IsTrue(stringEquivalent[i] == fileData[i]); } stringEquivalent = filer.ConvertToStrings(puzzle); Assert.IsTrue(stringEquivalent.Count == fileData.Length); for (int i = 0; i < fileData.Length; ++i) { Assert.IsTrue(stringEquivalent[i] == fileData[i]); } bool exceptionWasThrown = false; // Try a bad file path try { SudokuFiler badFiler = new SudokuFiler("notafilepath.txt"); } catch (Exception) { exceptionWasThrown = true; } Assert.IsTrue(exceptionWasThrown); exceptionWasThrown = false; File.Create("thisisanemptyfile.txt"); try { SudokuFiler badFiler = new SudokuFiler("thisisanemptyfile.txt"); } catch (Exception) { exceptionWasThrown = true; } Assert.IsTrue(exceptionWasThrown); }
public void TestSudokuSolver() { string[] files = Directory.GetFiles(Directory.GetCurrentDirectory() + @"\Input"); foreach (string file in files) { try { SudokuFiler filer = new SudokuFiler(file); SudokuSolver.SudokuSolver.SudokuSolver solver = new SudokuSolver.SudokuSolver.SudokuSolver(); List <SudokuPuzzle> puzzles = solver.Solve(filer.CreatePuzzle()); Assert.IsTrue(puzzles.Count == 1); } catch (Exception e) { } } }
public void TestClone() { string inputFilePath = "TestPuzzleInput.txt"; SudokuFiler filer = new SudokuFiler(inputFilePath); SudokuPuzzle puzzle = filer.CreatePuzzle(); for (int i = 0; i < puzzle.Dimension * puzzle.Dimension; ++i) { if (puzzle.Rows[0][i].Count == 1) { int allowedValue = puzzle.Rows[0][i].AllowedValue; if (allowedValue != 1) { allowedValue = 1; } else { ++allowedValue; } puzzle.Rows[0][i].AddAllowedValue(allowedValue); break; } } SudokuPuzzle clonedPuzzle = puzzle.Clone(); for (int i = 0; i < puzzle.Dimension * puzzle.Dimension; ++i) { for (int k = 0; k < puzzle.Dimension * puzzle.Dimension; ++k) { Assert.IsTrue(puzzle.Rows[i][k].Count == clonedPuzzle.Rows[i][k].Count); Assert.IsTrue(puzzle.Columns[i][k].Count == clonedPuzzle.Columns[i][k].Count); Assert.IsTrue(puzzle.Blocks[i][k].Count == clonedPuzzle.Blocks[i][k].Count); foreach (int allowedValue in puzzle.Rows[i][k].AllowedValues.Keys) { Assert.IsTrue(clonedPuzzle.Rows[i][k].AllowedValues.ContainsKey(allowedValue)); } } } }