Sift Stack is a small sand puzzle on JumpBud: you drag a block onto the board, it melts into sand, and any single colour that reaches from the left wall to the right wall sifts away. Keep the pile below the dashed line and you keep playing.
The whole simulation is two functions in plain JavaScript and three typed arrays. No physics engine, no WebGL. This post walks through both functions and the numbers I got when I ran them in Node.
The board is a byte array
The canvas is 360 px wide, but the game never thinks in pixels. Each grain is a 6×6 pixel square, so the board is 60 columns by 80 rows: 4,800 cells.
const COLS = 60, ROWS = 80; const BLOCK = 6; // grains per block edge const grid = new Uint8Array(COLS * ROWS); // colour index, 0 = empty const flash = new Float32Array(COLS * ROWS); // > 0 while a cleared grain fades A piece is made of blocks, and each block becomes 6×6 = 36 grains when you pour it. A 2×2 square drops 144 grains in one go. Drawing is equally cheap: the game writes one pixel per grain into a 60×80 ImageData and scales it up, so the browser does the "pixel art" part for free.
Part 1: fall, slide, slump
step() walks the grid bottom-up, so a grain that moves this pass is not moved again by the same pass. For each grain it tries three moves in order:
if (!grid[below]) { move(i, below); continue; } // 1. straight down for (const d of [dir, -dir]) { if (!grid[below + d] && !grid[i + d]) { move(i, below + d); break; } // 2. diagonal const far = x + 2 * d; if (far >= 0 && far < COLS && !grid[i + d] && !grid[below + 2 * d]) { move(i, i + d); break; // 3. slump sideways } } Moves 1 and 2 are the textbook falling-sand automaton. Move 3 is the one that makes it feel like sand instead of a stack of marbles: if the cell two columns over is free one row down, the grain shuffles one cell sideways now and falls next pass.
Two details stop the sand from drifting to one side:
- The column order flips every pass (
sweepFlip), left-to-right then right-to-left. - The first diagonal tried (
dir) depends on(x + y + flip) & 1, so neighbours lean opposite ways.
What the slump move changes
I poured six 2×2 squares (864 grains) at the same spot and measured the height of every column after the sand stopped moving.
| Peak height | Columns covered | Slope | |
|---|---|---|---|
| Fall + diagonal only | 28 rows | 58 of 60 | 1 row per column (45°) |
| With slump | 21 rows | 60 of 60 | 1 row per 2 columns (~27°) |
Without slumping, the pile is a sharp triangle that rises one row every column. With it, the pile spreads to both walls and climbs half as fast. That matters for gameplay: a gentler slope means colours spread wider, which is what you need to link both walls. It also means the pile reaches the danger line later.
Each pour settled in 51 to 73 game frames (two step() calls per frame), a bit over one second at 60 fps. Long enough to watch, short enough not to wait for.
Part 2: wall-to-wall in one flood fill
The clear rule sounds expensive: "find every connected group of one colour, and remove the groups that touch both walls". The trick is that every clearing group must touch the left wall, so the search only starts from column 0:
for (let y = 0; y < ROWS; y++) { const start = y * COLS; // left-wall cell const c = grid[start]; if (!c || seen[start]) continue; // iterative flood fill over 8 neighbours with the same colour // ...set touchesRight if any member has x === COLS - 1 if (touchesRight) for (const i of members) flash[i] = .45; } That gives at most 80 seeds, a shared seen array so no cell is visited twice, and a preallocated Int32Array as the stack instead of recursion (a full-width colour can be 2,000+ cells, too deep for comfortable recursive calls).
The fill uses 8 neighbours, so grains touching only at a corner still count as linked. With 6-pixel grains a corner touch is visible on screen, so counting it matches what the player sees.
Cleared grains don't vanish at once. They get flash = 0.45 seconds, keep their place while fading, and step() skips them. When the fade ends they become empty and everything above falls in. If that fall forms a new wall-to-wall link within 2.2 seconds, it scores as a chain: ×1.5, ×2 and so on.
How fast is it?
Timings from Node, averaged over thousands of runs:
-
step()on a half-full board: 0.038 ms. The game runs it twice per frame. -
findClears()when the bottom half is one solid colour (2,400 cells, worst case): 0.065 ms. -
findClears()on a mixed three-colour board: 0.003 ms, because most left-wall seeds stop after a few cells.
The game only checks for clears every 80 ms rather than every frame. Speed is clearly not the reason; a side effect is that freshly poured sand gets a moment to land before it can sift.
Is it too hard?
To check the starting difficulty, I ran 200 seeded games with the game's own PRNG (mulberry32), pouring random 2×2 squares in 3 colours at random spots, and counted pours until the first sift. Median: 6 pours, fastest 3, slowest 22. A player who aims should do better than random drops. The fourth colour arrives at 1,500 points and the fifth at 4,000, which is where it gets tight.
Takeaways
- A 60×80 byte grid is plenty for a sand game on a phone, and simple to reason about.
- One extra "slump" move turns a 45° spike into a 27° pile, and that one rule changes how the whole game plays.
- When a rule says "touches both sides", start the search from one side.
You can try it in the browser, no download: play Sift Stack on JumpBud.
