Extending SudokuSolver interface and specifying @overrides in implementation

This commit is contained in:
Imbus 2023-12-10 11:53:17 +01:00
parent 383af5be58
commit 6366a5e0f2
2 changed files with 22 additions and 1 deletions

View file

@ -10,6 +10,7 @@ public class Solver implements SudokuSolver {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public void setBoard(int[][] board) { public void setBoard(int[][] board) {
this.board = board; this.board = board;
} }
@ -17,6 +18,7 @@ public class Solver implements SudokuSolver {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public int[][] getBoard() { public int[][] getBoard() {
return board; return board;
} }
@ -24,6 +26,7 @@ public class Solver implements SudokuSolver {
/** /**
* Resets the board to all zeros * Resets the board to all zeros
*/ */
@Override
public void clear() { public void clear() {
board = new int[9][9]; board = new int[9][9];
} }
@ -31,6 +34,7 @@ public class Solver implements SudokuSolver {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public boolean solve() { public boolean solve() {
return solve(0, 0); return solve(0, 0);
} }
@ -79,6 +83,7 @@ public class Solver implements SudokuSolver {
/** /**
* Randomizes the board. This guarantees a solvable board. * Randomizes the board. This guarantees a solvable board.
*/ */
@Override
public void randomizeBoard() { public void randomizeBoard() {
this.clear(); this.clear();
for (int i = 0; i < 9; ++i) { for (int i = 0; i < 9; ++i) {
@ -94,6 +99,7 @@ public class Solver implements SudokuSolver {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public void set(int row, int col, int val) { public void set(int row, int col, int val) {
if (row < 9 && col < 9) { if (row < 9 && col < 9) {
board[row][col] = val; board[row][col] = val;
@ -103,6 +109,7 @@ public class Solver implements SudokuSolver {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public int get(int row, int col) { public int get(int row, int col) {
if (row < 9 && col < 9) { if (row < 9 && col < 9) {
return board[row][col]; return board[row][col];
@ -113,6 +120,7 @@ public class Solver implements SudokuSolver {
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override
public boolean isLegal(int row, int col, int val) { public boolean isLegal(int row, int col, int val) {
if (row < 0 || row >= 9 || col < 0 || col >= 9 || val < 1 || val > 9) { if (row < 0 || row >= 9 || col < 0 || col >= 9 || val < 1 || val > 9) {
return false; return false;
@ -153,6 +161,7 @@ public class Solver implements SudokuSolver {
* *
* @return true if solved * @return true if solved
*/ */
@Override
public boolean isSolved() { public boolean isSolved() {
return isSolved(0, 0); return isSolved(0, 0);
} }

View file

@ -7,7 +7,7 @@ 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); void setBoard(int[][] board) throws IllegalArgumentException;
/** /**
* Get a copy of the sudoku board * Get a copy of the sudoku board
@ -49,6 +49,18 @@ public interface SudokuSolver {
*/ */
void set(int row, int col, int nbr); void set(int row, int col, int nbr);
/**
* Randomize the board. Guaranteed to be solvable.
*/
void randomizeBoard();
/**
* Check if the board is solved
*
* @return true if solved
*/
boolean isSolved();
/** /**
* Clear the board * Clear the board
*/ */