Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Sudoku NEA - Python Sudoku Solver / Generator

This repository is my A-level Computer Science NEA project, now cleaned up and put on GitHub as part of my portfolio.

The project is a Sudoku game made in Python. It generates valid Sudoku puzzles, lets the user play them through a Tkinter interface, checks their inputs, gives hints, and can solve the puzzle using a recursive backtracking solver. It also includes a login/database system so the program can store users, puzzles, games, mistakes, hints used and completion times.

The main idea was not just to make a basic Sudoku game. I wanted to make something that helps the user actually learn how to solve the puzzle instead of just giving them the answer straight away.

GitHub repo description

Python Sudoku NEA with Tkinter UI, valid puzzle generation, recursive backtracking solver, hint engine and SQLite login/progress tracking.

Demo

NEA demo video:

https://youtu.be/gMHvNu3g-E8?si=NkMyHEde13d8xLXu

Why I made this

A lot of people get stuck playing Sudoku. This could be someone young who is still learning the rules, or an older person who knows the game but gets stuck on harder puzzles.

A normal hint button in a lot of Sudoku apps just gives the next answer. I did not really like this because it solves the problem for the user but does not explain anything. My idea was to make a Sudoku game where the hint system gives a smaller nudge, so the player can still stay in control and learn how the logic works.

I also wanted the program to be clean and not filled with adverts. When I looked at existing Sudoku websites and apps, a lot of them worked fine, but the adverts made the interface feel cluttered. This project is open source and there is no monetisation or adverts, so the game is more focused on the puzzle itself.

Main features

  • Random Sudoku puzzle generation
  • Easy, Medium and Hard difficulty options
  • Unique-solution checking so generated puzzles are valid
  • 9x9 grid stored as a list of lists
  • Input validation so only numbers 1-9 are accepted
  • Locked starting cells so the user cannot edit the original clues
  • Recursive backtracking solver
  • Hint engine that gives one move at a time
  • Tkinter user interface
  • Timer, mistakes and hints used tracking
  • Login/register system
  • SQLite database for users, puzzles, games and moves
  • Completion checking when the puzzle is solved

How the system works

The project is split into separate parts instead of putting everything into one huge block of code. I did this because it makes the program easier to understand, debug and extend.

The basic flow is:

User
  ↓
Tkinter User Interface
  ↓
Sudoku Game Logic
  ↓
Puzzle Generator / Solver / Hint Engine / SQLite Database

The user only sees the interface. Behind the interface, the program handles the Sudoku rules, puzzle generation, solving, hints and database storage.

Main parts of the project

Tkinter user interface

The UI is the part the player actually uses. It shows the Sudoku grid, buttons and status messages.

The interface includes:

  • Difficulty selector
  • 9x9 Sudoku grid
  • New Puzzle button
  • Hint button
  • Check button
  • Solve button
  • Clear button
  • Timer label
  • Status label for messages like invalid move or puzzle solved

I used Tkinter because it is simple, low-resource, and works on Windows and Linux. It is not meant to be a flashy interface. The main goal was to make it clear and usable.

Puzzle generator

The puzzle generator creates a completed Sudoku board first, then removes numbers from it.

The program removes numbers randomly, but it checks the puzzle after removing values to make sure the puzzle still has only one solution. This is important because if a puzzle has more than one solution, it is not really a fair Sudoku puzzle.

The difficulty is controlled by changing how many clues are left on the board:

Easy   → more clues left
Medium → fewer clues left
Hard   → even fewer clues left

The generator also makes sure the puzzle does not go below the minimum clue limit.

Solver

The solver uses recursive backtracking.

Backtracking works by trying a number in an empty cell, checking if it is valid, and then continuing. If the solver reaches a dead end, it resets the cell back to 0 and tries another number.

The basic idea is:

find an empty cell
try numbers 1-9
check if the number follows Sudoku rules
place the number if valid
recursively solve the rest of the board
if it fails, undo the move and try again

I used backtracking because it is easy to understand, fits well with recursion, and is efficient enough for a 9x9 Sudoku game.

Hint engine

The hint engine uses the solver in the background, but it does not solve the whole puzzle for the user.

It finds an empty cell and gives the correct value for that cell. The important part is that it only gives one move at a time. This means the user still has to carry on playing and learning instead of the program taking over completely.

The hint engine can also check possible values for a cell. For example, if only one number can go in a certain position, it can give a more useful explanation instead of just saying the answer.

This was one of the more important parts of the project because I wanted the program to feel more like a helper than a cheat button.

Database

The project uses SQLite to store information.

The database stores:

  • users
  • generated puzzles
  • completed or started games
  • moves made by the user
  • mistakes
  • hints used
  • completion time

I also used password hashing rather than storing passwords in plain text. This was included to make the login system more secure and to show that I was thinking about how user data should be handled.

The database is split into separate tables so the data is more organised. For example, user data is separate from puzzle data and move data. This makes it easier to manage and avoids mixing everything together.

Data structures

The main data structure is a 9x9 list of lists.

Example:

board = [
    [5, 3, 0, 0, 7, 0, 0, 0, 0],
    [6, 0, 0, 1, 9, 5, 0, 0, 0],
    [0, 9, 8, 0, 0, 0, 0, 6, 0],
]

A 0 means the cell is empty.

I used this because it is simple to read and write from. It also works well with the solver because the algorithm can easily access the row and column using indexes.

The program also uses a locked cell mask so it knows which numbers were part of the original puzzle and should not be edited by the player.

Sudoku validation

The validation checks three things:

  1. The same number is not already in the row.
  2. The same number is not already in the column.
  3. The same number is not already in the 3x3 box.

This validation is used in different places:

  • when the user enters a number
  • when the solver tries a number
  • when the generator checks if a puzzle is valid
  • when the program checks whether the completed board is correct

Keeping this logic consistent means the whole program follows the same Sudoku rules.

Testing

I tested the project manually using a testing table in my NEA.

The features that passed included:

  • user login
  • invalid password handling
  • generating new puzzles
  • generating different puzzles each time
  • generating valid puzzles
  • number validation
  • rejecting letters and invalid numbers
  • preventing edits to locked cells
  • hint system returning a valid move
  • solver completing puzzles
  • solver handling already solved boards
  • solver handling empty boards
  • full board hint handling

Some tests failed or showed weaknesses. The main issue was the difficulty system. The program can generate Easy, Medium and Hard puzzles, but some Hard puzzles were still too easy. This is because the difficulty is mainly based on the number of clues removed, not the actual solving techniques required.

This is something I would improve in the future by analysing how difficult the puzzle is to solve, instead of only counting clues.

What I learned from this project

This project helped me understand how to break a bigger program into smaller parts. At the start, it felt like one big Sudoku app, but once I split it into the generator, solver, hint engine, UI and database, it became much easier to work on.

I also got better at:

  • recursion
  • backtracking
  • OOP structure
  • using lists of lists
  • input validation
  • Tkinter interfaces
  • SQLite databases
  • testing features properly
  • explaining how my code works

The most useful part for me was learning how the solver and generator connect together. The generator needs the solver to check if a puzzle is valid, and the hint engine also depends on the solver to make sure the hints are correct.

Limitations

This is not a perfect commercial Sudoku app. It was made as an A-level NEA project and then put onto GitHub for display.

Current limitations:

  • Hard mode is not always consistently hard
  • The hint explanations could be more detailed
  • The UI could be cleaned up further
  • The database could be used more for statistics and progress graphs
  • More advanced Sudoku solving techniques could be added
  • Unit tests could be added instead of relying mainly on manual testing

Future improvements

In the future I would like to improve:

  • difficulty rating based on solving steps
  • better hint explanations
  • more polished UI
  • statistics page for completion times and mistakes
  • leaderboard or personal progress tracking
  • extra game modes
  • automated unit tests
  • a cleaner file structure for the portfolio version

How to run

Clone the repository:

git clone git@github.com:wwwiktor08/sudoku-nea.git
cd sudoku-nea

Run the Python file:

python Sudoky.py

If the project is renamed to main.py, use:

python main.py

Tkinter and SQLite are used by the project. SQLite is included with Python. On some Linux systems you may need to install Tkinter separately.

On Arch Linux:

sudo pacman -S tk

On Ubuntu/Debian:

sudo apt install python3-tk

Suggested repository topics

python
tkinter
sqlite
sudoku
backtracking
recursion
oop
a-level-computer-science
nea
portfolio-project

Project status

The NEA version is complete and the exams are now over. I am now using this repository as a portfolio project to show the work I did, how I structured the program, and how I approached solving a larger programming problem.

About

Python Sudoku NEA with Tkinter UI, valid puzzle generation, recursive backtracking solver, hint engine and SQLite login/progress tracking.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages