A Clash Royale win-probability engine, tested in a synthetic toy world
A research codebase for win probabilities, video-to-game-state reconstruction and an offline game-playing bot. No real game data has been used yet; everything runs on fictional cards.
- Real game data used
- None yet: a synthetic toy world
- Toy world
- 600 fictional players, 12,000 matches
- Pre-match winner picked (toy)
- 66%, against 53% for a baseline
- Python
- LightGBM
- OpenCV
- JavaScript
Clash Royale is a real-time card game in which two players spend elixir to deploy troops and spells and try to destroy each other’s towers. This project is a research codebase for it: a calibrated win-probability engine, game-state reconstruction from video, an analytics dashboard and a bot that plays offline.
No real Clash Royale data has been used yet. There was no API key, no sourced card statistics and no gameplay footage. Every model, metric and video so far comes from a synthetic toy world: 21 fictional cards, a toy simulator and scripted players, built to copy the game’s structure. The numbers below test the pipeline and the design choices in a world with known ground truth. They say nothing about accuracy in the real game.
What it does #
- Data layer. A client for the official API, a crawler, a database that records where every row came from, and a card database that knows each card’s stats on any date, so balance patches enter as changed numbers. So far it has only run on fictional data.
- Win probability. A pre-match model for two known decks, a prior that sharpens as the opponent’s cards are revealed one by one, and an in-game model that updates that prior from towers, elixir, the board and the opponent’s likely hand. Predictions are calibrated and explained with SHAP attributions (how much each group of inputs pushed the number).
- Vision. Frames rendered from simulator states, each watermarked “SYNTHETIC”, are read back into game states (timer, tower HP, elixir, hand, units) and scored against the simulator’s ground truth. This tests the logic, not detectors on real footage.
- Bot. Rollout-based search (beam search, a flat Monte Carlo search and one-step lookahead), in the toy simulator only. The game’s fair-play policy bans bots in real matches, so the code has no client interaction, screen tapping or network code.
- Dashboard. A local web app with a matchup analyser, a match replay that charts the win probability and explains its swings, and a model report.
Results in the toy world #
The world has 600 fictional players and 12,000 matches over 60 simulated days, split by time into training, validation and test windows.
- On 2,000 held-out matches, the pre-match model picks the winner 66% of the time (AUC 0.73, where 0.5 is a coin flip), against 53% for a baseline that only knows card levels and trophies.
- In the first version of the world, in-game accuracy rose from about 62% in the first 30 seconds to 85% in the last 30 seconds of normal time. Tower state carried most of the signal.
- A random train/test split would have overstated pre-match quality (log loss 0.573 against 0.612 on a split by time), so only time-split numbers are reported.
- The largest single gain came from simulation: projecting tower damage eight seconds ahead improved the in-game model at every stage of the match.
- Search-based bots beat the scripted players by a wide margin, with beam search strongest, at 20-100 ms a decision. Tuning a bot’s weights on 16-game evaluations overfit and made it worse in a fresh league.
What the review caught #
A technical review re-ran each idea on identical test rows, with confidence intervals that resample whole matches. It found that 8.1% of matches in the first world were near-idle because the scripted players could stall, and a second version of the world removed them. It also found that the opponent-elixir tracker was confidently wrong when plays were missed: its 95% intervals covered the truth only 20% of the time. Modelling missed detections explicitly raised that to 90%. Several ideas measured no gain and were dropped, including stage-by-stage recalibration and lagged history features.
Limits #
Real data is the whole gap. The in-game model needs real match timelines, which only screen recordings and trained detectors can supply, and the simulation feature needs a faithful game engine.
How it’s built #
Python throughout: SQLAlchemy and SQLite for the data, LightGBM, XGBoost, CatBoost and scikit-learn for the models, SHAP for explanations, OpenCV for the vision pipeline and FastAPI for the dashboard’s API. The dashboard itself is plain JavaScript with Chart.js.