Skip to main content

Examples

Sudoku

Constraint satisfaction, powered by Directive. The game rules ARE the constraints.

Play it

Loading example…

Use arrow keys to navigate, 1–9 to input, Backspace to clear, N for notes, H for hint. Ctrl+Z / Ctrl+Shift+Z for undo/redo.

How it works

Sudoku is literally a constraint satisfaction problem: no duplicates in rows, columns, or 3×3 boxes. The game rules map 1:1 to Directive’s constraint–resolver flow.

  1. Facts – Grid state, solution, givens, timer, selection, notes, difficulty
  2. Derivations – Conflicts, progress, timer display, same-number highlighting, candidates (auto-tracked, no manual deps)
  3. Events selectCell, inputNumber, toggleNote, requestHint, tick, newGame
  4. Constraints timerExpired (priority 200), detectConflict (100), puzzleSolved (90), hintAvailable (70) – evaluated by priority after every fact change
  5. Resolvers – Handle game won/lost, increment error count, reveal hints
  6. Effects – Timer warnings at 60s and 30s, game result logging

The constraint cascade is the key insight: when a player types “5”, the grid fact updates, derivations recompute (conflicts, progress, isSolved), then constraints evaluate by priority – detecting conflicts, checking for a win, or firing a hint.

Summary

What: A fully playable Sudoku puzzle with multiple difficulties, notes, hints, undo/redo, and a countdown timer.

How: The game is a single Directive module with 14 facts tracking grid/selection/timer state. Derivations auto-compute conflicts, progress, and candidates. Four prioritized constraints cascade on every input to detect conflicts, check for a win, expire the timer, or reveal hints. Resolvers handle the outcomes. Effects fire timer warnings.

Why it works: Sudoku is a constraint satisfaction problem – Directive’s constraint–resolver flow maps 1:1 to the game rules. No imperative state machine needed; declare what must be true and Directive handles the rest.

Source code

// Source file "sudoku.ts" not found

Stay in the loop. Sign up for our newsletter.

We care about your data. We'll never share your email.

Powered by Directive. This signup uses a Directive module with facts, derivations, constraints, and resolvers – zero useState, zero useEffect. Read how it works