Skip to content

Lesson 07 — Solve a capture safely ​

Where this lesson fits: the player can complete a trail, but a completed line has no effect until the solver determines which region it separates.

Time: 5–7 hours

Prerequisites and outcomes ​

Complete lessons 02, 04, and 05. By the end, you can explain rasterization, four-neighbour BFS, the region-selection policy, and why the solver must not mutate live arena state.

Prepare and run ​

  1. From your learner project's solution folder, follow tutorial chapter 06 steps 1–7. This introduces solver and commit together; LP3 lessons 07 and 08 study them separately.
  2. Open Source\Systems\AreaClaimingSystem.cs and Tests\Systems\AreaClaimingSystemTests.cs. Run the focused command below and follow the tutorial's checkpoint.
  3. Complete the independent test exercise below in your learner project, not in the generated snapshot.

Code anchors ​

  • last-perimeter/Source/Systems/AreaClaimingSystem.cs
  • last-perimeter/Source/Data/ClaimResult.cs
  • last-perimeter/Source/Entities/Region.cs
  • last-perimeter/Tests/Systems/AreaClaimingSystemTests.cs

Guided operations ​

  1. Start at ProcessCompletedTrail. Identify the cloned grid and explain why it is a scratch copy.
  2. Follow trail rasterization from world-space points to grid-space cells. Check how those cells become temporary boundaries.
  3. Follow FindRegions: find the visited set, queue, and four-neighbour expansion. Estimate its cost in terms of grid width and height.
  4. Inspect the partition guard. A trail that produces fewer than two regions returns no result rather than falling through to a whole-arena capture.
  5. Follow the selection rules: sort by tile count, choose the smallest enemy-free region; if none qualifies, choose the smallest region and return its eliminated-enemy indexes. Equal-size regions retain discovery order; do not assume an additional tie-break rule.
  6. Run the focused suite and compare a valid partition with a degenerate trail.
powershell
dotnet test --filter FullyQualifiedName~AreaClaimingSystemTests

Prove the solver is speculative ​

The existing non-partition test checks for null; it does not explicitly compare the grid. Add these focused assertions to your learner project:

  1. Create Tests\Systems\LearningPlan3CaptureTests.cs with the complete listing below. This new file is not part of the generated snapshot.
  2. Run the command after the listing from your learner project's solution folder. Both tests must be discovered and pass.
  3. Explain why the two snapshots are identical before applying the valid result, but the claimed tile count increases after applying it. The second test also verifies the choice among multiple differently sized enemy-free regions.
csharp
using Microsoft.Xna.Framework;
using LastPerimeter.Data;
using LastPerimeter.Entities;
using LastPerimeter.Systems;
using LastPerimeter.Tests.TestHelpers;

namespace LastPerimeter.Tests.Systems;

public sealed class LearningPlan3CaptureTests
{
    [Fact]
    public void RejectedAndAcceptedSolves_DoNotChangeArenaUntilApplied()
    {
        ArenaSystem arena = new(TestConfigFactory.Create(width: 40, height: 20, margin: 0, tileSize: 4));
        AreaClaimingSystem solver = new(arena);
        TileState[,] before = arena.CloneGrid();
        int countBefore = arena.ClaimedTileCount;

        Trail spur = new();
        spur.AddPoint(new Vector2(0, 8));
        spur.AddPoint(new Vector2(20, 8));
        spur.AddPoint(new Vector2(1, 8));

        Assert.Null(solver.ProcessCompletedTrail(spur));
        AssertGridEquals(before, arena.CloneGrid());
        Assert.Equal(countBefore, arena.ClaimedTileCount);

        Trail split = new();
        split.AddPoint(new Vector2(12, 0));
        split.AddPoint(new Vector2(12, 20));
        ClaimResult? result = solver.ProcessCompletedTrail(split);

        Assert.NotNull(result);
        AssertGridEquals(before, arena.CloneGrid());
        Assert.Equal(countBefore, arena.ClaimedTileCount);

        arena.ApplyClaimResult(result!);
        Assert.True(arena.ClaimedTileCount > countBefore);
    }

    [Fact]
    public void MultipleEnemyFreeRegions_ChooseTheSmallest()
    {
        ArenaSystem arena = new(TestConfigFactory.Create(width: 40, height: 20, margin: 0, tileSize: 4));
        AreaClaimingSystem solver = new(arena);
        Trail trail = new();
        trail.AddPoint(new Vector2(8, 0));
        trail.AddPoint(new Vector2(8, 20));
        trail.AddPoint(new Vector2(24, 20));
        trail.AddPoint(new Vector2(24, 0));

        ClaimResult? result = solver.ProcessCompletedTrail(trail);

        Assert.NotNull(result);
        Assert.Equal(10, result!.CapturedRegion.TileCount);
        Assert.All(result.CapturedRegion.Tiles, tile => Assert.True(tile.X < 2));
    }

    private static void AssertGridEquals(TileState[,] expected, TileState[,] actual)
    {
        Assert.Equal(expected.GetLength(0), actual.GetLength(0));
        Assert.Equal(expected.GetLength(1), actual.GetLength(1));
        for (int x = 0; x < expected.GetLength(0); x++)
        {
            for (int y = 0; y < expected.GetLength(1); y++)
            {
                Assert.Equal(expected[x, y], actual[x, y]);
            }
        }
    }
}
powershell
dotnet test --filter FullyQualifiedName~LearningPlan3CaptureTests

Checkpoint ​

The solver returns a ClaimResult for a valid split, returns null for a non-partition, and leaves the arena's authoritative grid unchanged in both cases.

Common mistakes ​

  • Flood-filling the live arena while still deciding which region to claim.
  • Using diagonal neighbors when the arena is defined by four-connected tiles.
  • Assuming "always avoid enemies" is possible even when every region contains one.

Questions and extension ​

Which tests prove that the selected region is correct rather than merely non-empty? Add an exact-count case for a small known grid.