Sub topic 13
Analyzing and modifying a game with the formal elements
This chapter uses the elements from earlier chapters (players, objectives, rules, resources, procedures) as a working method. You will take a game apart, write down its structure, predict what a change does, and then check the prediction. It assumes you know the earlier definitions, can read short Python functions, and have played a few turn-based games.
Why analysis comes before modification
Designers often change a game by instinct: "make the pile bigger" or "let players take more." Sometimes this works. More often one change shifts several elements at once, and the designer can't tell which element caused the effect.
The formal elements give you a checklist. Before you change anything, describe the game's current structure. After the change, compare the new structure against the old one. Whatever differs is your explanation for any change in play.
The method has five steps:
- Describe the game as it is, element by element.
- Find the points of conflict, boundaries, and outcomes.
- Decide which single element to change, and why.
- Predict the effect on each of the other elements.
- Test the prediction by playing or simulating.
Step 1: Describe the game as it is
Write one line for each element, using testable wording. Here is Nim, the game used throughout this tutorial.
| Element | Description in Nim |
|---|---|
| Players | Two, alternating turns, symmetric roles |
| Objective | Take the last stone from the pile |
| Rules | Take 1 to 3 stones per turn. You must take at least one. Players alternate. |
| Resources | One shared pile of stones. Neither player owns any stones. |
| Procedures | One action: choose a number of stones and remove them |
Check the objective against the earlier test: could an observer look at a finished game and say whether a player met it? "Take the last stone" passes. "Play well" does not.
Notice what the table leaves out. Nim could be dressed as a game about pebbles, matchsticks, or space debris. That theme sits outside the formal elements, so changing it does not change the analysis.
Step 2: Find conflict, boundaries, and outcome
These three describe how the elements work together.
- Conflict. Both players want the same last stone, and the shared pile is the only resource. Every stone one player takes brings the pile closer to the end and changes what the opponent can do.
- Boundaries. The game starts with a set pile size and player 0 to move. It ends when the pile reaches zero. The maximum take of 3 is also a boundary on what a single turn can do.
- Outcome. Exactly one winner and one loser. There are no draws and no scores. Because there are no hidden elements or dice, the outcome is fully determined by the starting pile and both players' choices.
That last point makes Nim easy to analyze exactly. Most games are less tidy, but you still ask the same three questions.
Step 3: Model the game in code
A model turns the description into something you can run. The code below uses the version from earlier in the tutorial. Each formal element has a place in it.
from dataclasses import dataclass
PLAYERS = 2 # players: two alternating players
MAX_TAKE = 3 # rule: take 1 to 3 stones per turn
@dataclass(frozen=True)
class State:
stones: int # resource: a shared pile of stones
current: int # index of the player whose turn it is
def legal_moves(state):
return list(range(1, min(MAX_TAKE, state.stones) + 1))
def apply_move(state, take):
if take not in legal_moves(state):
raise ValueError('illegal move')
return State(state.stones - take, (state.current + 1) % PLAYERS)
def winner(state):
# objective: take the last stone
if state.stones == 0:
return (state.current - 1) % PLAYERS
return None
def play(stones, choose):
state = State(stones, 0)
while winner(state) is None:
take = choose(state)
state = apply_move(state, take)
return winner(state)
def leave_multiple_of_four(state):
take = state.stones % 4
return take if take else 1
print(play(10, leave_multiple_of_four))This prints 0. Player 0 takes 2 from a pile of 10, leaving 8, which is a multiple of four. From then on, whatever player 1 takes, player 0 can restore a multiple of four. Player 1's fallback move of 1 stone only delays the loss.
Two design choices in the model matter for analysis. legal_moves holds the rule, so a rule change touches one place. winner holds the objective, so an objective change touches one place. Keeping the elements in separate functions is what makes controlled experiments cheap.
Step 4: Change one element and predict the effect
Changing the objective
Change only the objective so that taking the last stone loses. This version of the game is called misère play.
def winner_misere(state):
# objective changed: whoever takes the last stone loses
if state.stones == 0:
return state.current
return NoneThe players, rules, and resources are unchanged. Still, the strategy leave_multiple_of_four is now wrong. The good target is no longer a multiple of four, because the last stone has become something to avoid.
A prediction should be specific. Here is one: "Under the new objective, the player to move loses when the pile is one more than a multiple of four." The reasoning is that a player facing one stone must take it and lose. A player facing two, three, or four stones can take enough to leave exactly one. A player facing five stones can only leave two, three, or four, so they lose with correct play from the opponent.
Checking by computation
You can test a prediction like this without playing many games. The function below computes whether the player to move can force a win, for any pile size, maximum take, and objective.
from functools import lru_cache
@lru_cache(maxsize=None)
def mover_wins(stones, max_take, misere):
"""True if the player to move can force a win with correct play."""
if stones == 0:
# The previous player took the last stone.
# Normal play: they won, so the mover lost.
# Misere play: they lost, so the mover won.
return misere
return any(
not mover_wins(stones - take, max_take, misere)
for take in range(1, min(max_take, stones) + 1)
)
def losing_positions(limit, max_take, misere):
return [n for n in range(limit + 1) if not mover_wins(n, max_take, misere)]
print(losing_positions(12, 3, False)) # original game
print(losing_positions(12, 3, True)) # objective changed
print(losing_positions(12, 4, False)) # rule changed: take up to 4
print(losing_positions(12, 4, True)) # both changedThe output is:
[0, 4, 8, 12]
[1, 5, 9]
[0, 5, 10]
[1, 6, 11]Each list holds the pile sizes where the player to move loses against perfect play. The first line confirms the multiple-of-four strategy. The second confirms the prediction about one more than a multiple of four. The third shows that raising the maximum take from 3 to 4 moves the key number from four to five. In general, the period is the maximum take plus one.
Reading the results
The experiments show how the elements depend on each other.
| Change | Element changed | Effect on other elements |
|---|---|---|
| Last stone loses | Objective | Same rules and resources, but the winning strategy changes. The decisive piles shift by one. |
| Maximum take 3 to 4 | Rule | Same objective and resources, but the cycle length changes from four to five. |
| Larger starting pile | Resource | The decisive structure stays the same. Only whether the first player is on a losing pile changes. |
The third row is worth noticing. Making the pile bigger sounds like a significant change, but it adds length without adding any decision that did not exist before. A resource change that does not alter what choices are available mostly changes how long the game lasts.
Step 5: Judge the change against design goals
Computation tells you what happens. It does not tell you whether you like it. For that, name a goal and test against it.
Nim has a weakness that analysis exposes. With perfect play the result is decided by the starting pile, and an informed player never needs to think after the first move. For children learning the game this is fine. For a game meant to stay interesting, it is a problem.
Possible fixes, each tied to an element:
- Players. Give the players hidden information, so neither can compute the position. This adds a new element rather than tuning an existing one.
- Rules. Let each player take from one of several piles. This is the classic form of Nim and has a richer theory.
- Resources. Give each player a limited number of "big takes" to spend. Players now trade off using a large move now against saving it for later, which adds decisions about resources.
- Objective. Score by stones collected instead of taking the last one, so every turn matters and not only the final one.
Each fix changes the structure in a different way. Choose the one that matches the problem you named.
Common mistakes in analysis
- Describing the theme instead of the structure. "Players are space pirates" says nothing about how many there are or what they can do. Write the formal version.
- Listing a rule as an objective. "Take at most 3 stones" is a rule. "Take the last stone" is an objective.
- Forgetting implicit rules. Nim assumes players cannot pass and cannot move out of turn. Your model has to encode these, or it will accept moves the real game forbids.
- Predicting without a stated expectation. If you don't write the prediction first, any result will seem to confirm your intuition.
- Assuming a model of a small game shows how a large game behaves. Nim can be solved by hand. A game with hidden information, randomness, or many players usually cannot, so you test it with playtests and simulation instead of proofs.
Exercise
In the mover_wins function, a player faces 7 stones with a maximum take of 3 under normal play (last stone wins). Which move should they make, and which element of the game does the answer depend on?
Show answer
They should take 3, leaving 4. A pile of 4 is a losing position for the player to move, because the pile size is a multiple of the maximum take plus one. The answer depends on the rule (maximum take of 3) and the objective (taking the last stone wins). Changing either one would change the right move. For example, under misère play with the same rules, the right move would be to take 1 and leave 6, since 5 and 9 are the losing piles but 7 is a winning pile with a move to 5, so the correct move is to take 2.
Exercise
You change Nim so that each player starts with a private allowance of two "double takes," and a double take lets them remove up to 6 stones on one turn. Which formal elements have you changed, and what new kind of decision does this create?
Show answer
You added a resource (the private allowance, which is owned by one player and spent once) and changed the rules (a move may now take up to 6 under a condition). The objective and the number of players are unchanged. The new decision is a resource trade-off: a player chooses between spending a double take now to reach a winning pile and saving it for a later turn when it might matter more.