2048 — written in Mere

A tiny 2048 compiled from Mere to WebAssembly. The board is drawn as text into a <pre> via dom_set_text, and the arrow keys drive it through dom_on_key — a keyboard binding added to contrib/dom for this game. Turn-based, so no game loop or timer is needed; each keypress runs one move in Mere.

Use the arrow keys to slide tiles. Merge equal tiles to reach 2048. (Click the board first if the keys don't respond.)

loading…

What's happening

  1. mere -w g2048.mere lowers the game — a 16-slot vec board, the slide/merge row logic, a tiny LCG for tile spawns — to WebAssembly.
  2. dom_on_key registers a global keydown listener; the pressed key name ("ArrowLeft", …) is passed to a Mere closure that applies one move, spawns a tile if the board changed, and redraws the <pre>.
  3. No randomness FFI is used: tile spawns come from a pure-Mere linear-congruential generator seeded from a mutable vec slot, so the game is fully self-contained in the Wasm module.