Turn-Based Combat
Turn-based combat lets characters act one at a time in a fixed order, each once per round, until one side has nobody left standing. It usually goes wrong around death and order: a fallen character loses their turn, the round still moves on, and a minimum damage and a round limit make sure every battle ends.

Paste this to your AI
Copy it into Claude, Cursor or ChatGPT. It already has good values and the usual mistakes in it.
Add "Turn-Based Combat" to my game.
What it should do: Turn-based combat gives the player time to think. Every action is a decision, made in full view of what it will do.
Build it exactly as this state machine. First check that it fits my game, then build it like this:
```mermaid
stateDiagram-v2
[*] --> setup
setup: Line-up
first: First turn
turn: Later turn
won: One side wins
draw: Draw
setup --> first: both sides stand
setup --> won: one side is empty
setup --> draw: nobody is standing
first --> turn: attack, guard or wait
first --> won: last enemy falls
turn --> turn: next character acts
turn --> first: round is over
turn --> won: last enemy falls
turn --> draw: round limit reached
won --> [*]
draw --> [*]
```
Use these starting values:
- minDamage: 1
- guardPercent: 50 %
- maxRounds: 30
Watch out for these common mistakes:
- A character falls before their turn: Tell your AI to skip fallen characters when it moves to the next turn.
- The last character of a round falls: Tell your AI to start a new round whenever nobody is left to act, however that came about.
- Two tough sides never finish: Tell your AI to keep a minimum damage above zero and add a round limit.
- A block that lasts too long: Tell your AI to end a character's block when their own next turn begins.
Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.Or skip this and connect the MCP: your AI then finds all of this by itself.Connect the MCP
What it does
Turn-based combat gives the player time to think. Every action is a decision, made in full view of what it will do.
How it runs, as a diagram
Everything it can be in, and what moves it on. Your AI builds from exactly this.
How it runs, as a diagram
- Line-up → First turn: both sides stand
- Line-up → One side wins: one side is empty
- Line-up → Draw: nobody is standing
- First turn → Later turn: attack, guard or wait
- First turn → One side wins: last enemy falls
- Later turn → Later turn: next character acts
- Later turn → First turn: round is over
- Later turn → One side wins: last enemy falls
- Later turn → Draw: round limit reached
Good starting values
| Value | Start with | What it changes |
|---|---|---|
| minDamage | 1 | The least a hit does. Keep it above zero, so two sides can always hurt each other and a fight keeps moving. |
| guardPercent | 50 % | How much of a hit gets through a block. Lower makes blocking stronger. |
| maxRounds | 30 | After this many rounds the battle is a draw, so it always ends. Lower means shorter fights. |
What usually goes wrong
A character falls before their turn
The game moves on to the fallen character, who then acts, or the round hangs waiting for them.
Fix: Tell your AI to skip fallen characters when it moves to the next turn.
The last character of a round falls
If a new round starts only after the last character has acted, losing that character means the round never moves on.
Fix: Tell your AI to start a new round whenever nobody is left to act, however that came about.
Two tough sides never finish
If defence can cut damage to zero, or both sides only block, the battle never ends.
Fix: Tell your AI to keep a minimum damage above zero and add a round limit.
A block that lasts too long
A block that ends with the round does not hold against a faster enemy, and one that never ends lasts the whole battle.
Fix: Tell your AI to end a character's block when their own next turn begins.
How to check it works
- A slow character listed before a fast one. you start the battle: the fast one acts first.
- Characters of equal speed. you start the battle: they act in the order they are listed.
- Two characters. both have acted: the round count goes up by one and the first character acts again.
- A character who falls before acting. the turn moves on: it goes to the next living character.
- A round where only one character is left to act. that character acts: a new round starts with the first living character.
- A fallen enemy, an ally and a target that does not exist. you try to attack each of them: nothing changes.
- One enemy left. you defeat it: your side has won and it is nobody's turn.
- A finished battle. you try any action: nothing changes.
- A weak attacker against a high defence, with or without a block. the attacker hits: the hit does the minimum damage.
- A character who is blocking. they are hit and then their turn comes: the hit was halved and the block has ended.
- Two characters who only block. the round limit passes: the battle is a draw.
- A side with no living character at the start. the battle starts: the other side has won.
Common questions
how do you code turn based combat
Keep a fixed list of who goes next and a note of who has acted this round. After each action, move to the next living character who has not acted, and start a new round when there is none.
what do i tell my ai to add turn based combat
Paste this: Add turn-based combat where characters act in a fixed order by speed, each once per round, fallen characters are skipped, every hit does a minimum damage, and the battle is a draw after a round limit.
how is turn order decided
Usually by speed, with a fixed tie-break such as the listed order, so the same battle can be replayed. Sorting once per battle keeps it easy to follow; sorting again each round lets speed changes matter.
how do i stop a turn based battle from never ending
Keep a minimum damage above zero, so a hit always counts, and a round limit, so two sides that only block still finish.
Where it comes from
Earliest example we could find: Versuch eines aufs Schachspiel gebaueten taktischen Spiels (Johann Christian Ludwig Hellwig) (1780). Hellwig's chess-derived war game let a player move one piece, or one rectangular block of pieces, per turn. Capture worked as in chess, and infantry and artillery could also shoot. This is the oldest rule set located here that models fighting between military arms in discrete turns. The turn rule comes from a Wikipedia summary, not from the rulebook.
- 1780 Versuch eines aufs Schachspiel gebaueten taktischen Spiels (Johann Christian Ludwig Hellwig): Hellwig's chess-derived war game let a player move one piece, or one rectangular block of pieces, per turn. Capture worked as in chess, and infantry and artillery could also shoot. This is the oldest rule set located here that models fighting between military arms in discrete turns. The turn rule comes from a Wikipedia summary, not from the rulebook. [Recreational wargaming (Wikipedia)][Johann Christian Ludwig Hellwig (Wikipedia)][The Brunswick Wargame (Megan Anderson, Leiden Security and Global Affairs, 2014)]
- 1824 Kriegsspiel rules (Georg Heinrich Rudolf Johann von Reisswitz): Players wrote orders instead of moving pieces, and an umpire carried them out and settled fights with dice and tables. Play was split into turns of a fixed span of battle time. Wikipedia's summary of the English translation says both sides' turns ran at once; another source says alternate moves (see notes). [Shall We Play a Game? Interview with Jon Peterson (Asterisk, issue 14, 2026)][Kriegsspiel (Wikipedia)][Brief History Of Wargaming (wargaming.info)]
- 1913 Little Wars (H. G. Wells): Wells's printed rules for toy soldiers have the players toss for the opening move. In his account of how the game began, one side moved its whole force and then the other did. Firing guns is part of a player's move and comes before moving men. [Little Wars, full text (Project Gutenberg eBook 3691)][Little Wars, catalogue record (Project Gutenberg)]
- 1971 Chainmail (Gary Gygax and Jeff Perren, Guidon Games): Miniatures rules for medieval battles with a fantasy supplement. They offered two turn structures: a die roll that decides who moves first, followed by move and counter-move, or written orders that both sides carry out at the same time. The rulebook was not read directly; the turn rules come from two blogs. [Chainmail (game) (Wikipedia)]['Chainmail' vs 5th Edition 'D&D': Miniature Combat Compared (James Floyd Kelly, GeekDad, 2015)][The Original Game Part 2 - Chainmail #1 (Diegetic Advancement Blog)]
- 1981 Wizardry: Proving Grounds of the Mad Overlord (Andrew Greenberg and Robert Woodhead, Sir-tech): The manual splits combat into rounds. The computer settles what the monsters will do, the player picks one activity for each character, and the game then resolves the round and reports actions as messages. It is the oldest computer RPG in this timeline whose round structure is confirmed from a rules text. [Wizardry rules, text copy of the manual (textfiles.com mirror)][The History of Computer Role-Playing Games Part 1: The Early Years (Matt Barton, Gamasutra/Game Developer, 2007)]
- 1991 Active Time Battle, Final Fantasy IV (US patent 5390937, Square): Square's patent, naming Hironobu Sakaguchi and Hiroyuki Itou as inventors, describes conventional RPG battles as proceeding in turns, then covers a variant in which an enemy can attack while the player is still entering a command. The year is the patent's priority date. The patent text does not name Final Fantasy; the link to Final Fantasy IV comes from Den of Geek and Wikipedia. [US5390937A, Video game apparatus, method and device for controlling same (Google Patents)][Final Fantasy Creators Reveal the Secret Origins of the ATB Combat System (Aaron Greenbaum, Den of Geek, 2023)][Active Time Battle (Wikipedia)]
What was checked
- 17 tests pass on the reference code.
- The state diagram was checked against the code, at the default values and at every listed low and high value.
Your AI gets the code
Tested example code comes through the Spielerando connection, so your AI can use it right away. How to connect it