tinyfiles.tv
⏱ 00:00 • ❌ Mistakes: 0/3 • Good luck
How to play

Click a cell, then tap 1-9. Turn on Notes to pencil in candidates. 3 mistakes = game over (you can still keep playing in practice, just hit New Puzzle). Keyboard works too — arrows + 1-9 + Backspace.

Backend  ·  Template · Proposed · Not connected

⬇ Backend — Sudoku Backend — Template

Right now Sudoku is local-only — the puzzle is generated in JS, moves / mistakes / timer live in the browser, and refreshing loses the game. This backend is the map to make it real: the server generates the puzzle, validates moves, and saves progress. Proposed, not connected.

01 / Files to create
api/sudoku/new.php          POST /api/sudoku/new  { difficulty }
api/sudoku/move.php         POST /api/sudoku/move { game_id, r, c, n }
api/sudoku/state.php        GET  /api/sudoku/state?game_id=...
api/sudoku/finish.php       POST /api/sudoku/finish { game_id }
api/sudoku/daily.php        GET  /api/sudoku/daily
sql/sudoku_tables.sql

Keep the tinyfiles PHP pattern: each endpoint loads /api/bootstrap.php, uses json_in() / json_out(), prepared PDO statements.

02 / Database tables
-- sql/sudoku_tables.sql
sudoku_puzzles
  id VARCHAR(40) PRIMARY KEY
  difficulty ENUM('easy','medium','hard') NOT NULL
  puzzle TEXT NOT NULL       -- 81 chars, 0 = empty
  solution TEXT NOT NULL     -- 81 chars, solved
  created_at DATETIME NOT NULL

sudoku_games
  id VARCHAR(40) PRIMARY KEY
  member VARCHAR(80) NULL    -- NULL = guest, else member
  puzzle_id VARCHAR(40) NOT NULL
  player TEXT NOT NULL       -- 81 chars, current board
  notes TEXT NOT NULL        -- JSON: per-cell candidate sets
  mistakes INT NOT NULL DEFAULT 0
  status ENUM('playing','won','lost') NOT NULL DEFAULT 'playing'
  started_at DATETIME NOT NULL
  solved_at DATETIME NULL
  FOREIGN KEY (puzzle_id) REFERENCES sudoku_puzzles(id)

sudoku_scores
  id INT AUTO_INCREMENT PRIMARY KEY
  member VARCHAR(80) NULL
  difficulty ENUM('easy','medium','hard') NOT NULL
  seconds INT NOT NULL
  mistakes INT NOT NULL
  solved_at DATETIME NOT NULL

Store the board as 81-char strings, not 81 rows — one row per game is enough to reload it.

03 / API endpoints & request flow
POST /api/sudoku/new
{ "difficulty": "easy" }  -- easy 40 removed, medium 50, hard 55
- server generates full board (same solveBoard logic as this page),
  removes cells by difficulty, saves puzzle + solution to sudoku_puzzles,
  creates sudoku_games row, returns { "ok": true, "game_id": "...",
  "puzzle": "530070000...", "mistakes": 0 }

POST /api/sudoku/move
{ "game_id": "...", "r": 0, "c": 2, "n": 4 }
- load game, if status != 'playing' return { "ok": false }
- if cell is given (puzzle[r][c] != 0) return { "ok": false, "error": "given" }
- if n == solution[r][c]: player[r][c] = n, mistakes unchanged
- else: player[r][c] = n, mistakes++, if mistakes >= 3 status='lost'
- if player == solution: status='won', solved_at=NOW(), insert into sudoku_scores
- save player + mistakes + status, return { "ok": true, "correct": true/false,
  "mistakes": 1, "status": "playing" }

GET /api/sudoku/state?game_id=...
- returns { "ok": true, "puzzle": "...", "player": "...",
  "notes": {...}, "mistakes": 0, "status": "playing" }
- never returns solution until game is won / lost

GET /api/sudoku/daily
- returns today's puzzle (same puzzle for everyone, seeded by date)

Server is the checker. Browser shows red for wrong cells, but only the server decides win / loss / mistakes.

04 / Puzzle generation (server owns this)
// Same logic as solveBoard() + isValid() in this page, in PHP:
// 1. Fill 9x9 with backtracking, random order 1-9
// 2. Copy to solution
// 3. Shuffle all 81 cells, remove 40 / 50 / 55 by difficulty
// 4. Optional: verify uniqueness — re-solve counting solutions,
//    if more than 1 solution, try a different removal set
// 5. Save puzzle + solution, never send solution to browser
//    until the game is over

Keep generation on the server so Daily is the same for everyone and the solution is never in the page source.

05 / Frontend wiring
// Replace newGame(diff) in this page:
async function newGame(diff){
  const res = await fetch('/api/sudoku/new', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ difficulty: diff })
  });
  const data = await res.json();
  // data.game_id, data.puzzle -> build player / given / notes from it
  // keep game_id in JS for move / state calls
}

// Replace placeNum(n):
async function placeNum(n){
  // optimistic: show the number, then
  const res = await fetch('/api/sudoku/move', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ game_id: gameId, r, c, n })
  });
  // if data.correct === false, mark red + update mistakes
  // if data.status === 'won', show "You solved it!"
}

// On load: if localStorage has game_id, GET /api/sudoku/state
// to resume. If API fails, fall back to current local newGame().

Keep the local game playable with no backend — if /api/sudoku/new fails, use the current in-page generator.

Copy the snippets above into api/sudoku/ + sql/sudoku_tables.sql — no GitHub needed. The <pre> blocks are the backend spec: server-generated puzzles, server-checked moves, saved games.