Jack Tricker / Writing

Spire Network 1/5: I rewrote Slay the Spire in Rust so a neural network could play it

Jack Tricker, 28 October 2026


Part 1 of 5.

I wanted to see whether a neural network could learn Slay the Spire by playing it, with nobody telling it what a good deck looks like. That takes millions of games. The real game is a Java desktop program with animations, and a run takes a person the better part of an hour, so the first job was a copy of the game's rules that runs with no screen, as fast as the processor allows.

The result is spire-engine: the rules in Rust, four trained networks, and a site where you can watch them play or play yourself with a network's advice. It's an unofficial fan project with none of the game's art, music or text in it. If you like the game, buy it.

Reading the game

The game is written in Java, and Java decompiles well. Vineflower turned the December 2020 build into 2,010 readable source files. Those files were the reference for what every card, relic and monster does. The rules were then written in Rust, and the tables of card, relic and monster numbers were extracted from the reference by script.

Thirteen streams of random numbers

Slay the Spire is seeded: the same seed and the same choices give the same run. I wanted the engine to keep that property against the real game, so that anything played in the engine could be replayed in the real thing and compared.

The game keeps 13 separate random number streams. Seven are seeded once per run (monsters, events, the merchant, card rewards, treasure, relics and potions), five are reseeded on every floor, and one draws the map for each act. If the engine takes one number too many or too few from any of them, every later shuffle, reward and monster move comes out different, and the two games never agree again.

So the first commit was the random number generators: libGDX's generator, Java's own Random and Java's shuffle, each tested against values produced by the game's own jar. On day one those tests caught the decompiler printing two of the generator's constants as rounded decimals. The true values are 2^-53 and 2^-24.

A second trap was Java's hash maps. The game builds its lists of cards and relics by walking hash maps, so the order of those lists depends on Java's hashing, and a roll of "card number 40" means a different card if the order is off. The engine's orders come from running HashMap on the Java runtime that ships with the game.

The first week

By the end of the first day the engine had the generators, Act 1 combat for the Ironclad, a command-line player, and 107 fights recorded from the real game that it reproduced exactly. On the second day it gained relics, potions, the shop and the Act 1 events, then Acts 2 to 4, the keys and the Heart. The Silent, the Defect and the Watcher followed on 19 October, all three in one day of 82 commits.

With a random player the engine gets through about 75,000 fights a second on one core, or about 14,000 whole runs a second. From Python, where the training happens, it steps about a million moves a second on ten cores.

Checking it against the real game

A fast engine with wrong rules would train a network for a game nobody plays, so most of the effort went into proving the copy matches.

The real game runs with three community mods (ModTheSpire, BaseMod and CommunicationMod) and a patch of my own that can set up any deck, relics and HP and start any fight. The checking goes both ways.

From the game to the engine, a script plays the real game with random moves and records the full state at every decision. The engine replays the same seed and compares everything: the piles in order, HP, block, powers, each monster's next move, gold, and how far each random stream has advanced. A mismatch names the exact decision where the engine's rules are wrong.

From the engine to the game, runs that a network played in the engine are turned into commands and sent to the real game, with the floor and HP compared before every move. I call a few hundred of those left running unattended a soak.

So far 471 recorded fights and 118 recorded runs match, with five known exceptions. Each of the 855 items on the porting checklist (cards, relics, potions, powers, monsters, events) has a unit test and has turned up in a recording that matches the real game.

What the checks found

A 450-run soak across every ascension failed on 242 runs, and 241 of those had one cause. The game includes the event Note For Yourself at ascensions 1 to 14 only when you're playing below your highest unlocked ascension. My test profile hadn't unlocked any, so the game left the event out, the engine left it in, and every event roll afterwards went a different way.

Some of the game's randomness depends on the clock. While a curse sits selected on the transform screen, the game re-rolls its preview every 0.1 seconds from the same stream that decides card rewards, so how long you look at it changes your next reward. Discovery and the potions that let you pick one of three cards re-roll their three every frame. The Courier's restocked card isn't seeded at all. For each of these I patched the modded game to roll once in a fixed place, and the engine does the same. The repository lists them as known deviations.

Running the game fast caused bugs of its own. Reaper's heal went missing: a battle ends when the last death animation finishes, and at high speed that could happen before the queued heal ran.

The networks also found rules that random play never reached. The Defect network learned to throw a Smoke Bomb and walk away from the elite in Act 4. The real game doesn't allow Smoke Bomb while an enemy has Back Attack. The engine only blocked it against bosses, and I found out when the real game refused the command in eight runs.

Soaks were slow at first. Those 450 runs took about 12 hours on three copies of the game. Now each drawn frame runs the game's own update 200 times at a fixed time step, so the game passes through the same states as at normal speed, and 300 Defect runs (191,000 decisions) take 58 minutes on four copies.

Next: part 2, teaching a network to play the Ironclad.

[ Return to top ] [ All writing ]


Jack Tricker / me@jacktricker.com
Last modified: 28 October 2026