Sub topic 6
Procedures: the actions players can take
This chapter assumes you have read the earlier chapters on players, objectives, rules, and resources. It also assumes you can read short Python functions and dataclasses. The code is optional, but it makes the ideas precise.
What a procedure is
A procedure is an action a player can take during play, together with the steps that carry it out. "Take 1 to 3 stones", "play a card from your hand", "move a piece one square" and "jump" are all procedures. Procedures are the verbs of the game. Players are the nouns who act, resources are the nouns acted on, and the objective says why any of it matters.
Procedures sit close to rules, so the two are easy to confuse. A rule says what is allowed. A procedure is the thing that is allowed or forbidden. A useful test is to ask whether a player can do it.
| Element | Question it answers | Nim example |
|---|---|---|
| Objective | What is the player trying to achieve? | Take the last stone |
| Rule | What is permitted, forbidden, or defined? | You may take at most 3 stones |
| Procedure | What can the player do, and what happens when they do it? | Take 2 stones from the pile |
| Resource | What is changed or spent by acting? | The stone pile |
Procedures are formal elements, so they exist apart from theme. "Move a knight" and "play a card" can be re-skinned as "send a scout" or "cast a spell" without changing the structure. If the new wording would change which choices matter, the procedure itself has changed.
The anatomy of an action
You can specify any procedure with the same four parts:
- Actor. Which player may perform it, and in which role. Some actions are available to everyone, and some only to a role such as the dealer or the active player.
- Precondition. What must be true before the action is legal. Example: the pile holds at least as many stones as you want to take.
- Cost. What the actor gives up, if anything. This may be a resource, such as spending a coin. It may also be the turn itself, because choosing one action usually means not choosing another.
- Effect. How the game state changes afterward. This includes resource changes, position changes, and whose turn comes next.
Writing actions this way forces you to be unambiguous. "You can attack" is vague. "The active player may attack an adjacent enemy unit by spending 1 action point, and the defender loses 1 health" has all four parts, and two people reading it will play it the same way.
Procedures versus rules
Procedures and rules interact through the precondition. The rule "you may take at most 3 stones" is not a separate action. It limits the take action by deciding which versions of it are legal. When you analyze a game, list the actions first, then list the rules that constrain them. Many rules turn out to be preconditions on a specific action. Others, such as the constitutive rules, define what the actions mean, for example what counts as "capturing".
The action space
The action space is the set of actions available to a player at a given moment. Two properties of it matter in practice.
- Breadth is how many legal actions the player has right now. In Nim with a pile of 10, breadth is 3. With 1 stone left, breadth is 1.
- Depth is how far the consequences of an action reach. A single Nim move shapes every later move, so depth is high even though breadth is low.
Breadth is not the same as meaningful choice. A game can offer 50 legal actions where 49 are obviously bad, which is effectively one choice. A game can also offer 2 actions that are evenly matched and hard to compare, which is a rich choice. When you judge procedures, ask how many actions are worth considering, not how many exist.
A procedure as data
The Nim model from earlier chapters used a function, legal_moves, to compute legal moves. Here each action becomes an object with a name, a precondition, and an effect. That mirrors the anatomy above.
from dataclasses import dataclass
from typing import Callable
PLAYERS = 2
@dataclass(frozen=True)
class State:
stones: int
current: int
@dataclass(frozen=True)
class Action:
name: str
is_legal: Callable[[State], bool] # precondition
effect: Callable[[State], State] # effect on the state
def take(n):
return Action(
name=f"take {n}",
is_legal=lambda s: 1 <= n <= s.stones,
effect=lambda s: State(s.stones - n, (s.current + 1) % PLAYERS),
)
ACTIONS = [take(1), take(2), take(3)]
def available(state):
return [a for a in ACTIONS if a.is_legal(state)]
for stones in (10, 3, 2, 1, 0):
names = [a.name for a in available(State(stones, 0))]
print(stones, names)The output is:
10 ['take 1', 'take 2', 'take 3']
3 ['take 1', 'take 2', 'take 3']
2 ['take 1', 'take 2']
1 ['take 1']
0 []The breadth shrinks as the resource shrinks. At 0 stones, no action is legal. In this game that is fine, because the game is already over. In other games it would be a bug, as the edge cases below explain.
How an action resolves
Every turn-based game runs the same loop around its procedures. The player chooses an action, the game checks the precondition, the game applies cost and effect, and then it checks whether the game has ended.
Notice where each element appears. The objective is checked at the end of every cycle. The rules supply the precondition. The resources change in the cost and effect steps. Procedures are the path through the loop.
Changing the action set changes the game
Procedures are one of the strongest levers a designer has. Adding, removing, or resizing one action can change which strategy wins, even when players, objective, and resources stay the same.
The next program finds the pile sizes where the player to move loses, assuming the objective is to take the last stone and both players play perfectly. It then compares three action sets.
def losing_piles(sizes, limit):
"""Pile sizes where the player to move loses under perfect play.
Objective: taking the last stone wins.
sizes: the amounts a player may take in one move.
"""
wins = {}
losing = []
for n in range(limit + 1):
wins[n] = any(k <= n and not wins[n - k] for k in sizes)
if not wins[n]:
losing.append(n)
return losing
print((1, 2, 3), losing_piles((1, 2, 3), 20))
print((1, 2, 3, 4), losing_piles((1, 2, 3, 4), 20))
print((1, 3), losing_piles((1, 3), 20))The output is:
(1, 2, 3) [0, 4, 8, 12, 16, 20]
(1, 2, 3, 4) [0, 5, 10, 15, 20]
(1, 3) [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]Adding one action, "take 4", moves the key number from four to five. Removing "take 2" turns the key into any even number. The pile, the players, and the objective did not change, yet the winning strategy did. That is the interplay that later chapters study. Here the point is that you can predict the effect of a procedure change by reasoning about which states become reachable.
Turn structure and timing
Procedures also need a schedule. The timing of actions is part of their definition, and it varies in a few common ways.
- Strict alternation. One action per player in a fixed order, as in Nim or chess. This is the simplest case and the easiest to analyze.
- Multiple actions per turn. The player spends an allowance, such as three action points. The allowance is a resource, so this produces the trade-offs covered in the resource dynamics chapter.
- Phases. The turn is split into steps, such as draw, play, and discard. Each phase allows only certain procedures. Phases act as preconditions on the whole action set.
- Simultaneous selection. Players choose actions in secret and reveal them together, as in rock-paper-scissors. There is no precondition that depends on the opponent's current choice, so the game must resolve conflicts between the chosen actions explicitly.
- Real-time. Actions can be taken at any moment, limited only by cooldowns or reaction speed. The cooldown acts as a cost paid in time.
Whichever you choose, state who may act, when, and what ends the opportunity to act.
Edge cases and common errors
- No legal action. If a player must act but has no legal action, the game is stuck. Define what happens: the player passes, the game ends, or the rules supply a fallback action. Nim avoids this because the game ends when the pile is empty.
- Forced moves. When breadth is 1, the player has no choice. A few forced moves are normal. A long chain of them means the game is playing itself.
- Always-available passing. A "pass" action looks harmless, but it can allow endless play when both players pass, and it can change the strategy. Add a termination rule, such as ending the game after two consecutive passes.
- Dominant actions. If one action is always at least as good as every other, the choice is not real. Check by asking whether each action is best in some situation.
- Hidden preconditions. Players often assume an action is available because it is common in similar games. If your game forbids it, say so, or it becomes an implicit rule that people argue about.
- Mixing up the action and its result. "Capture a piece" is an effect of moving, not a separate procedure, unless the game lets players capture without moving. Describe what the player does, and then what changes.
- Confusing procedure with interface. Clicking a button, dragging a card, or rolling a die by hand are ways of performing an action. They belong to presentation. The procedure is the formal choice underneath them.
Checklist for specifying procedures
- List every action a player can take, using a verb phrase.
- For each, write the actor, precondition, cost, and effect.
- Note what the player gives up by choosing it instead of another action.
- Check what happens when no action is legal.
- Compare breadth with the number of meaningful choices.
- State the turn structure that decides when actions occur.
Exercise
In the Nim model, you add an action "pass" that is always legal, does not change the stone count, and gives the turn to the other player. Name two design problems this creates and one rule that would fix the first.
Show answer
First, the game can run forever if both players keep passing, because nothing moves the pile toward zero. Second, the winning strategy changes: leaving a multiple of four no longer forces a win, since the opponent can pass to hand you that position. The first problem can be fixed with a termination rule, such as ending the game after two consecutive passes. The same fix, or a limit on passes per player, also helps with the second problem.
Exercise
A card game says "Attack: choose an opponent; they lose 2 health." Rewrite it as a procedure with actor, precondition, cost, and effect. Add a cost that makes the choice a real trade-off.
Show answer
Actor: the active player. Precondition: at least one opponent is still in the game, and the actor has at least 1 energy. Cost: spend 1 energy, which also means the actor cannot spend it on another action this turn. Effect: the chosen opponent loses 2 health. Because the energy could instead be spent on defending or building, attacking has an opportunity cost.