File Exchange

image thumbnail

Sudoku (Dancing Links Solver)

version (17.5 KB) by Per-Anders Ekstrom
SUDOKU Graphical User Interface for playing Sudoku-Puzzles.


Updated 27 Feb 2007

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Game Rules:
Fill in the grid so that every row, every column, and every 3x3 box contains the digits 1 through 9.

Game Board:
The sudoku interface lets the user solve sudoku puzzles graphically. The puzzles are either generated by the built-in puzzle generator (three difficult levels can be chosen), randomly taken from the built-in database of 160 really difficult puzzles, entered manually by the or loaded from three standard ascii sudoku files 'Simple Sudoku' (.ss), 'Sudoku Puzzle' (.sdk) or 'Sudoku Puzzle Collection' (.sdm).
The game board can be saved to Simple Sudoku and Sudoku Puzzle files.
The current board can also be exported to various image-files, such as, png, bmp, eps or tiff. The board can be printed and also copied to clipboard to be pasted as an image in for example a MS Word document.

Game Controls:
The sudoku game can be played using either mouse or keyboard (or both). Move around marker using arrow keys and add a number using number keys. Delete, backspace or 0 (zero) will remove number from board. With mouse you move around marker using left-click and reach a context-menu with number choices using right-click.
Illegal characters can not be entered.

Game Solver:
The sudoku game has two built-in puzzle solvers, one quick and one 'human-like'. The quick one is based on Dancing Links (DLX), a technique suggested by Donald Knuth to efficiently implement his Algorithm X. Algorithm X is a recursive, nondeterministic, depth-first, brute-force algorithm that finds all solutions to the exact cover problem. The human solver finds Naked Singles, Hidden Singles, Naked Pairs and Locked Candidates 1 and 2. If this is not enough it takes a guess and tries to solve it recursively.
The dlx-solver is used for solving and generating puzzles while the human puzzle is used to generate a log of solution steps.
The game allows for automatic solving of the whole puzzle or just one single square. To help the user one can show all possible candidates for unfilled squares as well as showing all mistakes in a different color as well as making it impossible to make an illegal mark.

Extra Game Features:
The sudoku game has Undo and Redo functionality that can be reached from the Edit-menu, Context-menu or by the shortcuts Ctrl-Z (Undo) and Ctrl-R (Redo).
Every time one restarts a game a timer is started. Current time can be shown using Ctrl-T or enable/disabled from the Help menu.
A coloring help of possible candidates can be added to the matrix from the colored numbers in the toolbar.

sudoku % Start Main Sudoku Interface
sudoku('uitest') % Solve all 160 boards in database (test set)

Cite As

Per-Anders Ekstrom (2020). Sudoku (Dancing Links Solver) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)

tddt dtduh


Per-Anders Ekström

Hello Kiko Kiko, thanks for mentioning. My first guess is that you have an old Matlab version, this Sudoku game was developed using release R2006a. At line 176 I use a function named arrayfun introduced in R14sp3, so if you have an older version than that it will not work.

kiko kiko

I think there is a bug around line 176.

Gersi Rami

No comment

Paul G

Excellent code!

Aasmund Kvamme

Very nice graphics and an excellent logic!

Zhijun Wang

Very good

Ali Karapelit

Eric Dean

Very impressive!

John D'Errico

A superb Sudoku tool. Great help. Everything I tried worked as it should. It can generate game layouts for you.

A minor complaint - this tool almost makes solving a puzzle too easy. ;-) Well done anyway.


Enhanced the toolbar to use a real toolbar.

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Sudoku! (v3.4)