Sub topic 3
Local Interaction and Shared State
Assume you know loops, functions, dictionaries, and sets. You should also be familiar with a game loop that updates the game state in discrete ticks or turns. This chapter focuses on how nearby entities influence one another through shared state.
Local rules create global patterns
A local interaction is a rule that examines a small part of the game state, usually an entity and its neighbors. A neighbor is another entity within a defined distance or relationship. For example:
- Fire ignites an adjacent fuel tile.
- A guard hears a sound within five meters.
- A unit gains support from friendly units in nearby spaces.
- A cell changes based on the cells surrounding it.
The rule does not need to know the whole map. It only needs a local view. When the game applies that rule many times, effects can travel, combine, and reinforce one another. The resulting pattern may cover the whole map even though no rule explicitly describes it.
Shared state is the data that multiple rules can read or change. A board, inventory, health pool, or list of active effects can all be shared state. Shared state connects local interactions. Fire changes a tile, and that changed tile becomes the input for the next interaction.
Read first, write later
A common source of bugs is changing shared state while still reading it for the same tick. Consider a row of burning grass:
fire grass grassIf the game scans from left to right and immediately writes changes, the first fire might ignite the second tile. The scan then sees the new fire and ignites the third tile in the same tick. The result is different from a system where fire spreads only one tile per tick.
Use two states to avoid this order dependence:
- Read the old state while calculating effects.
- Write all effects into a new state.
- Replace the old state after the tick finishes.
This is called a synchronous update. Every interaction observes the same starting state.
FLAMMABLE = {"grass", "wood"}
NEIGHBORS = ((1, 0), (-1, 0), (0, 1), (0, -1))
def tick(grid):
"""Return the next grid after one synchronous fire update."""
height = len(grid)
width = len(grid[0])
new_grid = [row[:] for row in grid]
for y in range(height):
for x in range(width):
if grid[y][x] != "fire":
continue
new_grid[y][x] = "ash"
for dx, dy in NEIGHBORS:
nx, ny = x + dx, y + dy
inside = 0 <= nx < width and 0 <= ny < height
if inside and grid[ny][nx] in FLAMMABLE:
new_grid[ny][nx] = "fire"
return new_grid
grid = [["fire", "grass", "grass", "water", "grass"]]
for step in range(3):
print(step, grid)
grid = tick(grid)The rule knows only about the current tile and its four neighbors. It does not contain a special rule for the entire row or for water. Water stops the spread because it is not in FLAMMABLE.
Boundaries and update order
Every local rule needs a neighborhood definition. Decide whether diagonal cells count, whether distance wraps around the map, and what happens at an edge. These choices are design rules, not implementation details.
The order of updates also changes behavior. Sequential updates let early changes affect later decisions. Synchronous updates prevent that but may require more memory. Neither method is always correct. Use synchronous updates when all entities should react to the same moment. Use sequential updates when the order itself represents initiative, priority, or physical processing.
Shared state can amplify small effects
A single local change can trigger a chain:
- Fire changes grass into fire.
- The new fire changes another nearby tile.
- Several fires create a larger region of heat.
- The region changes movement options and player decisions.
The global pattern comes from repeated access to shared state. To debug it, record the state before and after each tick, then inspect which local rule changed each cell.
Exercise
Why can updating a grid in place make a fire spread farther in one tick than intended?
Show answer
A newly written fire can be read later in the same scan, so one tick contains multiple chained reactions instead of one simultaneous step.