Gokigen Naname
A browser-based implementation of the Gokigen Naname logic puzzle with on-device puzzle generation, a global leaderboard, and GitHub/Google sign-in. Built with React, TypeScript, and Cloudflare Pages.
A web app for playing Gokigen Naname — a grid-based logic puzzle where you fill every cell with a diagonal line (╲ or ╱) such that the number clues at each corner are satisfied and no loops form anywhere on the board.
How it works
Puzzle generation
Puzzles are generated entirely in the browser, off the main thread via a Web Worker. The generator:
- Fills a blank board with a random valid solution using backtracking and a seeded RNG, with a Union-Find structure enforcing the no-loops constraint at every step.
- Stamps all node clues from the completed solution.
- Iteratively removes clues in random order, keeping a removal only if the puzzle still has exactly one valid solution — verified by running the solver again and counting to two.
The result is a minimal clue set that uniquely determines the solution. Grid size (4×4 to 10×10) and difficulty (Easy / Medium / Hard) are configurable; difficulty controls how aggressively clues are removed.
Solver
The backtracking solver fills cells in row-major order, trying each diagonal direction and pruning branches early via constraint propagation — checking for clue overflows, unreachable clues, and loop formation after every placement.
Loop detection
Two separate algorithms handle loops. During generation and solving, Union-Find (path-compressed, near O(n)) detects any loop instantly. During gameplay, a DFS over the node graph identifies exactly which cells are part of a loop so they can be highlighted in real time.
Leaderboard
Completed solves are submitted to a Cloudflare Pages Function backed by a D1 (SQLite) database. Scores use adjusted time — elapsed seconds plus a 10-second penalty per hint used — so raw speed and clean solves are both meaningful. Sign in with GitHub or Google to track personal bests across devices; anonymous play is supported from the first visit with no friction.
Screenshots