Compare commits
No commits in common. "175545d3d593b9d0095a4393632c3a22a176ec8d" and "71c43b35c3e5c8474a100bedc566d4b305d9b391" have entirely different histories.
175545d3d5
...
71c43b35c3
3 changed files with 12 additions and 26 deletions
|
@ -12,11 +12,7 @@ public class Solver implements SudokuSolver {
|
||||||
|
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public void setBoard(int[][] board) throws IllegalArgumentException, NullPointerException {
|
public void setBoard(int[][] board) {
|
||||||
if (board == null)
|
|
||||||
throw new NullPointerException("Board cannot be null");
|
|
||||||
if (board.length != 9 || board[0].length != 9)
|
|
||||||
throw new IllegalArgumentException("Board must be 9x9");
|
|
||||||
this.board = board;
|
this.board = board;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +28,7 @@ public class Solver implements SudokuSolver {
|
||||||
board = new int[9][9];
|
board = new int[9][9];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* {@inheritDoc} */
|
/*{@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public boolean solve() {
|
public boolean solve() {
|
||||||
return solve(0, 0);
|
return solve(0, 0);
|
||||||
|
@ -111,7 +107,7 @@ public class Solver implements SudokuSolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recursively call randomizeBoard() until we get a solvable board
|
// Recursively call randomizeBoard() until we get a solvable board
|
||||||
// This is expensive, and there should be a better way to do this
|
// This is expensive, and there should be some voodoo magic that computes this in n^2 time
|
||||||
if (!isSolvable()) {
|
if (!isSolvable()) {
|
||||||
randomizeBoard(difficulty);
|
randomizeBoard(difficulty);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package sudoku;
|
package sudoku;
|
||||||
|
|
||||||
/** SudokuSolver is an interface for implementing Sudoku solvers */
|
|
||||||
public interface SudokuSolver {
|
public interface SudokuSolver {
|
||||||
/**
|
/**
|
||||||
* Set sudoku board, numbers 1-9 are fixed values, 0 is unsolved.
|
* Set sudoku board, numbers 1-9 are fixed values, 0 is unsolved.
|
||||||
|
@ -8,12 +7,10 @@ public interface SudokuSolver {
|
||||||
* @param board a board to copy values from
|
* @param board a board to copy values from
|
||||||
* @throws IllegalArgumentException if board is invalid, e.g. not 9x9
|
* @throws IllegalArgumentException if board is invalid, e.g. not 9x9
|
||||||
*/
|
*/
|
||||||
void setBoard(int[][] board) throws IllegalArgumentException, NullPointerException;
|
void setBoard(int[][] board) throws IllegalArgumentException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a copy of the sudoku board
|
* Get a copy of the sudoku board
|
||||||
*
|
|
||||||
* @return a <b>copy</b> of the sudoku board
|
|
||||||
*/
|
*/
|
||||||
int[][] getBoard();
|
int[][] getBoard();
|
||||||
|
|
||||||
|
@ -27,9 +24,9 @@ public interface SudokuSolver {
|
||||||
/**
|
/**
|
||||||
* Check if digit is legal on the current board
|
* Check if digit is legal on the current board
|
||||||
*
|
*
|
||||||
* @param row row
|
* @param row
|
||||||
* @param col column
|
* @param col
|
||||||
* @param nbr number to check
|
* @param nbr
|
||||||
* @return true if legal
|
* @return true if legal
|
||||||
*/
|
*/
|
||||||
boolean isLegal(int row, int col, int nbr);
|
boolean isLegal(int row, int col, int nbr);
|
||||||
|
@ -37,8 +34,8 @@ public interface SudokuSolver {
|
||||||
/**
|
/**
|
||||||
* Get number on board
|
* Get number on board
|
||||||
*
|
*
|
||||||
* @param row row
|
* @param row
|
||||||
* @param col column
|
* @param col
|
||||||
* @return number on board
|
* @return number on board
|
||||||
*/
|
*/
|
||||||
int get(int row, int col);
|
int get(int row, int col);
|
||||||
|
@ -46,9 +43,9 @@ public interface SudokuSolver {
|
||||||
/**
|
/**
|
||||||
* Set number on board, numbers 1-9 are fixed values, 0 is unsolved.
|
* Set number on board, numbers 1-9 are fixed values, 0 is unsolved.
|
||||||
*
|
*
|
||||||
* @param row row
|
* @param row
|
||||||
* @param col column
|
* @param col
|
||||||
* @param nbr number to set
|
* @param nbr
|
||||||
*/
|
*/
|
||||||
void set(int row, int col, int nbr);
|
void set(int row, int col, int nbr);
|
||||||
|
|
||||||
|
|
|
@ -106,11 +106,4 @@ class SolverTest {
|
||||||
solver.set(7, 1, 8);
|
solver.set(7, 1, 8);
|
||||||
assertFalse(solver.solve());
|
assertFalse(solver.solve());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void setBoardInvalidInputThrowsTest() {
|
|
||||||
Solver solver = new Solver();
|
|
||||||
assertThrows(NullPointerException.class, () -> solver.setBoard(null));
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> solver.setBoard(new int[8][8]));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue