▦ MATRIX THINKING

GAME LAB 05 · XONIX / MODULE 18

Close a line.
Watch an area grow.

A region is counted by spreading to its four neighbours. Follow each wave, count the reached cells, and see why one region becomes territory.

GO STRAIGHT TO THE EXPLANATION

Try a completed line, then advance the spread.

01 / THE GAME

1 · territory2 · trail3 · player4 · enemy

Enter · play / resume · P · pause
Hold one arrow to move. Leave territory to draw.
Return to blue territory to close the line.
An enemy touching the trail ends the attempt.

This teaching board has 16 × 24 cells. The book uses 32 × 48; the Boolean-layer and region rules are the same. Initial enemy positions are reproducible here.

02 / THE SPREAD, AS A MATRIX

■ · excluded· · not reachedteal · reachedyellow · newest wave

Discovery numbers are wave indices, not game colour IDs. Seed = 0. Four neighbours only: no diagonal growth or wrapping across an edge.

When a line closes, the game freezes while each region is examined. Press C or choose Apply capture after the regions have been counted.

03 / COUNT WHAT HAS BEEN REACHED

Region—
Wave—
New cells0
region.sum()0
previous = region.copy()
region[1:, :]  |= previous[:-1, :]
region[:-1, :] |= previous[1:, :]
region[:, 1:]  |= previous[:, :-1]
region[:, :-1] |= previous[:, 1:]
region &= ~blocked

Every direction reads the same previous state. A wave adds cells one step farther away. When no new cells appear, the region is complete.

04 / COMPARE THE FINISHED REGIONS

RegionAreaEnemy?Decision

A completed line becomes territory. Then examine the remaining free regions.

As in the book: if the line creates more than one region, capture the smallest region without enemies. A tie chooses the last equal candidate in scan order. Other regions are excluded from this search, but do not become territory.

FOUR BOOLEAN LAYERS

The drawing changes.
The masks explain why.

The field keeps territory, unfinished trail, player and enemy layers separate. After closing a line, a temporary blocked mask starts from territory. Each complete region is added to blocked, so it is examined only once.

Read the original Python project ↗

Try a prediction

Choose “Two regions”. Before spreading, predict the area on the left. Count the interior rows and columns, then compare their product with region.sum(). Next try “Choose the smallest”: an empty region is eligible, but only the smallest eligible region is captured.

JavaScript adaptation of Module 18. Growth is deliberately animated; the Python program completes it within one update. Movement is slowed for inspection. The browser pauses on a loss and waits for Restart; the book restarts automatically. Capture completion is measured over interior cells, with a 75% target.