# Tetris
A browser-based Tetris game built entirely by autonomous AI coding agents using [OpenSpec](https://github.com/fission-ai/openspec) for specification and [Fabro](https://github.com/fabro-ai/fabro) for implementation.

## How It Was Built
This project was built without a human writing any game code. The entire pipeline — from specification to implementation to testing — was orchestrated by AI agents:
1. **Specification** — [OpenSpec](https://github.com/fission-ai/openspec) defined the game's requirements: all 7 tetrominoes, SRS rotation, scoring, level progression, hold/next mechanics, and 4 game states (Waiting, Playing, Paused, Game Over)
2. **Implementation** — [Fabro](https://github.com/fabro-ai/fabro) orchestrated a multi-model pipeline to build the game ([view workflow](docs/generic-build-workflow.png)):
- Dual-model planning (two models independently create plans, then synthesize)
- Code generation from OpenSpec specs
- Automated code review against specs
- Verification loops (typecheck, build, structure check)
- Final UI/UX polish pass

3. **Testing** — Browser-based UI testing using AI agents that simulate a real human ([view workflow](docs/browser-testing-workflow.png)):
- First tested with [agent-browser](https://agent-browser.dev) (CDP-based CLI)
- Then switched to [Vibium](https://github.com/VibiumDev/vibium) (WebDriver BiDi-based CLI) for richer verification: annotated screenshots, `diff map` for change detection, semantic element finding, and state assertions
- 3 parallel test suites run concurrently: visual verification (6 tests), gameplay controls (12 tests), scoring & progression (10 tests)
- Tests are written in plain English with embedded CLI commands — no Playwright scripts

## Screenshots
| Waiting | Playing | Game Over |
|---------|---------|-----------|
|  |  |  |
## Tech Stack
- **Language:** TypeScript
- **Build:** Bun
- **Rendering:** Canvas 2D (no dependencies)
- **Output:** Single `dist/game.js` (23 KB) + `index.html`
## Game Features
- All 7 tetrominoes with Tetris Guideline colors
- Super Rotation System (SRS) with I-piece and JLSTZ wall kick tables
- 7-bag randomizer (Fisher-Yates shuffle)
- Hold piece (once per placement)
- Ghost piece preview
- DAS/ARR input handling (170ms delay, 50ms repeat)
- Scoring: soft drop (1pt/cell), hard drop (2pt/cell), line clears (100/300/500/800 x level)
- Level progression (every 10 lines, gravity speeds up)
- High score persistence via localStorage
- HiDPI canvas support
- Responsive layout
## Controls
| Key | Action |
|-----|--------|
| `← →` / `A D` | Move |
| `↑` / `W` | Rotate CW |
| `Z` | Rotate CCW |
| `Space` | Hard Drop |
| `↓` / `S` | Soft Drop |
| `C` | Hold |
| `P` / `Esc` | Pause |
## Run Locally
```bash
bun install
bun run build
open index.html
```
## Run Browser Tests
The browser testing workflow uses Vibium to validate all screens and interactions:
```bash
fab run browser-testing
```
Results are saved to `test-results/YYYY-MM-DD_HH-MM-SS/` with screenshots, session recordings, and markdown reports.
## Architecture
```
src/
state.ts # GameState enum, constants, types
pieces.ts # 7 tetrominoes, colors, SRS rotation tables, 7-bag randomizer
playfield.ts # 10x20 grid, collision, locking, line clear, ghost, rotation
input.ts # Keyboard events, DAS/ARR, action queuing
scoring.ts # Score calculation, level progression, gravity
storage.ts # localStorage high score persistence
renderer.ts # Canvas 2D rendering (playfield, panels, HUD, overlays)
main.ts # rAF game loop, state machine, module integration
```
## AI Agent Pipeline
```
OpenSpec (what to build)
→ Fabro generic-build (how to build it)
→ Bootstrap → Plan → Implement → Review → Verify → Polish
→ Fabro browser-testing (does it work?)
→ Build → Serve → [Visual | Controls | Scoring] → Report
```