# brainrot 011: tetris that waits for the tetris Live: https://brainrot.runfridge.dev/011/ Source: https://github.com/hwhang0917/brainrot/tree/main/011 License: MIT (see LICENSE at repo root); attribution only, no warranty. ## What it is Full-screen Tetris played by a bot that stacks every piece flat and clean, keeps the centre column open as a well, parks an I piece in hold, and only when the stack is about to reach the top drops the I down the well for a four-line TETRIS (big banner plus a screen shake). If it tops out, the stack greys, GAME OVER shows for 3 s and a fresh board follows. "I want to play" hands you the piece: arrows move/rotate/soft drop, Z rotates back, X rotates, space hard drops, C/Shift holds; on touch devices a button pad appears. A speed slider (1-10, default 6) scales the bot's moves and, for a person, gravity (1.2 s per row at 1, 0.12 s at 10). A "?" tooltip lists the controls. No sound. ## How it was built - Rendering: pixi.js v8, a single `Graphics` redrawn each frame: grid, tinted well column, bevelled blocks, ghost piece at 22% alpha; cleared rows flash white for 0.22 s. Banners and shake are CSS. - Board: always 20 rows; columns = max(10, round(20 * innerWidth / innerHeight)), so it fills the screen. Sized once at load; resize only rescales cells. - Model (`tetris.ts`): pieces generated by rotating the seven base shapes and deduplicating (I/S/Z have 2 rotations, O has 1); 7-bag randomiser; `fits`, `dropY`, `lock` (false = lock out above the top row), `fullRows`, `clearRows`, `columnHeights`. - Bot (`plan`): tries every rotation and column, scores the resulting board with classic heuristics (aggregate height -0.5, holes -2.5, bumpiness -0.35, max height -1.2, well use -20), and never lets a piece straddle the well. Hold logic: save an early I; once `tetrisTime` (stack within 6 rows of the top) fetch the I from hold and drop it vertically down the well. In danger (within 3 rows) it abandons the well and switches to a survival score (lines cleared, lowest top). - main.ts runs a small phase machine: spawn -> rotate -> move -> drop -> clear (bot), or `manual` with gravity and a 0.5 s lock delay reset by each successful move; `over` -> new game. Rotation uses simple column kicks [0, -1, 1, -2, 2]. Hold is spent once per piece by bot or person. - History: first commit was the bot-only autoplay; then the move from /11/ to /011/; then "I want to play" with keyboard/touch controls and the tooltip; then GAME OVER on top-out (greyed board, fresh game) instead of continuing; then a fix where the bot spun forever on an I after the person had spent the hold (`plan` now takes `canHold`). Vibe coded: prompted into existence with a coding agent, iterated commit by commit. ## Files - 011/index.html: page shell, inline CSS (shake keyframes, TETRIS/GAME OVER banner, touch pad shown only on html.manual without a fine pointer), controls tooltip, play button, speed slider. - 011/main.ts: pixi setup, board fit, phase machine and timings, manual controls (keyboard + #pad buttons via data-key), drawing, HUD (who plays, pieces, lines, tetrises, game, hold); imports `mountCrt` from lib/crt.ts and `mountHud` from lib/hud.ts. - 011/tetris.ts: pure model and bot: shapes/PIECES, board ops, `bag`, `wellColumn`, `stackHeight`, `tetrisTime`, `plan`. - 011/tetris.test.ts: rotation counts, bag deals seven, well kept empty and early I held, last-moment I clears four lines, survival J down the well near the top, lock out, bot places the I when hold is spent. ## Extending it - Run: `npm install && npm run dev`, open http://localhost:5173/011/ . Tests: `npm test`. Typecheck+build: `npm run build`. - Knobs: timings, colours (COLORS indexed by piece id) and `gravitySeconds` at the top of main.ts; ROWS, TETRIS_ROWS, DANGER_ROWS, TETRIS_HEADROOM and the score weights in tetris.ts. - Invariants: `plan` must respect `canHold` or the bot loops on hold; `plan` must skip placements resting above row 0 (that is a top-out); the well column is `floor(cols / 2)` and is only entered by the I at tetris time or in danger. Board cell value = piece id (1-7), 0 empty. - Tests build boards by hand with `createBoard` and fill rows, then call `plan`; add a scenario test like those when changing bot behaviour. - Keyboard input goes through `control(key)`; the touch pad sends the same key strings via `data-key`, so add a binding in both places. - Conventions: keep pure simulation in its own module with a `node --test` file beside it; match the surrounding code style; Conventional Commits `type(011): message`.