Sub topic 5
Combining Rules: Orthogonal Design
Prerequisites and the core idea
This chapter assumes you know loops, functions, dictionaries, and sets. You should also be familiar with a game loop that updates shared state in discrete ticks or turns. The previous chapter's idea of local interaction is useful here: a rule usually observes nearby state and changes part of the game state.
Orthogonal design means creating rules that handle different concerns without depending on each other's internal code. Each rule has a clear job. The rules can still affect the same world, so their combinations create outcomes that no single rule describes.
For example, consider these rules:
- A unit moves one tile per turn.
- Fire spreads to adjacent flammable tiles.
- Water blocks fire.
- A unit loses health while standing in fire.
- A player scores for controlling a tile.
The movement rule does not need to know how fire spreads. The scoring rule does not need to know how health changes. Yet a moving unit can enter a burning tile, lose health, abandon a control point, and change the score. That chain is emergent behavior.
Separate concerns instead of writing pair rules
A common mistake is to write a special rule for every pair of systems:
- Fire affects units.
- Fire affects control points.
- Water affects fire.
- Water affects units.
- Units affect control points.
- Units affect scoring.
As the number of systems grows, the number of pairwise cases grows quickly. A new system may require edits in many unrelated places.
Instead, give entities properties and let general rules inspect those properties. A tile can have tags such as flammable, water, or control_point. A unit can have health and position. General rules then use those facts.
| Rule | Reads | Changes | Independent concern |
|---|---|---|---|
| Movement | unit position, passability | unit position | Navigation |
| Fire spread | nearby tile tags | tile state | Environmental change |
| Damage | unit position, tile state | unit health | Survival |
| Scoring | ownership, control tags | player score | Match progress |
Independence does not mean that rule order never matters. It means that order is part of the simulation contract rather than hidden inside unrelated rules. For example, decide whether damage happens before scoring, or whether a unit that moves onto a tile can immediately capture it.
Compose rules through shared state
A useful update cycle reads the old state and writes a new state. This prevents one rule from seeing half-applied changes from another rule.
def update_world(world):
old = copy_world(world)
new = copy_world(world)
apply_movement(old, new)
apply_fire_spread(old, new)
apply_damage(old, new)
apply_scoring(old, new)
return newEach function should have a narrow responsibility. apply_damage should not secretly move units. apply_scoring should not extinguish fire. This makes rules easier to test and makes their combinations easier to reason about.
The following small example uses independent rules to produce a combined result. A unit moves onto a burning control point. Movement changes its position, damage changes its health, and scoring changes ownership.
def resolve_turn(state, move):
old = {
"unit": dict(state["unit"]),
"tile": dict(state["tile"]),
"scores": dict(state["scores"]),
}
new = {
"unit": dict(old["unit"]),
"tile": dict(old["tile"]),
"scores": dict(old["scores"]),
}
# Movement rule
new["unit"]["position"] = move
# Damage rule
if old["tile"].get(move) == "fire":
new["unit"]["health"] -= 2
# Scoring rule
if old["tile"].get(move) == "control_point":
new["scores"]["red"] += 1
return new
state = {
"unit": {"position": (0, 0), "health": 10},
"tile": {(1, 0): "fire", (1, 0): "control_point"},
"scores": {"red": 0},
}
result = resolve_turn(state, (1, 0))
print(result["unit"]["position"]) # (1, 0)
print(result["unit"]["health"]) # 10
print(result["scores"]["red"]) # 1This example exposes a data problem: a dictionary cannot store both "fire" and "control_point" under the same key. A tag set solves it. The tile can have both tags, allowing both rules to apply without adding a special “burning control point” case.
Test combinations, not only individual rules
A rule can pass its isolated tests and still fail when combined with another rule. Test situations where several properties overlap:
- A burning tile is also a control point.
- A unit moves onto water while carrying fuel.
- Two players capture the same point in one tick.
- A rule removes an object that a later rule expects to exist.
Prefer general rules over special cases, but define conflicts explicitly. Set an update order, use phases such as “collect events” and “apply events,” or reject incompatible state. Orthogonal design reduces accidental coupling; it does not remove the need for clear simulation semantics.
Exercise
A game has movement, poison damage, treasure collection, and score rules. A developer adds a special rule called “poisoned treasure pickup.” What design question should they ask first?
Show answer
Ask whether the existing movement, damage, collection, and scoring rules can independently inspect shared state and produce the intended result. Add a special rule only if the interaction cannot be expressed through those general rules.