From f84cccc1b510ade19bc445249fef6dadc47a82af Mon Sep 17 00:00:00 2001 From: Imbus Date: Tue, 5 Dec 2023 16:13:57 +0100 Subject: [PATCH 1/2] Better comments --- app/src/main/java/sudoku/Solver.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/src/main/java/sudoku/Solver.java b/app/src/main/java/sudoku/Solver.java index 1fea9c7..0d5ca47 100644 --- a/app/src/main/java/sudoku/Solver.java +++ b/app/src/main/java/sudoku/Solver.java @@ -38,6 +38,7 @@ public class Solver implements SudokuSolver { return false; } + // If row is 9, drop down to the next column, if column is 9, we are done if (row == 9) { row = 0; if (++col == 9) { @@ -45,13 +46,17 @@ public class Solver implements SudokuSolver { } } + // If we have a "number" in the current cell + // recursively call solve() on the next cell if (board[row][col] != 0) { return solve(row + 1, col); } + // Check for legal values in the current cell for (int val = 1; val <= 9; ++val) { if (legal(row, col, val)) { board[row][col] = val; + // When we find a legal value, recursively call solve() on the next cell if (solve(row + 1, col)) { return true; } From 56ab2ca98fe78a0c16f935853707590af0243768 Mon Sep 17 00:00:00 2001 From: Imbus Date: Tue, 5 Dec 2023 16:51:55 +0100 Subject: [PATCH 2/2] Better docstrings --- app/src/main/java/sudoku/Solver.java | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/sudoku/Solver.java b/app/src/main/java/sudoku/Solver.java index 0d5ca47..c171c8a 100644 --- a/app/src/main/java/sudoku/Solver.java +++ b/app/src/main/java/sudoku/Solver.java @@ -7,10 +7,16 @@ public class Solver implements SudokuSolver { board = new int[9][9]; } + /** + * {@inheritDoc} + */ public void setBoard(int[][] board) { this.board = board; } + /** + * {@inheritDoc} + */ public int[][] getBoard() { return board; } @@ -22,6 +28,9 @@ public class Solver implements SudokuSolver { board = new int[9][9]; } + /** + * {@inheritDoc} + */ public boolean solve() { return solve(0, 0); } @@ -96,12 +105,7 @@ public class Solver implements SudokuSolver { } /** - * Checks if val is legal in the given row, column, and 3x3 box - * - * @param row row to check - * @param col column to check - * @param val value to check - * @return true if val is legal + * {@inheritDoc} */ public boolean legal(int row, int col, int val) { if (row < 0 || row >= 9 || col < 0 || col >= 9 || val < 1 || val > 9) { @@ -138,6 +142,11 @@ public class Solver implements SudokuSolver { return true; } + /** + * Checks if the board is solved + * + * @return true if solved + */ public boolean isSolved() { return isSolved(0, 0); } @@ -147,7 +156,7 @@ public class Solver implements SudokuSolver { * * @param row * @param col - * @return + * @return true if solved */ private boolean isSolved(int row, int col) { // If we are at the 9th row and 0th column (the last cell), we are done