Spielerando

Z3 for Game Makers: Is There Any Number That Breaks This?

Z3 answers one question very well: is there any setting, any number in my ranges, that breaks this rule? It either shows you one, with the exact numbers, or answers unsat, which means no such setting exists anywhere in the range, not just among the values someone tried.

What is Z3, in plain words?

Z3 is a solver from Microsoft Research. You tell it your numbers, how far each one may go, and the bad thing you fear, such as "one hit does 300 damage". Then Z3 looks for numbers that make all of it true at once.

The Z3 guide lists three possible answers:

  • sat: it found numbers. Your rule can break, and Z3 shows you how.
  • unsat: no numbers in the ranges can do it. For a bad thing, this is the good news.
  • unknown: Z3 could not decide. Treat that as "we do not know", never as a pass.

The trick is to ask for the bad thing. Z3 does not try 20, 21, 22 and so on. It reasons about the whole range at once, fractions included.

When does it pay off?

  • Balance: can any build one-shot the boss? Can damage ever go below zero and heal the enemy?
  • Economy: can a player end up with no money and no way to earn more?
  • Level timing: does the jump clear the gap at every running speed the game allows?
  • Difficulty settings and upgrades, wherever values come from ranges, sliders or random rolls.

It is the wrong tool for the order of events over many turns, such as quests and softlocks. TLA+ handles those. It also says nothing about feel or fun.

Can one hit take the whole boss?

An invented boss has 300 life. Damage is weapon plus strength, times a critical hit factor, times a rune bonus. Save this as oneshot.smt2:

; Can one hit ever take the whole boss? The boss has 300 life.
(set-option :pp.decimal true)
(declare-const weapon Real)   ; weapon damage: 20 to 60
(declare-const strength Real) ; strength bonus: 0 to 30
(declare-const crit Real)     ; critical hit: times 1.5 to 2.5
(declare-const rune Real)     ; rune bonus: +0 to +50 percent
(assert (and (<= 20 weapon 60) (<= 0 strength 30)
             (<= 1.5 crit 2.5) (<= 0 rune 0.5)))
(define-fun hit () Real (* (+ weapon strength) crit (+ 1 rune)))
(assert (>= hit 300))
(check-sat)
(get-value (weapon strength crit rune hit))

Run z3 oneshot.smt2:

sat
((weapon 59.96875)
 (strength 21.0)
 (crit 2.49609375)
 (rune 0.484375)
 (hit 300.0004863739?))

So yes: a weapon near the top, 21 strength, a big critical hit and a rune near +50 % reach 300. The question mark only means the number goes on after the digits shown.

Is that a bug? That is your call. If the boss should never fall to one hit, cap the rune at +30 %, change 0.5 to 0.3, and run again:

unsat
(error "line 12 column 42: model is not available")

unsat: no setting in these ranges reaches 300. The second line is not a problem. It only says there is no example to show.

The same in Python

If your AI prefers Python, the same check needs pip install z3-solver:

from z3 import Reals, Solver, sat, set_option

set_option(rational_to_decimal=True, precision=2)
weapon, strength, crit, rune = Reals("weapon strength crit rune")
hit = (weapon + strength) * crit * (1 + rune)

s = Solver()
s.add(20 <= weapon, weapon <= 60, 0 <= strength, strength <= 30,
      1.5 <= crit, crit <= 2.5, 0 <= rune, rune <= 0.5)
s.add(hit >= 300)

if s.check() == sat:
    m = s.model()
    print("Breaks:", m, "hit =", m.eval(hit))
else:
    print("No setting reaches 300.")
Breaks: [strength = 21, weapon = 59.96?, crit = 2.49?, rune = 0.48?] hit = 300.00?

With rune <= 0.3 it prints No setting reaches 300.

Can the player get stuck with no money?

An invented farm. Every morning the player sells all crops, pays rent, then buys seeds, and each seed is a crop the next morning. The landlord is kind and always leaves you the price of one seed. A player is stuck when there are no crops and not even the money for one seed. Can a player who wakes up able to farm end the day stuck?

; An invented farm. Every morning: sell all crops, pay rent, buy seeds.
; Can a player who wakes up able to farm end the day stuck?
(declare-const price Int)  ; one crop sells for 8 to 15 coins
(declare-const seed Int)   ; one seed costs 5 to 10 coins
(declare-const rent Int)   ; rent is 0 to 20 coins a day
(declare-const money Int)  ; coins at dawn
(declare-const crops Int)  ; ripe crops at dawn
(assert (and (<= 8 price 15) (<= 5 seed 10) (<= 0 rent 20)))
(assert (and (>= money 0) (>= crops 0)))
; fine at dawn: crops to sell, or coins for a seed
(assert (or (> crops 0) (>= money seed)))
(define-fun sold () Int (+ money (* crops price)))
; the landlord always leaves you the price of one seed
(define-fun left () Int
  (ite (>= (- sold rent) seed) (- sold rent) (ite (< sold seed) sold seed)))
; stuck at night: not even one seed, so nothing grows tomorrow
(assert (< left seed))
(check-sat)
(get-value (price seed rent money crops left))
sat
((price 8)
 (seed 10)
 (rent 2)
 (money 1)
 (crops 1)
 (left 9))

The kind landlord did not help. One crop sells for 8 and a seed costs 10, so a player with one crop and one coin can never buy a seed again. Add one rule before (check-sat), (assert (<= seed price)), which means a seed never costs more than a crop sells for, and Z3 answers unsat.

Why is one day enough? Z3 checked every possible morning, not the mornings of one test run. If no good morning can end stuck, and the game starts with a good morning, then day 1,000 is fine too.

Does the jump clear the gap?

; Does a full jump clear a gap 4 tiles wide at every running speed?
(set-option :pp.decimal true)
(declare-const speed Real)           ; running speed: 4 to 9 tiles a second
(define-fun jump () Real 12.0)       ; upward speed at take-off
(define-fun gravity () Real 30.0)    ; pull down, per second
(define-fun airtime () Real (/ (* 2.0 jump) gravity))
(define-fun distance () Real (* speed airtime))
(assert (<= 4 speed 9))
(assert (< distance 4))              ; look for a jump that falls short
(check-sat)
(get-value (speed distance))
sat
((speed 4.0)
 (distance 3.2))

At the slowest speed the hero lands 0.8 tiles short. Make the slowest run 5 tiles a second, and Z3 answers unsat.

Be honest about what this checks: the formula, not your engine. Games move in frames, and computers round numbers. If that matters, ask your AI to write the formula the way your code computes it, frame by frame. Z3 can also work with the computer's own rounded numbers.

How do I install and run it?

  • On a Mac with Homebrew: brew install z3, then z3 yourfile.smt2.
  • With Python on any system: pip install z3-solver, as the Z3 GitHub page says.
  • In the browser: the online Z3 Guide has a playground and a tutorial.

Or let your AI install it. The examples here ran with Z3 4.15.

Why does this matter more as the game grows?

Ten items with five upgrade levels each make almost ten million combinations. Nobody tests them all, and an AI that says "looks balanced" has looked at a few. Every new item, rune or difficulty setting adds to the pile. Z3 does not sample. When it says unsat, it means every combination in the ranges you gave it.

Keep the files with your game. After a balance change, your AI runs them again, and a change that broke an old promise shows up with the exact numbers.

Paste this to your AI

Check the numbers in my game with the Z3 solver. Do not just read the code.

1. Find the numbers that matter for [damage / prices / jumping / ...] and the range each one can take in the game, including upgrades, items and random rolls. Show me the list with the place in the code for each, and ask me what is missing.
2. Ask me what must never happen, for example "one hit kills the boss", "damage below zero", "no money and no way to earn", "the jump falls short". Write each as its own Z3 check that searches for that bad case.
3. Write the formulas exactly the way my code computes them. If the code rounds or works frame by frame, include that, or tell me that you left it out.
4. Install Z3 if it is missing (pip install z3-solver, or brew install z3) and run every check.
5. Show me the real output. For sat, tell me in plain words which settings break the rule and how a player could get there. For unsat, say it holds for every value in the ranges. For unknown, say that we do not know.
6. Break one rule on purpose and show me the check catching it.
7. Keep the checks in the project and run them again after every change to these numbers.

Never say "looks balanced" or "should be fine". Say what Z3 checked, in which ranges, and what it did not check.

Which tool for which question?

Question Tool
Can things happen in an order that breaks the game? Softlocks, quests, turns, saving TLA+ and TLC
Is there any number or setting that breaks this? Balance, prices, timing Z3, this page
Must this law hold forever, for every input and any number of moves? Lean 4

None of them tells you whether a fight feels fair or fun. Only playing does. If you do not know yet where your game breaks, start with My game keeps breaking.

Was this page helpful?

One click, no account. It shows us which pages help.

Next step

Check in the browser

Check your game's rules in your browser

Before you install anything, the check page loads this guide's boss, money and jump files and runs them with Z3 inside your browser, so you see sat and unsat for yourself and nothing you type leaves your computer.

Sources

Last updated: 2026-11-25