Sub topic 13
Analyzing and Modifying a Game with Formal Elements
Assume you know common board, card, or video games and can read simple Python. This chapter uses the formal elements of players, objectives, rules, resources, and procedures to inspect a small game. The goal is to predict what changes when one element changes.
Start with a formal model
A formal model describes what the game requires, not what it looks or sounds like. For a stone-taking game, the model can be written like this:
| Element | Specification |
|---|---|
| Players | Two players alternate turns |
| Objective | Take the last stone |
| Rules | A player takes one, two, or three stones |
| Resource | A shared pile of stones |
| Procedure | Choose a legal number, remove the stones, and pass the turn |
This description leaves out the theme, graphics, and story. That is useful because you can test the system without confusing presentation with structure.
When analyzing a game, ask four questions:
- Who acts, and how can players affect one another?
- What condition defines success, failure, or completion?
- Which actions and states does the ruleset allow?
- Which resources change, and what choices do they create?
Find the important relationships
The elements matter because they constrain one another. In Nim, the objective gives meaning to the resource. The pile is valuable because taking its last stone wins. The rule limiting each move creates decision-making. If players could take any number, the game would usually become much shorter and less interesting.
The procedure connects intention to state change:
- The current player selects a number.
- The rules check whether the number is legal.
- The selected stones are removed.
- The turn changes.
- The game checks whether the objective was reached.
A useful analysis records the state before and after each procedure. Here, the state contains the resource count and the current player.
from dataclasses import dataclass
PLAYERS = 2
MAX_TAKE = 3
@dataclass(frozen=True)
class State:
stones: int
current: int
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):
if state.stones == 0:
return (state.current - 1) % PLAYERS
return None
def play(stones, choose):
state = State(stones, 0)
while winner(state) is None:
state = apply_move(state, choose(state))
return winner(state)
def leave_multiple_of_four(state):
remainder = state.stones % 4
return remainder if remainder else 1
print(play(10, leave_multiple_of_four))The strategy tries to leave a multiple of four stones. If the count is not a multiple of four, it removes the remainder. If it is already a multiple of four, no move guarantees control against perfect play, so it removes one legal stone.
Modify one element at a time
Controlled modification means changing one formal element while holding the others constant. This makes the resulting effect easier to explain.
For example, change only the objective:
- Original objective: the player taking the last stone wins.
- New objective: the player taking the last stone loses.
The players, resource, move limit, and turn procedure remain the same. However, the best choices change near the end of the game. With one stone left, taking it is now dangerous rather than desirable. This is called a misère objective.
def winner_misere(state):
# The player who takes the last stone loses.
if state.stones == 0:
return state.current
return NoneThe changed function also reveals a design issue: the procedure and outcome logic must agree about what happens after the final move. A complete implementation would use winner_misere throughout its turn loop.
Use the model to diagnose problems
Formal analysis helps identify why a game feels unbalanced or unclear.
- If one player always wins, inspect turn order, starting resources, and objective timing.
- If choices feel meaningless, inspect whether rules create useful trade-offs.
- If players cannot tell what to do, rewrite implicit rules as explicit procedures.
- If the game never ends, find a resource that decreases or a state that moves toward an end condition.
- If a resource has no effect on decisions, remove it or give it a clear cost and use.
A strong modification changes a specific relationship and predicts its consequence. For example, increasing MAX_TAKE from 3 to 4 changes the winning pattern from multiples of four to multiples of five. The objective and resource stay the same, but the rules change which resource states are favorable.
Exercise
In the original stone game, what formal elements change if players may take one to five stones instead of one to three, and what should you inspect next?
Show answer
The move-limit rule changes. Inspect the winning pattern, legal-move function, turn procedure, and whether the resource still produces meaningful choices.