How a headless, deterministic engine simplified almost every feature in my game
A Wizard-Chess Tournament started as a hotseat prototype: two people on one screen, just to see if chess with HP, attack values and spell cards actually worked. I had no plans for networking yet, so the only reason I made the engine headless and deterministic from day one was to keep the rules from turning into a mess.
With stacking buffs, delayed effects and traps on squares, two effects hitting the same piece can give different results depending on which ran first. If that order could wobble, I'd never know a balance problem from a bug, and unit tests wouldn't be reliable. So from the first commit, effects run in strict application order, all randomness goes through one seeded RNG, nothing touches the clock or hash iteration order, and the engine runs without the UI and reports changes through signals. Given the same starting state and inputs, it always produces the same game.
At the time those were just defensive habits for a rules-heavy game. Once the project grew past the prototype stage, they ended up powering almost every feature I built next.
Previews: run the real engine on a copy
The first payoff came while the game was still hotseat only. I wanted hovering over a move or a card to show what would happen: how much damage, whether the attacker bounces back, who dies. The obvious way is a second, simplified rules calculation for the preview, and that's exactly the code that drifts out of step with the real rules the first time a card does something new.
Because the engine is deterministic and doesn't need the UI, the preview instead deep-copies the game state, runs the real move or card on the copy with a signal bus nobody listens to, and compares the copy with the live game. Whatever the real engine would do, the preview shows, including interactions I never thought to special-case.
LAN play, with online play in mind
LAN play was the next step, and on its own it wouldn't have needed much thought. Two machines on the same network can talk to each other directly. But I knew online play would follow, and I didn't want to be running and paying for a game server when that time came. A server that knows the rules has to be hosted, kept up and updated alongside every balance patch. So while building LAN, I was really looking for a design that would carry over to the internet with nothing in the middle that had to understand the game.
The answer was lockstep. The two machines never send each other the board. They send each other the inputs (this move, this card on that piece, a pass, a resign), and each machine applies them to its own copy of the game. Because the engine was already deterministic, both copies stay identical with no extra work. Neither machine is the authority, and no third machine is needed.
Lockstep only works because of two other properties of the design. The game is turn-based, so nobody needs to see the other player's action within milliseconds. And both hands are shown face-up to both players, so there's no hidden information that one machine would have to keep from the other. The one thing left to agree on is the random seed. Both machines contribute to it through a commit-reveal exchange, so neither player can pick a seed that gives them a good draw.
To catch the case where determinism breaks anyway, every turn each machine hashes its whole game state and the two compare hashes. If they ever disagree, the match stops with a "desync" and writes a small report, rather than letting two players continue in two different games. Clocks are the one deliberate exception: they're UI-only and live outside the engine, so when a flag falls it reaches the engine as an input, the same way a resign does.
Reconnecting after a dropped connection reuses the same pieces. The machine that stayed sends a snapshot of its state, the returning one loads it, checks the hash, and play carries on.
When online play did come, the plan held. Steam online matches run on the same lockstep core behind a transport layer, carried over Steam's own networking, and the only part that changed was how the bytes travel. There's still no game server of mine in that picture.
Match logs: a game you can re-run
If a match is fully described by a starting state plus a list of inputs, then writing those down gives you the whole match in one small file. The game saves every match as one line of JSON: the settings, the starting state and its hash, then the input stream with a state hash after each move. It saves after every action, so a crash mid-game doesn't lose it.
The first thing built on those logs was the puzzle leaderboards. Puzzle mode scores a solve by how efficiently you win, and the score uploaded to Steam is the one the engine arrives at when it re-runs your match log from the authored puzzle position, rather than a number the game wrote down along the way. If the log doesn't replay to a win from that exact position, nothing is submitted.
Replays
The replay viewer came in October, and it was the clearest case of a feature that was mostly already built. A replay is the real engine rebuilt from a match log and fed its inputs again. It isn't a recording of what was on screen.
Stepping forward applies the next input on the live board, and the board animates it exactly the way it animates an opponent's move in an online game. The UI code didn't need to know the difference. Stepping backward is the one thing the engine can't do directly, since it has no undo, so it rebuilds the game from the start of the log up to the chosen point, silently on a bus nobody listens to, and swaps the result in without animating. The validator that checks a log and the viewer that plays it share one code path, so they can't disagree about what a log means.
The new work was the browser page, the timeline, a mode flag so that watching a replay's ending doesn't count as winning a match again, and sharing: a replay can be copied to the clipboard as a compact text code and pasted on another machine. That last part meant treating pasted text as untrusted input, so imported logs go through the same validation as a snapshot from a network peer.
The same logs now travel with bug reports. The in-game feedback window can attach one replay (with anything identifying stripped out), so a report arrives as a game I can step through.
Achievements: watching without touching
Achievements needed to know things like how a game was won, which the signals alone didn't say. They're built as observers that the engine calls at fixed points during each action, the same hook the match log recorder uses. The rule for an observer is that it only reads. It never changes the state, never draws a random number and never reorders anything. There's a test that plays a match with and without observers and checks the results are bit-identical, because an achievement tracker that changed the game would also cause desyncs online.
Testing: games that play themselves
Determinism is also what makes the test suite work. There are around 170 test suites now, and a lot of them lean on the fact that a seed plus a list of actions always produces the same game. One test plays a fixed seed and action list and checks the final state hash against a known value, so any change that quietly shifts the random draws or the order effects resolve in shows up immediately.
The multiplayer fuzz tests go further. Two complete copies of the game run side by side in one process, each acting as one player, and play random legal actions against each other: maybe a card, maybe a discard, then a move. After every action, the two game states have to hash the same. A higher-volume version skips the UI and the network sockets entirely, so it can play a lot of games quickly, and it picks a fresh random seed every run to find paths the fixed tests never take. When one of those games fails, it prints the seed, and because the engine is deterministic that seed replays the exact failing game. A random failure becomes a bug I can step through. Another fuzz test hammers the real game screen with random hovers and clicks while a game plays underneath, and that one found a real crash: hovering over the board at the wrong moment could send the preview code a selection that no longer existed.
Tuning the AI by replaying its mistakes
The computer opponents use the same machinery. The Tournament AI, the one that plays full wizard matches with cards, scores a candidate card or move the same way previews work: it copies the game, plays the candidate and a few turns of both sides forward with the real engine, and looks at what's left. That way it values every card by what the card actually does, and I never had to hand-price each effect for it.
Finding out why it lost uses the same trick as the replay viewer. Any game can be rebuilt to any turn, so when the computer threw a game away I could jump to the exact move where it went wrong and ask the current version of the AI what it would play there now, along with the full breakdown of which part of its evaluation pushed it that way. A fix gets checked the same way: rebuild the old bad positions and confirm the AI now picks something better. The AI also plays itself over a fixed set of seeds after every change to its evaluation, to check that its turns haven't slowed down.
The classic-chess difficulty levels got a similar treatment. A gauntlet tool plays the engine's difficulty settings against each other through the game's real chess rules and measures the strength gap between each pair, which is how the levels get spaced out instead of guessed.
Async play: a server that doesn't know the rules
The most recent feature leaned on all of this at once. Asynchronous play lets two people play one move a day if they want. That needs something to hold a turn while the other player is offline, so it's the one place I ended up hosting anything: a small relay on Cloudflare. I wrote about the server side in the async devlog. The short version is that the relay doesn't run the game rules at all. It stores each player's turn as an envelope of inputs and passes it along.
When you open an async match, your client rebuilds the whole game from those envelopes through the real engine, using the replay code, and the opponent's latest turn animates on the board the same way a replay step does. State hashes check that both sides agree. Keeping the rules out of the server is a big part of why it fits in the free tier.
What it costs
Determinism does have costs. Every new piece of game state has to go into both the code that copies the state for previews and the code that serializes it for snapshots and logs, or previews and multiplayer quietly go wrong. Anything random has to come from the one seeded generator. Anything that feels harmless, such as iterating a dictionary to apply effects, has to be checked for order dependence.
The bigger cost shows up with updates. A match log is only meaningful against the rules and cards it was played with, so every log carries a fingerprint of the game content. When a balance patch changes a card, logs from before the patch can't replay any more, and the replay browser shows them greyed out. For finished games that's an acceptable loss. For async matches, which can last weeks and span an update, it isn't. So each async turn also carries a snapshot of the game state, and the client knows which released versions came before its own. When a match spans an update, the client starts from the snapshot of the last turn played on the older version and replays only the turns after it with the new rules.
Looking back
Determinism started as a habit to keep card effects and tests predictable in a hotseat prototype. Lockstep was the reason it became load-bearing, because I wanted online play without a server. What I didn't expect was how many later features would reduce to the same three steps: take a starting state, apply a list of inputs with the real engine, compare hashes. Previews, multiplayer, reconnect, leaderboards, replays, fuzz testing, AI tuning and async all ended up as variations on that, and each one was a lot smaller than it would have been with its own copy of the rules.
You can see all of this at work in the free demo of A Wizard-Chess Tournament on Steam. If you have questions about the engine, or you're building something with similar rules, come and find me on Discord.