Sub topic 2
Resources: the assets players gain, spend, and protect
This chapter assumes you know the four formal elements (players, objectives, rules, and resources) and can read simple Python.
Resources as state
A resource is any trackable asset a game lets players hold. Stones, gold, cards in hand, and action points all qualify. Together, resources form the game's state. A rule that changes a resource changes what players can do next.
Specify three things for each resource: how players gain it, how they spend it, and whether it can be lost or protected.
Procedures: the actions players can take
A procedure is a named action a player may take on their turn. Procedures are the verbs of a game. Each one has a precondition (when it is legal), a cost (the resources it spends), and an effect (what it changes). Writing procedures this way makes each one testable.
def take_stones(state, n):
# precondition: take 1 to 3 stones, and no more than remain
if not 1 <= n <= min(3, state["stones"]):
raise ValueError("illegal procedure")
# cost and effect: remove stones from the shared pile
return {"stones": state["stones"] - n}
print(take_stones({"stones": 10}, 3)) # {'stones': 7}How the elements shape each other
The four elements constrain one another. Change one and the others shift. In the example above, the objective is to take the last stone, the rule caps each take at three, and the resource is the shared pile. Together they make pile size decide the winner. If you change the objective so that taking the last stone loses, the same rules and pile produce different good moves.
Exercise
In the takestones code above, what happens if you call takestones with n=0 on a pile of 5 stones, and why does the precondition matter?
Show answer
It raises ValueError("illegal procedure"), because 0 is outside the range 1 to 3. Without the precondition, a call with n=0 would pass forever, and a negative n would increase the pile.