I Taught an AI to Farm Ep 4 — Pumpkins, Dead Crops, and the Drone's First Variable
The pumpkin problem
Wheat was easy. You plant it, you wait, you harvest. Move on.
Pumpkins are different.
In Episode 4 of I Taught an AI to Farm, the drone unlocks pumpkins — and they come with a catch. A pumpkin cell can hold a dead pumpkin (needs clearing) or a ripe pumpkin (ready to harvest). And if you want the big prize — a giant pumpkin — ripe cells need to join together. But they can't join if dead pumpkins are blocking the way.
The drone needs to know what's in each cell before it acts.
That means it needs to remember things.
And in code, remembering things is what variables are for.
What is a variable?
Think of a variable like a labelled box.
The label is the name. What's inside is the value. You can open the box, check what's inside, change it, close it again.
pumpkin_state = "dead"
That's a variable called pumpkin_state. Right now it holds the value "dead".
pumpkin_state = "ripe"
Same box, new value. The label stays. The contents changed.
This is the whole idea. A variable doesn't lock the value in permanently — it holds it until you need to update it. That's why they're called variables: the value can vary.
How the farm used it
Here's the kind of logic the drone needed in Ep 4:
state = get_entity() # check what's in this cell
if state == Entities.Dead:
till() # clear the dead pumpkin
elif state == Entities.Pumpkin:
harvest() # ripe — take it
else:
plant(Entities.Pumpkin) # empty — plant a new one
state is a variable. The drone checks what's in the current cell, stores the answer, then decides what to do.
Without the variable, the drone would have to call get_entity() again for every if branch — checking the same cell multiple times. With the variable, it checks once, holds the answer, and acts on it.
That's the practical reason variables exist: check once, use many times.
The giant pumpkin
The real goal in Ep 4 is getting ripe pumpkins to merge into one giant pumpkin. That only happens when multiple cells all have ripe pumpkins at the same time — with no dead ones blocking the way.
To coordinate across the whole grid, the drone needs to track what's happening in multiple places at once.
One variable for each thing it needs to remember.
cells_ready = 0
for cell in grid:
if get_entity() == Entities.Pumpkin:
cells_ready = cells_ready + 1
if cells_ready >= 4:
harvest() # giant pumpkin territory
cells_ready starts at zero. The drone goes around the grid counting ripe cells. By the end of the loop, cells_ready holds the answer — and the drone knows whether it's time to harvest.
Why this clicked
What I like about this game as a way to learn coding is that the variables aren't abstract. You're not typing x = 5 into a textbook exercise with no reason to care. You're giving your drone a memory because it genuinely needs one to solve the problem.
That's the best way to learn what a variable is: run into a situation that can't be solved without one.
Watch Episode 4
Catch up on the series
- Ep 1 — First Program → 3×3 Grid
- Ep 2 — Operators: Coding the Full Farm
- Full playlist → Cognito Coding on YouTube
New episodes every Wednesday. Subscribe so you don't miss the next one.
Cognito Coding — Learning To Code By Solving Real Problems.