{sudoku}

A sudoku as a table, the givens bold - and with solve, solved on the server.

Games Single tags

Syntax

{sudoku 'puzzle'}
{sudoku 'puzzle', solve, title='Caption'}

Parameters

puzzle
The 81 cells row by row: a digit 1-9 for a given, . or 0 for an open cell. Whitespace is passed over, so nine lines will do.

Options

solve
Bare option: fill the open cells with the solution, in another colour than the givens.
title
A caption for the table. Without it the table is named by aria-label (Sudoku, 30 givens).

Description

{sudoku} draws a sudoku as an HTML table with the three-by-three boxes marked. The puzzle is written as its 81 cells, row by row: a digit for a given, a . or 0 for an open cell. Whitespace is passed over, as are the bars and plus signs of a grid drawn in text, so the puzzle may be written as one string or as nine lines.

Without options the givens show in bold and the open cells are empty - ready to print. An open cell says "empty" to a screen reader. With solve a backtracking solver fills in every open cell, in another colour than the givens. It tries the cell with the fewest candidates first, so even the hardest published puzzles are solved well within a second. title= adds a caption.

Under the strict check a wrong number of cells, an unknown character, two equal givens in a row, column or box, and - with solve - a puzzle that has no solution are errors. The colours are --pad-sudoku-line, -box, -given, -solved, -cell and -shade on .pad-sudoku.

Examples

The puzzle

The classic example puzzle as one string - givens bold, the open cells empty.

Templateexamples/sudoku/1-puzzle.pad
{sudoku '53..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79'}
Resultopen alone
5 3 empty empty 7 empty empty empty empty
6 empty empty 1 9 5 empty empty empty
empty 9 8 empty empty empty empty 6 empty
8 empty empty empty 6 empty empty empty 3
4 empty empty 8 empty 3 empty empty 1
7 empty empty empty 2 empty empty empty 6
empty 6 empty empty empty empty 2 8 empty
empty empty empty 4 1 9 empty empty 5
empty empty empty empty 8 empty empty 7 9

Solved on the server

Written as nine lines, solved by the backtracking solver, with a caption.

Templateexamples/sudoku/2-solved.pad
{sudoku '
  8........
  ..36.....
  .7..9.2..
  .5...7...
  ....457..
  ...1...3.
  ..1....68
  ..85...1.
  .9....4..
', solve, title='Arto Inkala, 2012'}
Resultopen alone
Arto Inkala, 2012
8 1 2 7 5 3 6 4 9
9 4 3 6 8 2 1 7 5
6 7 5 4 9 1 2 8 3
1 5 4 2 3 7 8 9 6
3 6 9 8 4 5 7 2 1
2 8 7 1 6 9 5 3 4
5 2 1 9 7 4 3 6 8
4 3 8 5 2 6 9 1 7
7 9 6 3 1 8 4 5 2

See also

  • {chess} A chess position from FEN as an SVG board, with marked squares and arrows.
  • {crossword} A crossword made from a list of words: a numbered SVG grid with the Across and Down clues.