Skip to content

Commit 67cce73

Browse files
committed
Update source code; Update tests
1 parent c2138d1 commit 67cce73

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/sudoku_suite.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ class Grid {
7070
* relating to the input which we are reusing. */
7171
std::ifstream file;
7272
file.open(filename, std::ios::in);
73+
if (file.fail()) throw std::invalid_argument(
74+
"The file couldn't be opened. Please check if the file exists.");
7375

7476
std::array<std::array<int, 9>, 9> grid;
7577

@@ -211,8 +213,10 @@ std::ostream& operator<< (std::ostream& out, Grid grid) {
211213
Coord get_next_cell_coord(Coord coord) {
212214
/* Function which returns the next successive coordinate for a
213215
* Sudoku grid, given a current coordinate. */
214-
if (coord.second == GRID_LEN-1 && coord.first == GRID_LEN-1) return coord;
215-
else if (coord.second == GRID_LEN-1) return std::make_pair(coord.first+1, 0);
216+
if (coord.second == GRID_LEN-1 && coord.first == GRID_LEN-1)
217+
return coord;
218+
else if (coord.second == GRID_LEN-1)
219+
return std::make_pair(coord.first+1, 0);
216220
return std::make_pair(coord.first, coord.second + 1);
217221
}
218222

@@ -298,17 +302,19 @@ void remove_values_from_solution(Grid *grid, int values_to_remove) {
298302
}
299303

300304
/*-----------------*/
301-
/* SUITE FUNCTIONS */
305+
/* SOLVE FUNCTIONS */
302306
/*-----------------*/
303307

304-
bool solve(
308+
bool _solve(
305309
Grid *grid,
306310
Coord cell_coord = std::make_pair(0, 0)
307311
) {
308312
auto next_coord = get_next_cell_coord(cell_coord);
309313

310-
if (grid->coord_was_pre_filled(cell_coord))
311-
return solve(grid, next_coord);
314+
if (grid->coord_was_pre_filled(cell_coord)) {
315+
if (cell_coord == std::make_pair(8, 8)) return true;
316+
return _solve(grid, next_coord);
317+
}
312318

313319
auto values = get_possible_values_for_cell_at_coord(*grid, cell_coord);
314320
if (values.size() == 0) return false;
@@ -317,7 +323,7 @@ bool solve(
317323
grid->update(cell_coord, value);
318324
if (cell_coord == std::make_pair(8, 8)) return true; // Solved!
319325

320-
bool next_cell_is_solved = solve(grid, next_coord);
326+
bool next_cell_is_solved = _solve(grid, next_coord);
321327
if (next_cell_is_solved) return true;
322328

323329
/* If this value didn't work, we need to clear the grid of all the
@@ -331,6 +337,14 @@ bool solve(
331337
return false;
332338
}
333339

340+
/*-----------------*/
341+
/* SUITE FUNCTIONS */
342+
/*-----------------*/
343+
344+
void solve(Grid *grid) {
345+
_solve(grid);
346+
}
347+
334348
bool is_valid_solution(
335349
const Grid& grid,
336350
Coord curr_coord = std::make_pair(0, 0)

tests/test_sudoku_solver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void test_puzzle_is_solved_correctly() {
237237
{{ 0, 0, 0, 0, 0, 0, 0, 3, 6 }},
238238
{{ 9, 6, 0, 0, 0, 0, 3, 0, 8 }},
239239
{{ 7, 0, 0, 6, 8, 0, 0, 0, 0 }},
240-
{{ 0, 2, 8, 0, 0, 0, 0, 0, 0 }}
240+
{{ 0, 2, 8, 0, 0, 0, 0, 0, 7 }}
241241
}});
242242

243243
sudoku::solve(&unsolved_puzzle_grid);

0 commit comments

Comments
 (0)