This is a Java-based Graphical User Interface (GUI) Sudoku Solver. It provides an interactive interface for solving Sudoku puzzles. It is written in Java and uses a backtracking algorithm to solve the puzzle.
- A user-friendly GUI that allows input of the Sudoku puzzle.
- The ability to solve any valid Sudoku puzzle.
- Provides a clear message if the puzzle is unsolvable.
- The ability to save and load puzzles from a file.
- Support for different levels of difficulty.
- Username: admin1
- Password: admin123
The Backtracking Algorithm is implemented in the SudokuSolver.java. It's implemented in the private solve method.
The algorithm starts at line 29:
private static boolean solve(int[][] puzzle, int row, int col) {First, it checks if we've reached the end of the puzzle:
if (row == 9) { // All rows have been filled, and the puzzle is solved return true; }If we've reached the end of a row, it moves to the next row:
if (col == 9) { // Move to the next row return solve(puzzle, row + 1, 0); }If the current cell is already filled, it moves to the next cell:
if (puzzle[row][col] != 0) { // Cell is already filled, move to next cell return solve(puzzle, row, col + 1); }The core of the algorithm is in the following loop:
for (int value = 1; value <= 9; value++) { if (isValid(puzzle, row, col, value)) { puzzle[row][col] = value; if (solve(puzzle, row, col + 1)) { return true; } puzzle[row][col] = 0; // Backtrack } }This loop tries each value from 1 to 9 in the current cell. If a value is valid (doesn't violate Sudoku rules), it:
- Places the value in the cell
- Recursively tries to solve the rest of the puzzle
- If the recursive call returns true, it means a solution is found
- If not, it backtracks by setting the cell back to 0 and tries the next value
If no value works for this cell, it returns false, which triggers backtracking in the previous recursive call:
return false;The isValid method (lines 64–82) checks if placing a value in a specific cell violates any Sudoku rules by checking the row, column, and 3x3 sub-grid.
Another part is SudokuGenerator.java which contains an algorithm for generating Sudoku puzzles. The core of the generation algorithm is contained in the solve method, which uses a recursive backtracking approach to generate a complete Sudoku solution.
The algorithm starts at line 76:
private boolean solve(int row, int col) { if (col == 9) { col = 0; row++; if (row == 9) { return true; } }This part handles moving to the next row when we reach the end of a column. If we've filled the entire grid (row == 9), we return true as we've found a solution.
if (solution[row][col] != 0) { return solve(row, col + 1); }If the current cell is already filled (not 0), we move to the next cell.
List<Integer> numbers = generateRandomNumbers(); for (int num : numbers) { if (isSafe(row, col, num)) { solution[row][col] = num; if (solve(row, col + 1)) { return true; } solution[row][col] = 0; } }This is the core of the backtracking algorithm:
- We generate a list of numbers 1–9 in random order.
- For each number, we check if it's safe to place in the current cell.
- If it's safe, we place the number and recursively try to solve the rest of the grid.
- If the recursive call returns true, we've found a solution.
- If not, we remove the number (backtrack) and try the next number.
return false;If we've tried all numbers and none work, we return false to trigger backtracking.
The isSafe method (lines 157–159) checks if a number can be placed in a cell without violating Sudoku rules:
private boolean isSafe(int row, int col, int num) { return !usedInRow(row, num) && !usedInColumn(col, num) && !usedInBox(row - row % 3, col - col % 3, num); }After generating a complete solution, the algorithm removes cells to create the puzzle:
private void removeCells(int difficulty) { Random rand = new Random(); int cellsToRemove = 81 - difficulty; while (cellsToRemove > 0) { int row = rand.nextInt(9); int col = rand.nextInt(9); if (puzzle[row][col] != 0) { int temp = puzzle[row][col]; puzzle[row][col] = 0; if (!hasUniqueSolution()) { puzzle[row][col] = temp; } else { cellsToRemove--; } } } }This method:
- Randomly selects cells to remove.
- Removes a cell's value temporarily.
- Checks if the puzzle still has a unique solution.
- If not, it restores the value; if yes, it keeps the cell empty.
- Continues until the desired number of cells are removed.
- A user-friendly GUI that allows input of the Sudoku puzzle.
- The ability to solve any valid Sudoku puzzle.
- Provides a clear message if the puzzle is unsolvable.
- The ability to save and load puzzles from a file.
- Support for different levels of difficulty.
- Clone or download the project from GitHub.
- Open the project in your preferred Java Integrated Development Environment (IDE).
- Add JAR files to the classpath if needed.
- Create the database using the provided SQL file.
- Build and run the project from your IDE.
- Run the project.
- Login or Register as a user.
- Enter the values in the empty cells of the Sudoku puzzle in the GUI.
- Click on the "Solve" button to solve the puzzle.
- If the puzzle is solvable, the solved puzzle will be displayed in the GUI.
- A clear message will be displayed in the GUI if the puzzle is unsolvable or has multiple solutions.
- Improve the algorithm to make it more efficient and faster.
- Add a feature to allow users to highlight cells that contain the same value.