# dellalu/sudoku architecture

> Architecture diagram of the GitHub repository dellalu/sudoku, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dellalu/sudoku
- Repository: https://github.com/dellalu/sudoku

## Overview

#### Purpose and Entry Points
A dependency-free, mobile-first static Sudoku PWA. Browser startup, DOM rendering, and user-event orchestration live in `src/main.js`, loaded by `index.html`. The app is deployable to any static HTTPS host and can run offline after service-worker caching.

#### Browser UI and Application Control
`src/main.js` owns the interactive shell: rendering the board/keypad, dispatching player actions, timer/update UI, and coordinating engine and game-state APIs. `src/styles.css` provides the mobile-first presentation layer. This is a direct browser application rather than a client/server architecture.

#### Sudoku Engine
Core pure puzzle logic is isolated under `src/engine/`:
- `src/engine/board.js` represents and validates Sudoku board structure and constraints.
- `src/engine/solver.js` solves grids and supports uniqueness/validity checks.
- `src/engine/generator.js` builds puzzles with guaranteed unique solutions.
- `src/engine/difficulty.js` grades generated puzzles through logical, human-style solving techniques.
- `src/engine/random.js` supplies seeded deterministic randomness, enabling repeatable generation.

The engine boundary keeps generation and solving independent of DOM, persistence, and UI concerns.

#### Game State and Player Operations
`src/game/state.js` is the stateful gameplay layer between UI and engine. It tracks the generated puzzle, solution, player entries, notes, selected cell, timer-related progress, options, and undo/redo history. It applies moves, removes peer notes after placements, detects completion, and exposes state transitions for UI rendering. Engine validation protects game rules; state logic controls player-facing behavior.

#### Persistence and Save Transfer
`src/game/storage.js` persists resumable game data in browser `localStorage` and handles JSON export/import. Import is a trust boundary: save files are validated for version, size, solution integrity, and consistency between givens and solution before becoming active state. Undo/redo history is intentionally excluded from exported/imported data.

#### Offline PWA Lifecycle
`manifest.webmanifest` provides install metadata, while `sw.js` caches the static application shell for offline startup. The service worker lifecycle also supports an update-available flow surfaced by the browser UI in `src/main.js`. Secure hosting is required for full service-worker and install behavior outside localhost.

#### Runtime and Verification
The runtime is standard browser JavaScript/CSS/HTML, with no npm package dependencies. Development uses Python’s built-in HTTP server via package scripts. Engine correctness—including solving, uniqueness, generation, validity, and seeded determinism—is verified in `tests/engine.test.js`.

Main entry points: [`src/engine/solver.js`](https://github.com/dellalu/sudoku/blob/master/src/engine/solver.js) (Solver), [`src/engine/generator.js`](https://github.com/dellalu/sudoku/blob/master/src/engine/generator.js) (Puzzle generator), [`tests/engine.test.js`](https://github.com/dellalu/sudoku/blob/master/tests/engine.test.js) (Engine verification), [`src/main.js`](https://github.com/dellalu/sudoku/blob/master/src/main.js) (UI controller)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["Browser UI"]
  node_html["App document<br/>HTML entry<br/>[index.html]"]
  node_main["UI controller<br/>browser JavaScript<br/>[main.js]"]
  node_styles["Mobile styles<br/>CSS<br/>[styles.css]"]
end

subgraph group_engine["Sudoku Engine"]
  node_board["Board constraints<br/>board model<br/>[board.js]"]
  node_solver["Solver<br/>[solver.js]"]
  node_generator["Puzzle generator<br/>puzzle generation<br/>[generator.js]"]
  node_difficulty["Difficulty grader<br/>logical grading<br/>[difficulty.js]"]
  node_random["Seeded random<br/>deterministic RNG<br/>[random.js]"]
  node_tests["Engine verification<br/>test suite<br/>[engine.test.js]"]
end

subgraph group_game["Game State &amp; Saves"]
  node_state["Gameplay state<br/>stateful game layer<br/>[state.js]"]
  node_storage[("Save storage &amp; transfer<br/>browser persistence<br/>[storage.js]")]
  node_localstorage[("Browser localStorage<br/>browser storage")]
end

subgraph group_pwa["PWA Lifecycle"]
  node_manifest["Install manifest<br/>PWA manifest"]
  node_serviceworker{{"Service worker<br/>offline cache<br/>[sw.js]"}}
  node_staticcache[("Static shell cache<br/>Cache Storage")]
end

node_html -->|"loads"| node_main
node_html -->|"loads"| node_styles
node_main -->|"applies player actions"| node_state
node_main -->|"save, export, import"| node_storage
node_main -->|"registers; receives updates"| node_serviceworker
node_state -->|"validates moves"| node_board
node_state -->|"starts games"| node_generator
node_state -->|"checks solution state"| node_solver
node_storage -->|"persists resumable data"| node_localstorage
node_storage -->|"validates imported saves"| node_board
node_storage -->|"checks solution integrity"| node_solver
node_generator -->|"guarantees uniqueness"| node_solver
node_generator -->|"grades puzzles"| node_difficulty
node_generator -->|"uses seeded randomness"| node_random
node_solver -->|"uses constraints"| node_board
node_difficulty -->|"applies logical solving"| node_solver
node_serviceworker -->|"caches app shell"| node_staticcache
node_manifest -.->|"install metadata for"| node_html
node_tests -->|"verifies"| node_board
node_tests -->|"verifies"| node_solver
node_tests -->|"verifies"| node_generator
node_tests -->|"verifies determinism"| node_random

click node_html "https://github.com/dellalu/sudoku/blob/master/index.html"
click node_main "https://github.com/dellalu/sudoku/blob/master/src/main.js"
click node_styles "https://github.com/dellalu/sudoku/blob/master/src/styles.css"
click node_state "https://github.com/dellalu/sudoku/blob/master/src/game/state.js"
click node_storage "https://github.com/dellalu/sudoku/blob/master/src/game/storage.js"
click node_board "https://github.com/dellalu/sudoku/blob/master/src/engine/board.js"
click node_solver "https://github.com/dellalu/sudoku/blob/master/src/engine/solver.js"
click node_generator "https://github.com/dellalu/sudoku/blob/master/src/engine/generator.js"
click node_difficulty "https://github.com/dellalu/sudoku/blob/master/src/engine/difficulty.js"
click node_random "https://github.com/dellalu/sudoku/blob/master/src/engine/random.js"
click node_manifest "https://github.com/dellalu/sudoku/blob/master/manifest.webmanifest"
click node_serviceworker "https://github.com/dellalu/sudoku/blob/master/sw.js"
click node_tests "https://github.com/dellalu/sudoku/blob/master/tests/engine.test.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_html,node_main,node_styles toneBlue
class node_board,node_solver,node_generator,node_difficulty,node_random,node_tests toneAmber
class node_state,node_storage,node_localstorage toneMint
class node_manifest,node_serviceworker,node_staticcache toneRose
```

## Components

### Browser UI

- [App document](https://github.com/dellalu/sudoku/blob/master/index.html): HTML entry · Static browser entry point. · `index.html`
- [UI controller](https://github.com/dellalu/sudoku/blob/master/src/main.js): browser JavaScript · Renders controls and orchestrates player actions. · `src/main.js`
- [Mobile styles](https://github.com/dellalu/sudoku/blob/master/src/styles.css): CSS · `src/styles.css`

### Sudoku Engine

Pure puzzle logic independent of browser state.

- [Board constraints](https://github.com/dellalu/sudoku/blob/master/src/engine/board.js): board model · Represents and validates Sudoku grids. · `src/engine/board.js`
- [Solver](https://github.com/dellalu/sudoku/blob/master/src/engine/solver.js): puzzle solver · Solves grids and checks validity or uniqueness. · `src/engine/solver.js`
- [Puzzle generator](https://github.com/dellalu/sudoku/blob/master/src/engine/generator.js): puzzle generation · Creates puzzles with unique solutions. · `src/engine/generator.js`
- [Difficulty grader](https://github.com/dellalu/sudoku/blob/master/src/engine/difficulty.js): logical grading · Rates puzzles using human-style techniques. · `src/engine/difficulty.js`
- [Seeded random](https://github.com/dellalu/sudoku/blob/master/src/engine/random.js): deterministic RNG · Enables repeatable puzzle generation. · `src/engine/random.js`
- [Engine verification](https://github.com/dellalu/sudoku/blob/master/tests/engine.test.js): test suite · Verifies solving, generation, validity, uniqueness, and deterministic seeds. · `tests/engine.test.js`

### Game State & Saves

- [Gameplay state](https://github.com/dellalu/sudoku/blob/master/src/game/state.js): stateful game layer · Tracks play progress, options, and undo/redo. · `src/game/state.js`
- [Save storage & transfer](https://github.com/dellalu/sudoku/blob/master/src/game/storage.js): browser persistence · Validates imports and persists resumable games. · `src/game/storage.js`
- **Browser localStorage**: browser storage

### PWA Lifecycle

- [Install manifest](https://github.com/dellalu/sudoku/blob/master/manifest.webmanifest): PWA manifest · `manifest.webmanifest`
- [Service worker](https://github.com/dellalu/sudoku/blob/master/sw.js): offline cache · Caches the static application shell and manages updates. · `sw.js`
- **Static shell cache**: Cache Storage

## Connections

- App document → UI controller: loads
- App document → Mobile styles: loads
- UI controller → Gameplay state: applies player actions
- UI controller → Save storage & transfer: save, export, import
- UI controller → Service worker: registers; receives updates
- Gameplay state → Board constraints: validates moves
- Gameplay state → Puzzle generator: starts games
- Gameplay state → Solver: checks solution state
- Save storage & transfer → Browser localStorage: persists resumable data
- Save storage & transfer → Board constraints: validates imported saves
- Save storage & transfer → Solver: checks solution integrity
- Puzzle generator → Solver: guarantees uniqueness
- Puzzle generator → Difficulty grader: grades puzzles
- Puzzle generator → Seeded random: uses seeded randomness
- Solver → Board constraints: uses constraints
- Difficulty grader → Solver: applies logical solving
- Service worker → Static shell cache: caches app shell
- Install manifest → App document: install metadata for
- Engine verification → Board constraints: verifies
- Engine verification → Solver: verifies
- Engine verification → Puzzle generator: verifies
- Engine verification → Seeded random: verifies determinism
