The Farmer Was Replaced - Code Examples

Complete code examples from the game with detailed explanations. Copy and paste these codes directly into your game to improve your farming automation skills.

Example 1: Basic Auto Harvest

The simplest automation script that continuously harvests mature grass.

python
# Basic Auto Harvest - The simplest automation script
# This continuously checks and harvests mature crops
while True:
    if can_harvest():
        harvest()
    # The game will automatically pause between iterations

📊 Code Analysis

6
Lines
0
Functions
1
Loops
1
Conditions

Example 2: 3×3 Farm Auto Traversal

Automatically traverse all tiles and harvest.

python
# 3x3 Farm Auto Traversal - Systematic farming pattern
# This script moves in a grid pattern to cover the entire farm
while True:
    # Always check for harvestable crops first
    if can_harvest():
        harvest()

    # Move to the next tile (East direction)
    move(East)

    # When reaching the end of a row, move to the next row (North)
    # get_world_size() returns the size of your farm (usually 3x3 or 5x5)
    if get_pos_x() == get_world_size() - 1:
        move(North)

Example 3: Multi-Crop Automation (Grass + Bush + Carrot)

Plant different crops based on column position.

python
# Multi-Crop Automation - Different crops per column
# Demonstrates conditional logic and resource management
while True:
    # Always harvest what's ready first
    if can_harvest():
        harvest()

    # Get current position (x-coordinate)
    x = get_pos_x()

    # Column-based planting strategy:
    if x == 0:
        # Column 0: Let grass grow naturally (free resource)
        pass  # No action needed
    elif x == 1:
        # Column 1: Plant bushes for wood
        plant(Entities.Bush)
    elif x == 2:
        # Column 2: Plant carrots (requires soil preparation)
        # Check if ground is soil, if not, till it
        if get_ground_type() != Grounds.Soil:
            till()
        # Ensure we have carrot seeds before planting
        if num_items(Items.Carrot_Seed) < 1:
            trade(Items.Carrot_Seed)  # Buy seeds if needed
        plant(Entities.Carrot)

    # Move to next column
    move(East)
    # When reaching end of row, move to next row
    if x == get_world_size() - 1:
        move(North)

Example 4: Tree Checkerboard Planting

Trees need checkerboard planting to avoid adjacency.

python
# Checkerboard Planting Pattern - Space-efficient farming
# Trees cannot be planted adjacent to each other, so use checkerboard pattern
while True:
    # Harvest any mature crops
    if can_harvest():
        harvest()

    # Get current coordinates
    x = get_pos_x()  # Current column (0, 1, 2, etc.)
    y = get_pos_y()  # Current row (0, 1, 2, etc.)

    # Checkerboard logic: Plant trees on "black squares", bushes on "white squares"
    # This ensures no two trees are adjacent (trees need space to grow)
    if (x % 2 == 0 and y % 2 == 0) or (x % 2 == 1 and y % 2 == 1):
        # "Black squares" - plant trees
        plant(Entities.Tree)
    else:
        # "White squares" - plant bushes
        plant(Entities.Bush)

    # Move to next tile
    move(East)
    # Wrap to next row when reaching end of current row
    if x == get_world_size() - 1:
        move(North)

Example 5: Resource Priority Management

Intelligent resource management, prioritize collecting scarce resources.

python
# Resource Priority Management - Smart resource allocation
# Automatically adjusts farming strategy based on current inventory levels
while True:
    # Always harvest first - this is our primary action
    if can_harvest():
        harvest()

    # Get current position for planting decisions
    x = get_pos_x()

    # Resource-based planting strategy with priorities:

    # PRIORITY 1: Ensure hay supply (essential for trading and feeding)
    if num_items(Items.Hay) < 500:
        # Don't plant anything - let grass grow naturally for free hay
        pass  # No action needed

    # PRIORITY 2: Maintain wood supply (needed for various upgrades)
    elif num_items(Items.Wood) < 300:
        # Plant bushes to generate wood
        plant(Entities.Bush)

    # PRIORITY 3: Build carrot reserves (for food and advanced trading)
    elif num_items(Items.Carrot) < 200:
        # Plant carrots (requires soil preparation)
        if get_ground_type() != Grounds.Soil:
            till()  # Prepare soil if needed
        if num_items(Items.Carrot_Seed) == 0:
            trade(Items.Carrot_Seed)  # Buy seeds if we don't have any
        plant(Entities.Carrot)

    # Move to next tile in systematic pattern
    move(East)
    if x == get_world_size() - 1:
        move(North)

Example 6: Auto Watering System

Automated watering system, speeds up crop growth up to 5x.

python
# Auto Watering System - Advanced crop acceleration
# Maintains water tanks and accelerates crop growth up to 5x speed
while True:
    # WATER MANAGEMENT: Ensure adequate water supply
    # Water tanks are essential for speeding up crop growth
    if num_items(Items.Water_Tank) < 100:
        # Trade for empty tanks if we don't have enough
        trade(Items.Empty_Tank)

    # WATER APPLICATION: Use water when crops need it
    # get_water() returns hydration level (0.0 to 1.0)
    # 0.75 means crops are 75% hydrated - water when below this
    if get_water() < 0.75:
        use_item(Items.Water_Tank)  # Apply water to current tile

    # HARVEST: Always check for mature crops first
    if can_harvest():
        harvest()

    # PLANTING: Maintain carrot crop cycle
    # Prepare soil if needed (carrots require tilled soil)
    if get_ground_type() != Grounds.Soil:
        till()  # Convert turf/grass to soil
    # Ensure we have seeds before planting
    if num_items(Items.Carrot_Seed) < 1:
        trade(Items.Carrot_Seed)  # Purchase seeds if needed
    plant(Entities.Carrot)  # Plant carrot seeds

    # MOVEMENT: Continue systematic traversal
    move(East)
    if get_pos_x() == get_world_size() - 1:
        move(North)

Example 7: Function Encapsulation

Use functions to make code clearer and more readable.

python
# Function Encapsulation - Code organization and reusability
# Break down complex logic into reusable functions

# FUNCTION: Handle movement to next tile in grid pattern
def move_to_next():
    """Move to the next tile, wrapping to next row when reaching end"""
    x = get_pos_x()
    move(East)  # Always try to move east first
    # If we reached the end of current row, move north to next row
    if x == get_world_size() - 1:
        move(North)

# FUNCTION: Handle carrot planting with all necessary preparations
def plant_carrot():
    """Plant a carrot with soil preparation and seed management"""
    # Ensure we have proper soil for carrots
    if get_ground_type() != Grounds.Soil:
        till()  # Prepare soil if needed
    # Ensure we have carrot seeds
    if num_items(Items.Carrot_Seed) < 1:
        trade(Items.Carrot_Seed)  # Buy seeds if we don't have any
    # Plant the carrot
    plant(Entities.Carrot)

# MAIN PROGRAM: Clean, readable logic using our functions
while True:
    # Harvest any mature crops (always do this first)
    if can_harvest():
        harvest()

    # Maintain carrot production if inventory is low
    if num_items(Items.Carrot) < 100:
        plant_carrot()  # Use our function for clean code

    # Move to next position using our movement function
    move_to_next()  # Clean, reusable movement logic

Example 8: Sunflower Energy Optimization

Sunflower optimization strategy, only harvest high-energy flowers.

python
# Sunflower Energy Optimization - Advanced measurement and selection
# This script demonstrates loops, arrays, and optimization algorithms

# PHASE 1: Plant and measure all sunflowers across the farm
sunflowers = []  # Array to store energy measurements
world_size = get_world_size()

# Plant sunflowers on every tile and measure their energy
for i in range(world_size * world_size):
    # Prepare soil if needed
    if get_ground_type() == Grounds.Turf:
        till()

    # Plant sunflower (trade for seeds if we don't have any)
    if num_items(Items.Sunflower_Seed) > 0 or trade(Items.Sunflower_Seed):
        plant(Entities.Sunflower)

    # Measure energy if sunflower is planted
    if get_entity_type() == Entities.Sunflower:
        energy = measure()  # Get sunflower's energy value
        sunflowers.append(energy)  # Store in our array

    # Move to next tile in row-major order
    move(East)
    if get_pos_x() == 0:  # Reached end of row
        move(North)  # Move to next row

# PHASE 2: Find the sunflower with highest energy
max_val = 0
max_index = 0
# Iterate through all measurements to find maximum
for i in range(len(sunflowers)):
    if sunflowers[i] > max_val:
        max_val = sunflowers[i]  # Update max value
        max_index = i  # Remember position of best sunflower

# PHASE 3: Move to and harvest the optimal sunflower
# (In a real implementation, you'd need movement logic to go to max_index position)
# For now, we'll assume we're at the right position
if measure() == max_val:  # Double-check we're at the right flower
    if can_harvest():
        harvest()  # Harvest the highest-energy sunflower