On the road to the market a river bandit steps out of the reeds with a club,
and this time there is no talking. A fight in a role-playing game can look
like a new set of rules, but it is made of the same pieces as everything
before: checks, dice and averages. This lesson plays one fight as a sequence of
checks, then asks the two questions a player wants answered before drawing a
sword: how often do I win, and how long will it take?
The two fighters
Ada
The bandit
The sheet
Agility +2, Endurance 0; trained in Fighting
Might +2, Agility +1, Endurance +1; trained in Fighting
Hit points (10 + 2 × Endurance)
10
12
Defence (10 + Agility + armour)
14 (leather, 2)
13 (leather, 2)
Weapon
light sword: Agility +2, trained +3
club: Might +2, trained +3
To hit
d20 + 5
d20 + 5
Damage
1d8 + 3
1d6 + 3
A fight, step by step
Who acts first. Each fighter rolls for
initiativeInitiative (role-playing)The roll that decides who acts first in a fight: a d20 plus the fighter's Agility, the higher total going first in every round.: a d20 plus their
Agility. The higher total acts first in every round of the fight; on a tie,
both roll again.
A round. In a roundRound (role-playing)One turn of a fight, in which each fighter still standing acts once, in initiative order. each
fighter acts once, in initiative order. Acting here means attacking.
An attack. An attack rollAttack rollA check made to hit someone in a fight: a d20 plus the attacker's to-hit modifier, against the target's defence. is a check: a
d20 plus the attacker's to-hit modifier, against the target's
defenceDefence (role-playing)How hard a character is to hit: 10, plus their Agility, plus their armour. An attack roll must reach it., which plays the part of
the difficulty. Reaching it is a hit. Defence is the one target that is not
easy, medium or hard: it belongs to the character, not to the task.
Damage. A hit rolls the weapon's die; a fighter trained in Fighting
adds 3. The result is the attack's
damageDamage (role-playing)The harm a hit does, taken off the target's hit points: the weapon's dice, plus 3 when the attacker is trained in Fighting., and it is taken off the
target's hit pointsHit points (role-playing)A count of how much harm a character can take: each point of damage takes one away, and at 0 the character is out of the fight..
The end. A fighter at 0 hit points or below is out of the fight and
does not act again. In this platform's game they are knocked out or
captured, never killed.
Ada's attack: d20 + 5 against 13, so the die must show 8 or more:
13 faces, 65 %. A hit averages 4.5 + 3 = 7.5, so her expected damage is
0.65 × 7.5 = 4.875 a round.
The bandit's attack: d20 + 5 against 14, a die of 9 or more: 12 faces,
60 %. A hit averages 3.5 + 3 = 6.5, so 3.9 a round.
Initiative: Ada rolls d20 + 2, the bandit d20 + 1. Of the 400 pairs of
faces, 19 are ties, Ada is ahead in 210 and the bandit in 171. Ties are
rolled again, so Ada acts first in 210 of 381: 55 %.
Neither fighter can win with one blow: Ada's sword deals at most 8 + 3 = 11,
one short of the bandit's 12, and the club at most 6 + 3 = 9, one short of
Ada's 10.
The same rules gave the troll of
Beyond the average: risk, variance and ruin
its odds: a troll attacking
at +3 against Ada's defence of 14 needs an 11 or more, and lands half its
blows.
How long, and who wins
A first guess comes from the averages. The bandit's 12 hit points last 12 ÷
4.875, about 2.5 rounds of Ada's attacks; Ada's 10 last 10 ÷ 3.9, about
2.6 rounds of the bandit's. It is close, and Ada is slightly ahead. But a
fight is not its average: one lucky blow, or acting first, changes everything.
Worked out exactly, over every way the dice can fall:
Ada wins 54 % of fights.
A fight lasts 2.6 rounds on average.
Initiative matters most: when Ada acts first she wins 65 % of the time,
and when the bandit does, only 40 %.
ⓘ Concept: Acting first is worth a lot in a short fight
Each fighter needs at least two hits, and a fight lasts about two and a half rounds,
so whoever acts first often lands the second hit before the other does. That
is why Ada's chance moves from 40 % to 65 % with initiative alone. Combat is
only checks and averages, but it adds one thing they lack: order. In a
longer fight, between fighters with more hit points, the first attack would
matter less.
Why it matters — When a fight lasts two or three rounds, one extra attack is a large share of the whole fight.
The fight, twice
The program below works the fight out exactly, round by round, over every
outcome of every attack, and then simulates a thousand duels with a fixed seed.
import random
from functools import cache
ADA = {"hit_points": 10, "defence": 14, "to_hit": 5, "damage": (8, 3), "agility": 2}
BANDIT = {"hit_points": 12, "defence": 13, "to_hit": 5, "damage": (6, 3), "agility": 1}
def hit_chance(attacker, target):
"""An attack roll is a check: d20 + to-hit against the target's defence."""
return sum(face + attacker["to_hit"] >= target["defence"] for face in range(1, 21)) / 20
def first_chance(a, b):
"""Initiative: d20 + Agility each, the higher acts first, a tie rolled again."""
pairs = [(x + a["agility"], y + b["agility"]) for x in range(1, 21) for y in range(1, 21)]
ahead = sum(p > q for p, q in pairs)
return ahead / (ahead + sum(p < q for p, q in pairs))
def blows(attacker, target):
"""Each possible result of one attack: (chance, damage), a miss doing 0."""
sides, plus = attacker["damage"]
hit = hit_chance(attacker, target)
return [(1 - hit, 0)] + [(hit / sides, face + plus) for face in range(1, sides + 1)]
def exact(a, b):
"""(chance that a wins, expected rounds), by working through every outcome."""
a_hits, b_hits = blows(a, b), blows(b, a)
@cache
def fight(a_left, b_left, a_first):
# One round from here: both act in turn, a fighter at 0 does not act.
ends, wins, onward = 0.0, 0.0, {}
for p1, d1 in (a_hits if a_first else b_hits):
hp_a, hp_b = (a_left, b_left - d1) if a_first else (a_left - d1, b_left)
if min(hp_a, hp_b) <= 0:
ends += p1
wins += p1 * (hp_b <= 0)
continue
for p2, d2 in (b_hits if a_first else a_hits):
after = (hp_a - d2, hp_b) if a_first else (hp_a, hp_b - d2)
if min(after) <= 0:
ends += p1 * p2
wins += p1 * p2 * (after[1] <= 0)
else:
onward[after] = onward.get(after, 0) + p1 * p2
stay = onward.pop((a_left, b_left), 0) # a round where both missed
win, rounds = wins, 1.0
for (hp_a, hp_b), p in onward.items():
w, r = fight(hp_a, hp_b, a_first)
win, rounds = win + p * w, rounds + p * r
return win / (1 - stay), rounds / (1 - stay)
first = first_chance(a, b)
win_first, rounds_first = fight(a["hit_points"], b["hit_points"], True)
win_second, rounds_second = fight(a["hit_points"], b["hit_points"], False)
return (first * win_first + (1 - first) * win_second,
first * rounds_first + (1 - first) * rounds_second,
win_first, win_second)
def simulated(a, b, duels=1_000, seed=1):
"""(share of duels a wins, average rounds), over many simulated duels."""
roller = random.Random(seed)
fighters = (a, b)
won = rounds = 0
for _duel in range(duels):
rolls = [0, 0]
while rolls[0] == rolls[1]: # initiative, rolled again on a tie
rolls = [roller.randint(1, 20) + f["agility"] for f in fighters]
order = (0, 1) if rolls[0] > rolls[1] else (1, 0)
left = [a["hit_points"], b["hit_points"]]
while min(left) > 0:
rounds += 1
for me in order:
foe = 1 - me
if left[me] > 0 and roller.randint(1, 20) + fighters[me]["to_hit"] >= fighters[foe]["defence"]:
sides, plus = fighters[me]["damage"]
left[foe] -= roller.randint(1, sides) + plus
won += left[1] <= 0
return won / duels, rounds / duels
print(hit_chance(ADA, BANDIT), hit_chance(BANDIT, ADA), round(first_chance(ADA, BANDIT), 4)) # 0.65 0.6 0.5512
win, rounds, win_first, win_second = exact(ADA, BANDIT)
print(round(win, 4), round(rounds, 2), round(win_first, 4), round(win_second, 4)) # 0.5365 2.61 0.6462 0.4018
print(simulated(ADA, BANDIT)) # (0.554, 2.61)
The simulated duels land within two percentage points and 0.1 rounds of the
exact answer. A thousand duels are fewer than the ten thousand rolls of earlier
lessons, so they agree less closely.
What comes next: the person who runs the table, describes the fight and
decides when the dice are needed at all
(What a game master does: describe, adjudicate, pace).
Later, a language model plays that role while a program keeps these rules,
rolls these dice and shows every roll to the table.
Your turn
The first rung asks for one number: the fewest hits Ada needs to bring the
bandit down. The second, written from scratch, simulates a duel a thousand
times and reports who wins and how long it takes.
Ask the mentor about this module
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Images, PDF or text. Kept on this device only.
Keeping your files on this device
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
Ctrl/Cmd + Enter to send
Rung 1 — how many hits to win
Loading exercise…
Rung 2 — a thousand duels
Loading exercise…
My notes on this module
Loading your notes...
Combat as a sequence of checks: turns, hit points, damage — TransformerLab