Slide, join, and keep doubling until you reach 2048
← Back to Fun TimeThis game is split into small library files inside funtime/lib/, and every function has a comment saying exactly what goes in, what comes out, and the algorithm in plain English. Empty any function out, read its comment, and write it yourself.
| File | What it does | Good functions to try re-writing |
|---|---|---|
| lib/twenty48-rules.js | The rules: sliding, joining, turning | slideRow, mergeRow, rotateBoard, moveBoard |
| lib/twenty48-draw.js | Painting the tiles and their numbers | tileLook, drawTile, renderGame |
| lib/twenty48-main.js | The glue: the arrow keys and finger swipes | doAction, connectSwipe, setUpGame |
💡 The big idea: write one move, not four. Sliding left is the only direction this game really knows; the other three are done by turning the board round, sliding left, and turning it back. One function to get right, one place for a bug to hide.
🛠️ Want to be walked through it? Build 2048 Yourself takes you through the key functions one step at a time and tests each one you write. There is a Python version too.