Compare commits
No commits in common. "26df774ca35d68ba7a937c0f77a8ddf59ce68b81" and "2acf69d466662b4cb0ca420d209f3eb82f557786" have entirely different histories.
26df774ca3
...
2acf69d466
7 changed files with 11 additions and 103 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -5,4 +5,3 @@
|
||||||
build
|
build
|
||||||
|
|
||||||
.vscode
|
.vscode
|
||||||
app/bin
|
|
4
Justfile
4
Justfile
|
@ -11,7 +11,7 @@ clean:
|
||||||
fd -td -I build -x rm -r
|
fd -td -I build -x rm -r
|
||||||
|
|
||||||
watch:
|
watch:
|
||||||
watchexec -r -c -w app/src "just test && just run"
|
watchexec -c -w app/src "just test && just run"
|
||||||
|
|
||||||
watchdoc:
|
watchdoc:
|
||||||
watchexec -r -c -w app/src "just doc"
|
watchexec -c -w app/src "just doc"
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
1 2 3 0 0 0 0 0 0
|
|
||||||
4 5 6 0 0 0 0 0 0
|
|
||||||
0 0 0 7 0 0 0 0 0
|
|
||||||
0 0 0 0 0 0 0 0 0
|
|
||||||
0 0 0 0 0 0 0 0 0
|
|
||||||
0 0 0 0 0 0 0 0 0
|
|
||||||
0 0 0 0 0 0 0 0 0
|
|
||||||
0 0 0 0 0 0 0 0 0
|
|
||||||
0 0 0 0 0 0 0 0 0
|
|
|
@ -1,9 +0,0 @@
|
||||||
0 0 8 0 0 9 0 6 2
|
|
||||||
0 0 0 0 0 0 0 0 5
|
|
||||||
1 0 2 5 0 0 0 0 0
|
|
||||||
0 0 0 2 1 0 0 9 0
|
|
||||||
0 5 0 0 0 0 6 0 0
|
|
||||||
6 0 0 0 0 0 0 2 8
|
|
||||||
4 1 0 6 0 8 0 0 0
|
|
||||||
8 6 0 0 3 0 1 0 0
|
|
||||||
0 0 0 0 0 0 4 0 0
|
|
|
@ -25,16 +25,10 @@ public class SudokuController {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
// Solve the board
|
// Solve the board
|
||||||
boolean solved = model.solve();
|
model.solve();
|
||||||
|
|
||||||
// Update the view
|
// Update the view
|
||||||
view.updateView(model.getBoard());
|
view.updateView(model.getBoard());
|
||||||
if (!solved) {
|
|
||||||
view.showErrorMessage("Could not solve the board.");
|
|
||||||
System.out.println("Could not solve the board.");
|
|
||||||
System.out.println(model.toString());
|
|
||||||
} else {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -70,13 +64,6 @@ public class SudokuController {
|
||||||
if (newBoard != null) {
|
if (newBoard != null) {
|
||||||
// Set the model
|
// Set the model
|
||||||
model.setBoard(newBoard);
|
model.setBoard(newBoard);
|
||||||
|
|
||||||
// Warn and clear if the board is not solvable
|
|
||||||
if(!model.isSolvable()) {
|
|
||||||
view.showErrorMessage("The board is not solvable.");
|
|
||||||
model.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the view
|
// Update the view
|
||||||
view.updateView(model.getBoard());
|
view.updateView(model.getBoard());
|
||||||
}
|
}
|
||||||
|
@ -101,33 +88,14 @@ public class SudokuController {
|
||||||
int row = view.getSelectedRow();
|
int row = view.getSelectedRow();
|
||||||
int col = view.getSelectedColumn();
|
int col = view.getSelectedColumn();
|
||||||
|
|
||||||
// The value to be inserted into the cell
|
int value = Integer.parseInt(view.getCellValue(row, col));
|
||||||
// Zero inicates an empty cell
|
// Check if the input is legal and update the model and view
|
||||||
// Negative values are invalid
|
if (model.isLegal(row, col, value)) {
|
||||||
int value = 0;
|
|
||||||
|
|
||||||
String cellValue = view.getCellValue(row, col);
|
|
||||||
|
|
||||||
// We need to check for null and empty string
|
|
||||||
if (cellValue == null || cellValue.equals("")) {
|
|
||||||
value = 0;
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
value = Integer.parseInt(cellValue);
|
|
||||||
} catch (NumberFormatException ex) {
|
|
||||||
value = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the input is invalid, value < 0 indicates parse error
|
|
||||||
if (value != 0 && !model.isLegal(row, col, value) || value < 0) {
|
|
||||||
value = 0;
|
|
||||||
view.showErrorMessage("Invalid input. Try again.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the model and view
|
|
||||||
model.set(row, col, value);
|
model.set(row, col, value);
|
||||||
view.updateView(model.getBoard());
|
view.updateView(model.getBoard());
|
||||||
|
} else {
|
||||||
|
view.showErrorMessage("Invalid input. Try again.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,12 +29,7 @@ public class Solver implements SudokuSolver {
|
||||||
/** Resets the board to all zeros */
|
/** Resets the board to all zeros */
|
||||||
@Override
|
@Override
|
||||||
public void clear() {
|
public void clear() {
|
||||||
for (int[] row : board) {
|
board = new int[9][9];
|
||||||
for (int i = 0; i < row.length; ++i) {
|
|
||||||
row[i] = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// board = new int[9][9];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* {@inheritDoc} */
|
/* {@inheritDoc} */
|
||||||
|
@ -149,11 +144,6 @@ public class Solver implements SudokuSolver {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ihe the number is already present in the cell
|
|
||||||
if (board[row][col] == num) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check both the row and column
|
// Check both the row and column
|
||||||
for (int i = 0; i < 9; i++) {
|
for (int i = 0; i < 9; i++) {
|
||||||
if (board[row][i] == num || board[i][col] == num) {
|
if (board[row][i] == num || board[i][col] == num) {
|
||||||
|
|
|
@ -107,37 +107,6 @@ class SolverTest {
|
||||||
assertFalse(solver.solve());
|
assertFalse(solver.solve());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void unsolvableTestCase3() {
|
|
||||||
Solver solver = new Solver();
|
|
||||||
|
|
||||||
// More complex example
|
|
||||||
solver.clear();
|
|
||||||
solver.set(0, 0, 1);
|
|
||||||
solver.set(0, 1, 2);
|
|
||||||
solver.set(0, 2, 3);
|
|
||||||
solver.set(1, 0, 4);
|
|
||||||
solver.set(1, 1, 5);
|
|
||||||
solver.set(1, 2, 6);
|
|
||||||
solver.set(2, 3, 7);
|
|
||||||
assertFalse(solver.isSolvable());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void solvableTestCase3() {
|
|
||||||
Solver solver = new Solver();
|
|
||||||
|
|
||||||
// More complex example
|
|
||||||
solver.clear();
|
|
||||||
solver.set(0, 0, 1);
|
|
||||||
solver.set(0, 1, 2);
|
|
||||||
solver.set(0, 2, 3);
|
|
||||||
solver.set(1, 0, 4);
|
|
||||||
solver.set(1, 1, 5);
|
|
||||||
solver.set(1, 2, 6);
|
|
||||||
assertTrue(solver.isSolvable());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void setBoardInvalidInputThrowsTest() {
|
void setBoardInvalidInputThrowsTest() {
|
||||||
Solver solver = new Solver();
|
Solver solver = new Solver();
|
||||||
|
|
Loading…
Reference in a new issue