Skip to content
Game 5 Unit 11 of 128 1 hr learning time

Defensive AI

The AI now blocks your expansion too. Balances offense and defense for smarter play.

9% of Ink War

The Unit 10 AI expands its own territory but ignores the human completely. If you build a large connected region, it won’t try to stop you. This unit adds defensive awareness — the AI now considers blocking human expansion as valuable as growing its own territory.

The result is a significantly more competitive opponent. It’s no longer just racing to fill the board; it’s actively fighting for contested cells.

Run It

pasmonext --sna inkwar.asm inkwar.sna

Unit 11 Screenshot

Watch how the AI positions itself. It doesn’t just expand outward — it claims cells that would otherwise extend your territory.

The Strategic Shift

In Unit 10, the AI scored each cell by counting adjacent AI cells. A cell with 2 AI neighbours scored higher than one with 1. But adjacent human cells were ignored.

Now we count both:

  • Offense: Adjacent AI cells (expanding territory)
  • Defense: Adjacent human cells (blocking expansion)

The combined score determines which cell to claim. A cell adjacent to 2 human cells and 1 AI cell scores 3 — the same as a cell adjacent to 3 AI cells. This makes contested boundary cells high priority.

Combined Scoring

The updated algorithm counts both types of neighbours:

Two function calls now: count_adjacent_ai (from Unit 10) plus the new count_adjacent_p1. The scores are simply added together — no weighting, just raw neighbour count.

Counting Human Neighbours

The new function mirrors count_adjacent_ai, but checks for STATE_P1 instead of STATE_P2:

Same boundary checks, same helper pattern. The only difference is which player state we’re looking for.

Why Simple Addition Works

You might expect defense to need higher weighting — shouldn’t blocking be more important than expanding? In practice, simple addition works well:

  • Early game: Few neighbours of either type, so random cells get picked
  • Mid game: Contested boundaries naturally score highest (neighbours on both sides)
  • Late game: Remaining cells tend to be near both players anyway

The AI doesn’t need explicit priorities because contested cells inherently have high combined scores.

The Behaviour Change

Compare the two AI versions:

Unit 10 (Offense Only):

  • Claims cells adjacent to own territory
  • Ignores human expansion entirely
  • Lets you build large regions unopposed

Unit 11 (Offense + Defense):

  • Values boundary cells between territories
  • Actively contests your expansion
  • Creates interleaved, competitive positions

The defensive AI is noticeably harder to beat. It won’t let you quietly dominate one side of the board.

The Complete Code

Try This: Defensive Weighting

Want the AI to prioritise blocking over expanding? Multiply the defense score:

; Count adjacent human cells (defense)
ld      a, c
call    count_adjacent_p1
add     a, a                ; Double the defense score
add     a, b                ; A = (P1 * 2) + AI

This makes blocking human territory twice as valuable as expanding AI territory.

Try This: Offense Weighting

Or prioritise expansion:

; Count adjacent AI cells (offense)
ld      a, c
call    count_adjacent_ai
add     a, a                ; Double the offense score
ld      b, a
; Count adjacent human cells (defense)
ld      a, c
call    count_adjacent_p1
add     a, b                ; A = (AI * 2) + P1

Different weightings create different AI personalities.

What You’ve Learnt

  • Multi-factor evaluation — Combining multiple scores for better decisions
  • Defense vs offense — Blocking as a strategic consideration
  • Code reuse — New function mirrors existing one with minimal changes
  • Emergent behaviour — Simple scoring creates complex strategy

What’s Next

In Unit 12, we’ll add corner and edge strategy — the AI will recognise that edges have fewer neighbours and corners are strategic positions.

What Changed

Unit 10 → Unit 11