Compare commits

...

9 commits

Author SHA1 Message Date
Imbus
26df774ca3 Watchexec targets modified in justfile 2023-12-11 14:59:11 +01:00
Imbus
270a9f381d File picker now refuses to load unsolvable files 2023-12-11 14:58:54 +01:00
Imbus
e6cd5a2915 Added bin directory to gitignore 2023-12-11 14:35:25 +01:00
dDogge
af77468edc Extending testcases 2023-12-11 14:34:51 +01:00
dDogge
f9fa515651 Extending testcases 2023-12-11 14:32:51 +01:00
Imbus
d056732add Polishing invalid input handling 2023-12-11 14:25:04 +01:00
Imbus
7e5253fb4b Slightly less ugly fix for invalid input 2023-12-11 13:58:05 +01:00
Imbus
2209dd7786 Ugly fix for invalid input 2023-12-11 13:50:58 +01:00
Imbus
3cf7005151 User feedback on unsolvable 2023-12-11 13:12:50 +01:00
7 changed files with 103 additions and 11 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@
build
.vscode
app/bin

View file

@ -11,7 +11,7 @@ clean:
fd -td -I build -x rm -r
watch:
watchexec -c -w app/src "just test && just run"
watchexec -r -c -w app/src "just test && just run"
watchdoc:
watchexec -c -w app/src "just doc"
watchexec -r -c -w app/src "just doc"

View file

@ -0,0 +1,9 @@
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

View file

@ -0,0 +1,9 @@
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

View file

@ -25,10 +25,16 @@ public class SudokuController {
@Override
public void actionPerformed(ActionEvent e) {
// Solve the board
model.solve();
boolean solved = model.solve();
// Update the view
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 {
}
}
});
@ -64,6 +70,13 @@ public class SudokuController {
if (newBoard != null) {
// Set the model
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
view.updateView(model.getBoard());
}
@ -88,14 +101,33 @@ public class SudokuController {
int row = view.getSelectedRow();
int col = view.getSelectedColumn();
int value = Integer.parseInt(view.getCellValue(row, col));
// Check if the input is legal and update the model and view
if (model.isLegal(row, col, value)) {
model.set(row, col, value);
view.updateView(model.getBoard());
// The value to be inserted into the cell
// Zero inicates an empty cell
// Negative values are invalid
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);
view.updateView(model.getBoard());
}
}
}

View file

@ -29,7 +29,12 @@ public class Solver implements SudokuSolver {
/** Resets the board to all zeros */
@Override
public void clear() {
board = new int[9][9];
for (int[] row : board) {
for (int i = 0; i < row.length; ++i) {
row[i] = 0;
}
}
// board = new int[9][9];
}
/* {@inheritDoc} */
@ -144,6 +149,11 @@ public class Solver implements SudokuSolver {
return false;
}
// Ihe the number is already present in the cell
if (board[row][col] == num) {
return true;
}
// Check both the row and column
for (int i = 0; i < 9; i++) {
if (board[row][i] == num || board[i][col] == num) {

View file

@ -86,7 +86,7 @@ class SolverTest {
@Test
void unsolvableTest() {
Solver solver = new Solver();
// Simple example
solver.clear();
solver.set(0, 0, 1);
@ -107,6 +107,37 @@ class SolverTest {
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
void setBoardInvalidInputThrowsTest() {
Solver solver = new Solver();