# Worked port — one game, before and after

A normal browser game, and the same game able to carry a match. Run the proof:

```bash
node kit/worked-port/prove.mjs
```

Measured output, ten replays of one recorded input trace on each version:

```
BEFORE  scores 950, 884, 1000, 683, 535, 751, 950, 641, 950, 900
        8 different results from one recording

AFTER   score 614 every time
        trace 1681d1ed689e8f5c
        1 distinct result across ten runs
```

Nothing is wrong with the before version. It is how most HTML5 games are written,
and it works fine until somebody has to check who won.

## The five changes

| # | Before | After |
|---|---|---|
| 1 | `Date.now()` in the loop | gone — the frame number is the clock |
| 2 | `dt = (now - last) / 1000`, physics scaled by it | fixed step; a frame is a frame on every machine |
| 3 | `Math.random()` for spawns | one seeded stream, replayed identically |
| 4 | float pixel positions | integers, fixed point at 16 units to the pixel |
| 5 | no state hash | `stateHash()` published every frame |

None of it is a rewrite. `after.js` is the same game, the same feel, about the same
number of lines.

## Why each one matters

**The clock (1, 2).** A variable timestep means a 144 Hz machine and a 60 Hz machine
compute different physics from identical inputs, and a frame that stalls while the OS
does something else changes the result. Nobody can re-run that; it is not the game's
fault, it is the timestep's.

**Ambient randomness (3).** `Math.random()` is a different sequence every run and
nothing records what it was, so the run cannot be reproduced even on the same machine
a second later. One seeded stream costs nothing and makes the luck part of the match
rather than part of the environment.

**Floats (4).** Floats are allowed on the platform and NaN payloads are canonicalised
at the boundary. Integers are still the version nobody has to think about — especially
when positions accumulate over hundreds of frames, where the order of operations starts
to matter.

**The state hash (5).** Without it, a divergence is "these two runs disagree somewhere
in nine hundred frames". With it, a divergence is a frame number. It is the difference
between a dispute that resolves in seconds and one that needs a person.

## What you get for it

- **Replay.** The recording is the whole match. Rendering it back costs nothing.
- **Verification.** Anybody can recompute the score from the seed and the inputs, so
  nobody has to be taken at their word — including the platform.
- **Async matches.** Player one posts a run, player two answers it an hour later. No
  two people have to be online at the same moment, which is the cold-start problem
  most of these platforms die of.
- **A dispute that costs one frame of work** instead of a screenshot argument.

## Then

Compile it to WASM against the four host functions (see `../template.wat`), or keep it
in JS and register it that way. Either path, drop the build into `../check.html` and
the same three gates run on your machine before anybody else sees it.
