Skip to content

Commit

Permalink
Implement SudokuTest.java
Browse files Browse the repository at this point in the history
  • Loading branch information
viktigpetterr committed Jul 12, 2021
1 parent 12f68b0 commit b400c25
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 62 deletions.
20 changes: 17 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,29 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,8 @@ public void start(Stage stage) {
solve.setDefaultButton(true);
solve.setStyle("-fx-base: #82e584;");
solve.setOnAction(event -> {
if (sudoku.checkSudoku(0, 0)) {

if (!sudoku.solve(0, 0)) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Notification");
alert.setHeaderText(null);
alert.setContentText("Sudoku is unsolvable!");
alert.showAndWait();
} else {
rebuildSolvedSudokuField(tilePane, sudoku);

}
if (sudoku.solve()) {
rebuildSolvedSudokuField(tilePane, sudoku);
} else {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle("Notification");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ public class Sudoku {

private final int[][] matrix;

private static final int DIMENSION = 9;
private static final int UNSET = 0;

/**
* Constructs a sudoku board in shape of a Integer matrix with 9x9 squares.
*/
public Sudoku() {
matrix = new int[9][9];
matrix = new int[DIMENSION][DIMENSION];
}

/**
Expand All @@ -41,13 +44,20 @@ public void setValue(int row, int col, int value) {
matrix[row][col] = value;
}

/**
* @return the dimension of the matrix.
*/
public int dimension() {
return DIMENSION;
}

/**
* Clears all Integers in the matrix and replace them with zeros.
*/
public void clear() {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
matrix[i][j] = 0;
matrix[i][j] = UNSET;
}
}
}
Expand All @@ -56,25 +66,19 @@ public void clear() {
* Checks if the number(num) already exist in the specific column, row or
* square.
*/
private boolean numIsLegal(int row, int col, int num) {
// Check the column.
for (int k = 0; k < 9; ++k) {
if (num == matrix[k][col]) {
return false;
}
}
// Check the row.
for (int k = 0; k < 9; ++k) {
if (num == matrix[row][k]) {
public boolean numIsLegal(int row, int col, int num) {
// Check the column and the row.
for (int i = 0; i < DIMENSION; ++i) {
if (num == matrix[row][i] || num == matrix[i][col]) {
return false;
}
}
// Check the square.
int boxRow = (row / 3) * 3;
int boxCol = (col / 3) * 3;
for (int k = 0; k < 3; ++k) {
for (int m = 0; m < 3; ++m) {
if (num == matrix[boxRow + k][boxCol + m]) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (num == matrix[boxRow + i][boxCol + j]) {
return false;
}
}
Expand All @@ -83,27 +87,38 @@ private boolean numIsLegal(int row, int col, int num) {
}

/**
* Backtracking method that checks if the current values in the matrix is
* legal according to Sudoku. rules.
* Checks if the current values in the matrix are
* legal according to Sudoku rules.
*
* @return Returns true if the current values in the matrix are legal
* according to Sudoku rules. Otherwise false.
*/
public boolean checkSudoku() {
return checkSudoku(0, 0);
}

/**
* Backtracking method that checks if the current values in the matrix are
* legal according to Sudoku rules.
*
* @param row - The row where the check will start.
* @param col - The column where the check will start.
* @return Returns true if the current values in the matrix are legal
* according to Sudoku rules. Otherwise false.
*/
public boolean checkSudoku(int row, int col) {
if (row == 9) {
private boolean checkSudoku(int row, int col) {
if (row == DIMENSION) {
row = 0;
col++;
if (col == 9) {
if (col == DIMENSION) {
return true; // All places in the matrix has been checked.
}
}
if (matrix[row][col] == 0) {
if (matrix[row][col] == UNSET) {
return checkSudoku(row + 1, col);
}
int current = matrix[row][col];
matrix[row][col] = 0;
matrix[row][col] = UNSET;
if (numIsLegal(row, col, current)) {
if (checkSudoku(row + 1, col)) {
matrix[row][col] = current;
Expand All @@ -115,6 +130,15 @@ public boolean checkSudoku(int row, int col) {
return false;
}

/**
* Method for solving the Matrix according to Sudoku rules.
*
* @return Returns true if the Sudoku has been solved according to Sudoku
* rules. Otherwise false.
*/
public boolean solve() {
return solve(0, 0);
}
/**
* Backtracking method for solving the Matrix according to Sudoku rules.
*
Expand All @@ -123,28 +147,32 @@ public boolean checkSudoku(int row, int col) {
* @return Returns true if the Sudoku has been solved according to Sudoku
* rules. Otherwise false.
*/

public boolean solve(int row, int col) {
if (row == 9) {
if (!checkSudoku(row, col)) {
return false;
}

if (row == DIMENSION) {
row = 0;
col++;
if (col == 9) {
if (col == DIMENSION) {
return true; // All places in the matrix has been checked.
}
}
if (matrix[row][col] != 0) {

if (matrix[row][col] != UNSET) {
return solve(row + 1, col);
}

for (int num = 1; num <= 9; num++) {
for (int num = 1; num <= DIMENSION; num++) {
if (numIsLegal(row, col, num)) {
matrix[row][col] = num;
if (solve(row + 1, col)) {
return true;
}
}
}
matrix[row][col] = 0; // reset on backtrack
matrix[row][col] = UNSET; // reset on backtrack
return false;
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,96 @@
package com.github.viktigpetterr.sudokusolver.sudoku;

import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class SudokuTest
{
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SudokuTest {

private Sudoku sudoku;

@BeforeEach
public void setUp() {
sudoku = new Sudoku();
}

@Test
public void testSize() {
assertEquals(9, sudoku.dimension());
}

@Test
public void testGetValueOf() {
int row = 0;
int col = 0;
int val = 1;

sudoku.setValue(row, col, val);
assertEquals(Integer.toString(val), sudoku.getValueOf(col, row));
}

@Test
public void testClear() {
int row = 0;
int col = 0;
int val = 1;

sudoku.setValue(row, col, val);
assertEquals(Integer.toString(val), sudoku.getValueOf(col, row));

sudoku.clear();
int dimension = sudoku.dimension();
for (col = 0; col < dimension; col++) {
for (row = 0; row < dimension; row++) {
assertEquals(Integer.toString(0), sudoku.getValueOf(row, col));
}
}
}

@Test
public void testNumIsLegal() {
int row = 0;
int col = 0;

sudoku.setValue(row, col, 1);
sudoku.setValue(row, sudoku.dimension() - 1, 1);
assertFalse(sudoku.numIsLegal(row, col, 1));

sudoku.setValue(row, sudoku.dimension() - 1, 0);
sudoku.setValue(sudoku.dimension() - 1, col, 1);
assertFalse(sudoku.numIsLegal(row, col, 1));

sudoku.setValue(sudoku.dimension() - 1, col, 0);
sudoku.setValue(row, col, 1);
assertFalse(sudoku.numIsLegal(row, col, 1));

assertTrue(sudoku.numIsLegal(row, col , 2));
}

@Test
public void testCheckSudoku() {
int row = 0;
int col = 0;

sudoku.setValue(row, col, 1);
sudoku.setValue(row, sudoku.dimension() - 1, 1);
assertFalse(sudoku.checkSudoku());
}

@Test
public void testSolve() {
int row = 0;
int col = 0;

sudoku.setValue(row, col, 1);
assertTrue(sudoku.solve());

sudoku.clear();
sudoku.setValue(row, col, 1);
sudoku.setValue(row, sudoku.dimension() - 1, 1);
assertFalse(sudoku.solve());
}

}

0 comments on commit b400c25

Please sign in to comment.