How the AI is computed
The battle AI on this site does not "think" while you play — every move is read from a table that was computed once, in advance. Here is how that table is built, aimed at a high-school student who likes math. The only tools you need are probability, expected value, and the patience to let an equation solve itself.
1 · One position is a tiny game
Look at a single moment in the battle. Both players choose at the same time — attack, or switch — and only then is the choice revealed. Because you move blind, no single choice is always best: if you always attack, the opponent switches to dodge it; if you always switch, they punish you. This is exactly rock–paper–scissors, and the right answer is to mix — to choose randomly with carefully tuned probabilities.
| opp attacks | opp switches | |
|---|---|---|
| you attack | 0.30 | 0.60 |
| you switch | 0.70 | 0.40 |
For a 2×2 game like this there is a formula. With the four entries named
a b / c d (top-left to bottom-right), the probability you should attack and the
resulting win probability of the position are:
Here that gives p = 0.5 and win prob = 0.5: attack half the time, and the
position is a coin-flip. This mix is the Nash equilibrium of the little game — neither
player can do better by deviating, so a mixed strategy is not weakness, it is the exact answer.
2 · Positions point at each other
There is a catch hiding in that table. The number 0.30 for "both attack" is itself
"your win probability in the position you reach next" — after the damage is dealt. So the
win probability of a position is defined in terms of the win probabilities of other positions. Everything refers
to everything else. Writing V(s) for the win probability of position s:
"Averaged", because the game has dice: critical hits (1 in 24), a damage roll (16 equally likely rolls), and a coin flip for who moves first. Each cell of the matrix is the expected value over all those outcomes — an ordinary weighted average.
3 · Let the equation solve itself
An equation where V appears on both sides looks circular, but it can be solved by
repetition. Start from the positions whose win probability is already obvious — someone has fainted,
so V = 1 or V = 0. Then sweep over every position and recompute
its win probability from the current guesses of its neighbours. Repeat. Each pass pushes the truth one step
further back from the decided positions, and the numbers stop moving once they are consistent
everywhere. This is value iteration, and the strategy (the switch probabilities) falls out
of the same matrix solves.
4 · The trap: games that never end
Two Pokémon can just keep switching, forever, and never take damage. What is the win probability of a game that never ends? If we call it a draw worth 0.5, something breaks: a losing player escapes to 0.5 simply by stalling, and the whole table collapses — every position becomes 0.5 and the AI refuses to fight. A single table can't fix this on its own, because it has no clock: it cannot tell turn 3 from turn 80, so it cannot say "stop stalling, the end is near."
The fix is two small rule changes to the math of the game:
- If a game drags on, decide it by health. Whoever has more Pokémon left — and then more total HP — wins. A staller who is behind now loses by stalling, so stalling stops being a free escape.
- Give every turn a tiny chance of being the last. Model a small probability (here 1%) that the battle simply ends and is settled by that health tiebreak. This is called discounting, and it is the magic ingredient.
A theorem of Lloyd Shapley (1953) says that with that per-turn ending chance, a game like this always has an optimal strategy that depends only on the position — exactly the single clock-free table we want. Remove the ending chance and the guarantee is gone; the true optimal play would have to count how long the opponent has been stalling, which no single table can do.
5 · The result
Enumerate every position — each side's active Pokémon and both Pokémon's HP, bucketed into 26 levels — and run value iteration to convergence. That yields, for all 3.38 million positions, a probability of switching. We then check it honestly: we let a perfect opponent best-respond to the table and measure how much they gain. The answer is essentially zero — the table is unexploitable in this game.
One honesty note. The playable battles here do not impose the "1% sudden ending" rule — being told a match can just stop feels unfair to a human. So the table is the exact equilibrium of the game with that rule, deployed in the game without it. In ordinary play, which resolves in around ten turns, the ending almost never fires and the AI plays near-optimally anyway. In theory a player who deliberately stalls forever could gain an edge — whether a human can actually pull that off is the next thing to test.