A desktop Sudoku game written in C++ with SFML. A backtracking algorithm both generates the puzzles and solves them, with several difficulty levels, sound effects, and a solver mode that completes any valid grid on demand.
The game is available as a downloadable release.
Why Sudoku#
Sudoku is a small, well-defined constraint problem: fill a 9×9 grid so that every row, every column and every 3×3 box contains the digits 1 to 9 exactly once. That makes it an ideal project for practising two things at once — a classic search algorithm, and the structure of a real-time graphical application in C++.
What it does#
- Classic gameplay — select a cell, enter a digit, and see the grid update immediately
- Puzzle generation — every new game is a fresh, valid puzzle
- Difficulty levels — from casual to advanced, by changing how many cells start empty
- Solver mode — the algorithm completes any valid puzzle automatically
- Audio — sound effects that make the game feel responsive
The backtracking algorithm#
Backtracking is a depth-first search that builds a solution one step at a time and undoes the last step when it hits a dead end. For Sudoku, that means:
- Find the next empty cell.
- Try a digit from 1 to 9.
- If the digit doesn't break the row, column or box rules, place it and move on to the next cell.
- If no digit fits, clear the cell and go back to the previous one to try its next option.
- When there are no empty cells left, the grid is solved.
In simplified form:
bool solve(Grid& grid) {
auto cell = findEmpty(grid);
if (!cell) return true; // no empty cell: solved
for (int digit : candidates()) { // shuffled for generation
if (isValid(grid, *cell, digit)) {
grid.set(*cell, digit);
if (solve(grid)) return true;
grid.clear(*cell); // dead end: backtrack
}
}
return false;
}One algorithm, two jobs#
The same function generates puzzles and solves them. To generate, it starts from an empty grid and tries digits in a random order, which produces a different complete grid every game. Cells are then removed to create the puzzle, with the number removed set by the difficulty level. To solve, it runs on the player's puzzle and tries digits in order until the grid is complete.
Unique solutions#
A good Sudoku puzzle has exactly one solution — otherwise the player can be "wrong" while following every rule. Guaranteeing that is the subtle part of generation: removing a cell is only safe if the puzzle still has a single solution afterwards. The standard way to check it is to reuse the solver as a counter: instead of stopping at the first solution, it keeps searching and reports whether a second one exists.
Validity checks#
Every placement is checked against its row, its column and its 3×3 box. Because these checks run millions of times during a search, they need to be cheap — direct index arithmetic on the grid, no allocation, no copying.
Architecture#
main
└── Game (engine)
├── SFML window + event loop
├── Grid board state, fixed vs. player cells
├── Solver backtracking: generate, solve, validate
├── Renderer draws grid, numbers, selection
└── Audio sound effectsA game engine class owns the SFML window and runs the classic loop: handle events, update state, draw. The grid, the solver and the renderer are separate modules, so the algorithm never knows it is being drawn and the renderer never knows how puzzles are made.
Results#
- A fully playable Sudoku game with smooth SFML rendering
- The backtracking solver completes any valid 9×9 grid in milliseconds
- Published as a downloadable binary on GitHub Releases
What I learned#
Backtracking is short to write and easy to get subtly wrong, because a mistake in a single validity check still produces grids that look plausible. Keeping the solver completely separate from the rendering code meant it could be run and checked on its own, without opening a window.
Sudoku
Welcome to the Sudoku Game repository! This project is a classic Sudoku puzzle game built using SFML (Simple and Fast Multimedia Library). Enjoy a user-friendly interface, various difficulty levels, and hours of brain-teasing fun!
The game is developed using a backtracking algorithm to generate and solve the Sudoku puzzles. The algorithm works by filling in the grid with numbers from 1 to the grid size (9 for a standard 9x9 Sudoku grid) aleatorily. If the number is valid, it moves on to the next cell. If the number is not valid, it tries the next number. If no number is valid, it backtracks to the previous cell and tries a different number. This process is repeated until the grid is filled.
Features
- Classic Sudoku gameplay with a clean, intuitive interface.
- Multiple difficulty levels to challenge both beginners and seasoned players.
- Enhanced audio effects for an immersive experience.
Screenshots
Getting Started
Prerequisites
Ensure you have the following installed on your system:
- A C++ compiler (GCC, Clang, MSVC, etc.)
- SFML 2.6.1
Note:
-
If you are using Visual Studio Code, you need to add the SFML include path to your project's
c_cpp_properties.jsonfile. For example:Navigate to .vscode/c_cpp_properties.json and add the following to the includePath array in the configurations object:
{ "configurations": [ { "includePath": [ "/your/path/to/SFML-2.6.1/include" ], } ] } -
Change the Makefile to reflect the location of your SFML include and lib directories. For example:
# SFML include and lib directories SFML_INCLUDE = "\your\path\to\SFML-2.6.1\include" SFML_LIB = "\your\path\to\SFML-2.6.1\lib"
Installation
-
Clone the repository:
git clone https://github.com/ismailza/Sudoku.git
-
Navigate to the project directory:
cd Sudoku -
Compile the project (ensure SFML is correctly set up):
make
-
Run the executable:
make run
Contributing
Contributions are what make the open-source community such an amazing place to be, learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (git checkout -b feature/feature_name)
- Commit your Changes (git commit -m 'Add some feature_name')
- Push to the Branch (git push origin feature/feature_name)
- Open a Pull Request
License
Distributed under the MIT License. See LICENSE for more information.
MIT License Copyright (c) 2024 Ismail ZAHIR Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.







