public List<Cell> GetSolution() { int[,] arr = new int[9, 9]; foreach (Cell cell in _puzzle) { if (cell.X >= arr.GetLength(0) || cell.Y >= arr.GetLength(1) || cell.X < 0 || cell.Y < 0) { throw new IndexOutOfRangeException(); } int v = 0; if(int.TryParse(cell.Value, out v)) { if (v < 0 || v > 9) { throw new ArgumentOutOfRangeException(); } arr[cell.X, cell.Y] = v; } } PuzzleSolver solver = new PuzzleSolver(arr); return ArrayToCells(solver.GetSolution()); }
public void TEST_GetSolution_IsNullWhenNoValidSolution() { int[,] puzzle = new int[,] { { 5, 1, 3, 0, 0, 4, 0, 0, 0, }, { 0, 6, 0, 3, 0, 8, 1, 0, 0, }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, }, { 0, 0, 0, 7, 0, 6, 0, 0, 0, }, { 6, 5, 8, 0, 0, 0, 4, 3, 7, }, { 0, 0, 0, 1, 0, 3, 0, 0, 0, }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, }, { 0, 0, 6, 4, 0, 7, 0, 1, 0, }, { 0, 0, 0, 8, 0, 0, 7, 4, 6, } }; IPuzzleAnalyzer solver = new PuzzleSolver(puzzle); int[,] solution = solver.GetSolution(); Assert.IsNull(solution); }
public void TEST_GetSolution() { IPuzzleAnalyzer solver = new PuzzleSolver(GetPuzzle()); int[,] solution = solver.GetSolution(); Assert.IsNotNull(solution); Assert.AreNotEqual(GetPuzzle(), solution); Assert.Equals(solution, GetSolution()); }