"The puzzle is a partially erased solution."
That’s the secret behind all logic puzzle generators. In this case, the trick is the Hamiltonian Path algorithm.
This is an interactive puzzle game inspired by LinkedIn's daily puzzle challenges. Connect numbered checkpoints in sequence while filling every cell to create a perfect Hamiltonian path!
At the heart of this puzzle lies sophisticated graph theory and optimization algorithms that ensure every puzzle is solvable and interesting.
A Hamiltonian path is a path in a graph that visits each vertex exactly once. In our grid-based puzzle:
- Each cell is a vertex
- Adjacent cells (up/down/left/right) are connected by edges
- The solution is a Hamiltonian path through the grid graph
Why Hamiltonian Paths?
- Guarantees every cell is visited exactly once ✓
- Creates challenging puzzles with a single valid solution ✓
- Classic NP-complete problem - no polynomial-time algorithm exists for arbitrary graphs ✗
For a 7×7 grid (49 cells), there are potentially billions of different Hamiltonian paths, making each puzzle unique!
Finding a Hamiltonian path is computationally expensive (NP-complete). We use Warnsdorff's heuristic to generate paths efficiently:
The Algorithm:
- Start from a random cell
- At each step, move to the unvisited neighbor with the fewest onward options
- This greedy approach avoids "dead ends" early in the path
- Combined with backtracking (DFS) when stuck
Implementation (src/core/hamilton.ts):
// Warnsdorff heuristic: sort neighbors by fewest onward moves
const neighbors = shuffleArray(getUnvisitedNeighbors(cell, width, height, visited));
neighbors.sort((a, b) => {
const aMoves = getUnvisitedNeighbors(a, width, height, visited).length;
const bMoves = getUnvisitedNeighbors(b, width, height, visited).length;
return aMoves - bMoves; // Prefer cells with fewer options
});Why Warnsdorff's Works:
- Originally developed for the Knight's Tour problem
- Prioritizes moves that don't paint us into corners
- Dramatically reduces search space
- Typically finds solutions in milliseconds for our grid sizes
| Grid Size | Cells | Avg Generation Time | Success Rate |
|---|---|---|---|
| 5×5 | 25 | <10ms | ~99% |
| 7×7 | 49 | 50-200ms | ~95% |
| 9×9 | 81 | 200-1000ms | ~85% |
Fallback Strategy: If Hamiltonian path generation fails after 100 attempts or times out (5 seconds), we use a snake pattern fallback:
- Guarantees a valid path in O(n) time
- Zigzag pattern: →→→ ↓ ←←← ↓ →→→
- Always solvable but more predictable
- Time Complexity: O(n!) worst case, but Warnsdorff's heuristic reduces average case to ~O(n²)
- Space Complexity: O(n) for recursion stack and visited set
- Randomization: Shuffling neighbors before sorting adds variety while maintaining heuristic benefits
- Smart Backtracking: Single-click to undo one step, double-click for larger backtracks
- Touch & Mouse Support: Fully responsive with smooth touch interactions for mobile devices
- Random Checkpoint Placement: Every puzzle is unique with unpredictable checkpoint positions
- Auto-start at Checkpoint #1: Path automatically starts at the first checkpoint for better UX
- Helpful Hints: Friendly messages guide you when you fill all cells without ending at the last checkpoint
- Solution Viewer: Stuck? View the solution with a single click
- Beautiful UI: Clean indigo color scheme with smooth animations and modern design
- Loading Screen: No FOUC (Flash of Unstyled Content) - smooth page load experience
- Start at checkpoint #1 (automatically highlighted when the game loads)
- Draw a path by clicking or dragging through adjacent cells
- Connect numbered checkpoints in order (1 → 2 → 3 → ...)
- Fill every cell exactly once
- End at the last checkpoint to win!
- Click & Drag: Draw the path through cells
- Single-Click on second-to-last cell: Quick undo (remove last cell)
- Double-Click on any path cell: Backtrack to that position
- "Show Solution" button: View the correct path
- "Play another" button: Generate a new puzzle
- Node.js (v14 or higher)
- npm or yarn
# Clone the repository
git clone <repository-url>
# Navigate to project directory
cd zip-puzzle-game
# Install dependencies
npm install
# Start development server
npm run dev
# Build for production
npm run buildThe game will be available at http://localhost:5173
- TypeScript - Type-safe development
- Vite - Lightning-fast dev server and build tool
- HTML5 Canvas - High-performance rendering
- Vanilla CSS - Custom styling without frameworks
- Hamiltonian Path Algorithm - Puzzle generation logic
zip-puzzle-game/
├── index.html # Main HTML entry point
├── src/
│ ├── main.ts # App initialization & game loop
│ ├── style.css # Styling and animations
│ ├── assets/ # SVG icons and static assets
│ ├── core/ # Game logic
│ │ ├── grid.ts # Grid utilities
│ │ ├── hamilton.ts # Hamiltonian path generation
│ │ └── puzzle.ts # Checkpoint selection (randomized)
│ ├── render/ # Canvas rendering system
│ │ ├── canvas.ts # Main renderer
│ │ └── draw.ts # Drawing utilities
│ └── input/ # User input handling
│ └── mouse.ts # Mouse & touch events
└── CODE_DOCUMENTATION.md # Comprehensive code documentation
The current configuration uses:
- Grid Size: 7×7 (49 cells)
- Checkpoints: 8 randomly-placed numbered dots
- Completion Time: ~2-5 minutes per puzzle
- Fast Undo: Single-click on the second-to-last cell removes just one cell
- Controlled Backtrack: Double-click any earlier path cell to remove everything after it
- Drag Protection: Accidentally dragging over existing path cells won't remove them
This prevents accidental path removal while maintaining quick editing capabilities!
This project was inspired by the LinkedIn Puzzle Game, which features daily brain-teasing challenges.
For detailed code architecture and implementation details, see CODE_DOCUMENTATION.md
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Submit pull requests
- Improve documentation
MIT License - feel free to use this project for learning or your own implementations!
- LinkedIn Puzzle Game for the original inspiration.
- The web development community for excellent resources on Canvas API and algorithms.
Enjoy the game! 🧩
