Adding Sudoku to libipuz

Status: Draft | Sep 2025

Author: Jonathan

Reviewer: Federico, Sepehr

Goals

This doc details a proposed way of implementing sudoku-style puzzles in libipuz. There are a number of such puzzles listed in the ipuz spec. We want to load, save, and potentially modify these puzzle kinds.

Overall Approach

We create a Sudoku base class that inherits from IpuzGrid. Each cell in IpuzGrid has a a numerical value (or letter) and can be accessed through the grid functions.

Proposed Class Heirarchy

We add the following classes to libipuz:

IpuzPuzzle
↳IpuzGrid
 ↳IpuzSudoku
  ↳IpuzCalcudoku
  ↳IpuzSudokuDiagonal
  ↳IpuzSudokuHyper
  ↳IpuzSudokuJigsaw
  ↳IpuzSudokuGreaterThan
  ↳IpuzSudokuKiller
  ↳IpuzLatinSquare
  ↳IpuzWordoku

Note

I decided on IpuzSudokuHyper instead of IpuzHyperSudoku for class naming convention, as it’s close to what GTK does. IpuzNonogramColor does this as well. That being said, we may want to go with eg. IpuzKiller and drop the Sudoku string from the class name. It’s more concise, and matches IpuzCryptic / IpuzBarred. We are inconsistent here.

Representation

IpuzSudoku inherits from IpuzGrid. Each IpuzCell within the grid can have either a solution or a number depending on puzzle kind, though not both. The label and initial val attributes are unused. In addition, cells can have an IpuzStyle attached to them.

Kind

Cell Value Type

::boxes

Group Type

SUDOKU

number

TRUE

None

CALCUDOKU

number

FALSE

Cage

DIAGONAL

number

TRUE

Zone

HYPER

number

TRUE

Zone

JIGSAW

number

FALSE

Zone

GREATER_THAN

number

TRUE

None

KILLER

number

FALSE

Cage

LATIN_SQUARE

number

FALSE

None

WORDOKU

solution

TRUE

None

Note

GREATER_THAN uses an IpuzStyle on border cells to indicate the equality comparisons.

API

The base class, IpuzSudoku will have the following properties, shared across all the variants:

::values GArray<string/null> — Values used in the puzzle
::displayvalues boolean — Indicates the list of values should be displayed
::boxes boolean — Divide the puzzle into boxes
::showoperators boolean — Show calculation operators
::cageborder enum{THICK / DASHED} — Cage borders are thicker or dashed

Not every value is used with every puzzle type, but like the spec, there’s enough overlap that we should keep all these the base IpuzSudoku class.

Groups

In addition, the spec has the concept of Zones and Cages. These are effectively a set of cells with a style, and in the case of cages, a value and operator as well. No puzzle type has both. The spec treats them as separate types, but there’s enough overlap that it’s easier to combine them into the same struct. We will use IpuzCellGroup for this:

typedef enum
{
  IPUZ_CELL_OPERATOR_NONE = 0,
  IPUZ_CELL_OPERATOR_PLUS,
  IPUZ_CELL_OPERATOR_MINUS,
  IPUZ_CELL_OPERATOR_MULTIPLY,
  IPUZ_CELL_OPERATOR_DIVIDE,
} IpuzCellOperator;

typedef struct _IpuzCellGroup IpuzCellGroup;

IpuzCellGroup      *ipuz_cell_group_ref          (IpuzCellGroup *group);
void                ipuz_cell_group_unref        (IpuzCellGroup *group);
IpuzCellCoordArray *ipuz_cell_group_get_cells    (IpuzCellGroup *group);
IpuzStyle          *ipuz_cell_group_get_style    (IpuzCellGroup *group);
IpuzCellOperator    ipuz_cell_group_get_operator (IpuzCellGroup *group);
gint                ipuz_cell_group_get_value    (IpuzCellGroup *group);

We could potentially add the following if it turned out to be necessary:

typedef enum
{
  IPUZ_CELL_GROUP_MODE_ZONE,
  IPUZ_CELL_GROUP_MODE_CAGE,
} IpuzCellGroupMode;

IpuzCellGroupMode   ipuz_cell_group_get_mode     (IpuzCellGroup *group);

Accessing groups

Groups are somewhat equivalent to Clues within IpuzCrosswords, as they provide a region of the cell to operate on. As such, it should probably have a similar API. We don’t need an interface to represent this, as we can put this access on IpuzSudoku (while with clues we split it between acrostics and crosswords)

guint          ipuz_sudoku_get_n_cell_groups (IpuzSudoku *self);
IpuzCellGroup *ipuz_sudoku_get_cell_group    (IpuzSudoku *self,
                                              guint       n);

Open Questions

  • IpuzCell uses a string for solution. We could add :get_solution_number() to match that rather than force a string→int conversion.

  • It’s incredibly convenient to have a link between cells and clues (and vice-versa). Thus, we have the back-pointer along with IpuzCell::get_clue(). Should we set up the same backlink for IpuzCell and IpuzCellGroup? Note, crosswords can technically have zones as well to apply styles, though I’ve never seen one in the wild and we don’t support it yet.

  • Do we need to make it possible to edit sudokus? What would the _fix() functions look like? For some puzzles, they’d probably create the appropriate groups, but could also imagine wordokus having a quote property like acrostics.

    As a rule of thumb, I’d rather not write a speculative API. I think we should only work on the editing interface after we have a tool that is able to start editing it. If GNOME Sudoku develops that as part of its generation path, we can investigate that.

  • Sudoku puzzles with boxes set to TRUE implicitly have a set of groups as well as potentially explicit ones. Do we want to handle those boxes as groups as well? Is this useful?