This is a simple Sudoku game built with Java-11 and Swing's JFrames. The game is interactive, allowing players to enter digits into the grid and see if they are correct or not. The game also includes a backtracking algorithm that players can use to solve the puzzle if they get stuck. Additionally, there is a "New Game" button that generates a new proper Sudoku puzzle with only one solution.
Java 11 or higher
- Clone the repository to your local machine:
git clone https://github.com/ArmanGrewal/SudokuJava.git
- Open the project in your favorite IDE (e.g. Eclipse, IntelliJ, NetBeans).
- Run the SudokuMain.java file in your IDE to start the game.
- Use the mouse to click on a cell in the grid and enter a digit (1-9) using the keyboard.
- Click the "Enter/Return" to see if your current entries are correct. Correct digits will turn green, while incorrect digits will turn red.
- Press the "Solve" button to automatically fill in the rest of the puzzle using the backtracking algorithm.
- To generate a new proper Sudoku puzzle with only one solution, click the "New Game" button.
- To exit the game, simply close the window.
- It is a grid with 9 rows, 9 columns and 9 boxes (or blocks or regions), each having 9 cells (or squares), for a total of 81 cells
- Each row, each column and each box must have a unique number in them
- Generate a random completely filled sudoku board (inspiration)
- First fill the 3x3 subgrids at the diagonal with random permutations (As they are independent of each other)
- Secondly fill all the remaining sudoku board using backtracking
- Remove random values from the board one by one
- Check if current board is proper (has only one solution)
- If current board is not proper, add back the previous value you removed which made the sudoku ("unproper").
- Now you have a "minimal sudoku" at hand, proper sudoku with most possible removed cells.
-
public class Cell extends JTextField
newGame()
# reset this cell for new gamepaint()
# paint the cell based on its status (Given, Not given, Correct guess, Wrong guess)
-
public class Puzzle
int[][] numbers
# numbers on the puzzleboolean[][] isGiven
# need to guess or notvoid newPuzzle(int guesses, boolean difficult)
# generates new puzzle with given guesses or most difficult possiblegetMostDifficultSudoku()
# Generate most difficult possible sudoku board (using greedy algm)getSudokuGuesses(int guesses)
# Generate sudoku board with given no. of guesses
1.generateSudokuBoard()
# Generates a completely filled random sudoku board
-helper_fillSubgrid(int[][], int, int)
# Fills 3x3 subgrids with random permutations
-helper_fillBoard(int[][], int, int)
# Fills rest of board with correct values (using backtracking)
2.hasUniqueSoln(int[][])
# Check if given sudoku is proper
-allSolutions(int[][])
# Returns all possible solutions of current sudoku
3.removeOneElement(int[][])
# Removes one random element from sudoku board
isValid(int[][], int, int, int)
# Checks if given cell placement is validisSolved(int[][])
# Check if we have solved the current board
-
public class GameBoardPanel extends JPanel
addBordersAndCells()
# adds borders and numbers to cellssolveAndSet()
# Function added to btnSolvesolve()
# Solves puzzle using backtracking
isValid(int[][], int, int, int)
# Checks if given cell placement is validisSolved()
# Check if we have solved the current boardnewGame()
# Function added to btnResetGame-
private class CellInputListener implements ActionListener```
-
public class SudokuMain extends JFrame
GameBoardPanel board
# PuzzleJpanel buttonPanel
# Panel for two buttonsJButton btnResetGame
# Button to reset gameJButton btnSolve
# Button to solve the game
Add a reset button to generate new sudokus- Adding a difficulty bar to generate Easy/Medium/Hard sudokus (currently it generates hardest it can find using a Greedy algorithm)
- After entering a guess, highlight all boxes with the same value of the guess and signal the conflicting cells if any.
- Timer (pause/resume), score, progress bar.
- Sound effect, background music, enable/disable sound?
- Try
KeyEvent
withkeyTyped()
handler; or other means that does not require pushing of ENTER key. - High score and player name?
- Hints and cheats (reveal a cell, or reveal all cells with number 8)?
- Mouseless interface?
- Multi-Player network game.