4X Strategy
Explore, expand, exploit, exterminate — civilization in miniature | One More Turn
"A great empire, like a great cake, is most easily diminished at the edges."
Prerequisites
| Module | What You Used From It |
|---|---|
| Module 22 (RTS) or Module 23 (TBS) | Real-time or turn-based strategic decision-making, unit management, and resource systems |
| Module 6 - Tower Defense | Economic systems — income per wave, spending on defenses vs. saving for upgrades |
Week 1: History & Design Theory
The Origin
The 4X genre gets its name from the four verbs that define it: explore, expand, exploit, exterminate. Coined by Alan Emrich in a 1993 review of Master of Orion, the label describes games where you guide a civilization from a single settlement to dominance over an entire world (or galaxy). The genre's roots stretch back to board games like Civilization (1980) and early computer games like Empire (1977), but it was Sid Meier's Civilization (1991) that established the template: a procedurally generated map, fog of war, technology research, city management, diplomacy, and multiple victory conditions. What makes 4X endure is its promise of consequence. Every decision — where to found a city, which technology to research, whether to ally with or invade a neighbor — echoes across hundreds of turns.
How the Genre Evolved
Civilization IV (Firaxis, 2005): Widely regarded as the most refined entry at the time. Civ IV perfected the core loop of settling cities, researching technologies, and managing diplomacy. It introduced religion as a soft-power system and great people as milestone rewards. Its lasting contribution was proving that a 4X game could be deeply complex without being impenetrable.
Stellaris (Paradox Interactive, 2016): Moved the 4X formula into real-time with pause, blending grand strategy with space exploration. Stellaris introduced emergent storytelling through event chains and crisis events that made each playthrough feel like a unique science fiction narrative.
Humankind (Amplitude Studios, 2021): Challenged the Civilization formula by letting players change their civilization's culture every era. Its territory system offered a fresh take on expansion, proving the design space still has room for innovation.
What Makes It "Great"
A great 4X game makes you feel like every decision matters — not just now, but fifty turns from now. The interlocking systems (economy, military, research, diplomacy) create a web where pulling one thread tugs on all the others. This depth is what creates the "one more turn" phenomenon — there is always another decision to make, another consequence to see unfold.
The Essential Mechanic
Managing an empire across multiple interconnected systems where every choice has long-term consequences.
Week 2: Build the MVP
What You're Building
A miniature 4X game played on a small hex map. You start with one settler unit and must found a city, explore the map, research technologies, manage your economy, and achieve a victory condition — all in about 30 minutes of play time.
Core Concepts (Must Implement)
1. Hex Grid System
Hex grids are the foundation of nearly every modern 4X game. Hexes have six equidistant neighbors (unlike squares, which have diagonal adjacency problems), making distance calculations uniform and movement intuitive. Cube coordinates are the cleanest way to do hex math.
// Cube coordinates: each hex has (q, r, s) where q + r + s = 0
class HexCoord:
q, r, s // constraint: q + r + s == 0
function hex_distance(a, b):
return max(abs(a.q - b.q), abs(a.r - b.r), abs(a.s - b.s))
function hex_neighbors(hex):
directions = [
{+1, -1, 0}, {+1, 0, -1}, {0, +1, -1},
{-1, +1, 0}, {-1, 0, +1}, {0, -1, +1}
]
return [HexCoord(hex.q + d.q, hex.r + d.r, hex.s + d.s) for d in directions]
function hex_to_pixel(hex, size):
x = size * (3/2 * hex.q)
y = size * (sqrt(3)/2 * hex.q + sqrt(3) * hex.r)
return {x, y}
function pixel_to_hex(x, y, size):
q = (2/3 * x) / size
r = (-1/3 * x + sqrt(3)/3 * y) / size
return cube_round(q, r, -q - r) Why it matters: Hex math is the skeleton of the entire game. Every system — movement, territory, fog of war, combat range — builds on hex distance and neighbor calculations. Cube coordinates eliminate the offset-grid headaches that plague square grids and make algorithms clean and predictable.
Click any hex to select it. Neighbors of the selected hex are highlighted green. Click a second hex to see the distance between them. Cube coordinates (q, r, s) are shown for each hex.
2. City / Settlement Founding and Territory
Cities are the economic engine of a 4X game. When a settler founds a city, it claims the surrounding hexes as its territory. Each hex provides yields based on its terrain. The city grows and its borders expand.
INITIAL_BORDER_RADIUS = 1
class City:
position: HexCoord
territory: Set of HexCoord
population: int = 1
food_stockpile: float = 0
function found_city(player, settler_unit, hex):
city = City(position=hex)
city.territory = hexes_in_range(hex, INITIAL_BORDER_RADIUS)
player.cities.append(city)
remove_unit(settler_unit)
for tile in city.territory:
tile.owner = player Why it matters: Cities turn the hex map from a static landscape into a contested economic engine. The founding decision is one of the most consequential in the game: a city placed on a river with wheat and hills will outperform one on desert for the entire game.
3. Tech Tree / Research System
Players spend accumulated science points to unlock technologies. Each technology takes multiple turns and may unlock new buildings, units, or abilities. The tree has branching paths so players must prioritize.
tech_tree = {
"agriculture": {cost: 20, prereqs: [], unlocks: ["granary"]},
"mining": {cost: 20, prereqs: [], unlocks: ["mine"]},
"bronze_working":{cost: 40, prereqs: ["mining"], unlocks: ["warrior"]},
"writing": {cost: 40, prereqs: ["agriculture"], unlocks: ["library"]},
"mathematics": {cost: 60, prereqs: ["writing"], unlocks: ["walls"]},
"iron_working": {cost: 80, prereqs: ["bronze_working"], unlocks: ["swordsman"]},
"astronomy": {cost: 100, prereqs: ["mathematics", "mining"], unlocks: ["victory"]}
}
function start_research(player, tech_name):
tech = tech_tree[tech_name]
if all(prereq in player.researched for prereq in tech.prereqs):
player.current_research = tech_name
player.research_progress = 0 Why it matters: The tech tree is the strategic backbone. It forces long-term planning: researching military technology means delaying economic improvements. The branching structure means two players will diverge based on research choices.
Click a technology node to begin researching it (prerequisites must be completed first). Watch the progress bar fill. Completed techs unlock downstream technologies. The dependency graph shows connections.
4. Diplomacy Basics
AI opponents have attitudes toward the player based on game events. Actions like expanding near their borders or building a large military change their disposition.
function update_attitude(ai, player, event):
modifiers = {
"settled_near_border": -20,
"declared_war": -50,
"trade_deal": +15,
"gifted_gold": +10,
"military_buildup": -10
}
ai.attitude_toward[player.id] += modifiers.get(event, 0)
clamp(ai.attitude_toward[player.id], -100, 100) Why it matters: Diplomacy transforms the AI from a faceless obstacle into a character with motivations. Even a simple attitude system creates meaningful moments.
5. Turn-Based Macro Economy
Each turn, every city generates yields: food for growth, production for building, gold for maintenance, science for research. The economy operates at a per-turn granularity.
function process_economy(player):
total_income = 0
total_expenses = 0
for city in player.cities:
yields = calculate_city_yields(city)
city.food_stockpile += yields.food - (city.population * FOOD_PER_POP)
city.production_stockpile += yields.production
total_income += yields.gold
for unit in player.units:
total_expenses += unit.maintenance_cost
player.gold += total_income - total_expenses Why it matters: The economy connects every system. Military units cost gold to maintain, so a large army is a constant drain. This creates the fundamental 4X tension: growth has costs, and unchecked expansion can bankrupt an empire.
6. Victory Conditions
Multiple victory conditions give players different strategic goals. Having more than one victory path means the player must decide which to pursue and which to defend against.
victory_conditions = {
"domination": {
check: (player, game) => all other players have 0 cities
},
"science": {
check: (player, game) => "astronomy" in player.researched
AND player.has_built("space_program")
}
} Why it matters: Multiple victory conditions are what make 4X strategy truly strategic. A player pursuing science must still maintain enough military to not be conquered.
7. Fog of War at Civilization Scale
The map starts unexplored. As units move, they reveal terrain around them. Explored but unoccupied areas become "shrouded" — visible terrain but not current enemy positions.
enum TileVisibility: UNEXPLORED, SHROUDED, VISIBLE
function update_fog_of_war(player, hex_map):
for tile in hex_map:
if tile.visibility[player] == VISIBLE:
tile.visibility[player] = SHROUDED
visible_sources = player.units + player.cities
for source in visible_sources:
range = VISION_RANGE_CITY if source is City else VISION_RANGE_UNIT
for hex in hexes_in_range(source.position, range):
tile = hex_map.get(hex)
if tile:
tile.visibility[player] = VISIBLE Why it matters: Fog of war creates information asymmetry — the most important ingredient in strategic tension. This uncertainty forces scouting and makes exploration genuinely exciting.
Stretch Goals
- Multiple AI opponents: Add a second AI player to create three-way diplomatic dynamics.
- Unit combat system: Implement rock-paper-scissors unit types.
- City production queue: Let cities queue multiple buildings and units.
- Map generation: Procedurally generate the hex map with noise functions.
MVP Spec
| Element | Scope |
|---|---|
| Map | 10x10 hex grid with 3-4 terrain types |
| Resources | 2 resource types placed on specific hexes |
| Cities | Found cities with settlers, cities claim surrounding hexes |
| Tech tree | 8-12 technologies in a branching tree |
| Economy | Per-turn yields: food, production, gold, science |
| Diplomacy | 1 AI opponent with attitude tracking |
| Victory | 2 conditions: domination and science |
| Fog of war | Unexplored/shrouded/visible states |
| Units | 3 types: settler, warrior, scout |
Deliverable
A playable miniature 4X game on a hex map where you found cities, research technologies, manage an economy, interact with one AI opponent, and pursue one of two victory conditions. A complete game should take approximately 30 minutes.
Analogies by Background
For Backend Developers
| Concept | Analogy |
|---|---|
| Hex Grid System | Like a distributed hash ring — nodes are evenly spaced, each responsible for a region, and neighbor lookups use consistent coordinate math. |
| City Founding | Like provisioning a new server in a region — it claims resources, serves nearby requests, and has ongoing operational costs. |
| Tech Tree | Like a dependency graph in a build system — you cannot build a target until its prerequisites are satisfied. |
| Victory Conditions | Like SLA compliance checks — multiple metrics are evaluated each cycle, and meeting any one threshold triggers a state change. |
| Fog of War | Like service discovery with TTL — known endpoints go stale if not refreshed. |
For Frontend Developers
| Concept | Analogy |
|---|---|
| Hex Grid System | Like a CSS hex grid layout — each cell has a defined position and neighbors are calculated from coordinates. |
| City Founding | Like creating a new component scope — the component claims its DOM territory and manages its own state. |
| Tech Tree | Like a feature flag dependency chain — enabling a flag requires its prerequisites to be active. |
| Economy | Like a render budget — each frame has income and expenses, and overspending causes degradation. |
| Fog of War | Like lazy loading — unloaded sections show a skeleton, loaded sections show real data. |
For Data / ML Engineers
| Concept | Analogy |
|---|---|
| Hex Grid System | Like a spatial index structure — coordinates enable efficient neighbor queries and range searches. |
| Tech Tree | Like a DAG of data pipeline stages — downstream transformations cannot run until upstream dependencies complete. |
| Diplomacy | Like a reinforcement learning reward signal — the AI's attitude is a running score that drives policy decisions. |
| Victory Conditions | Like early stopping criteria — multiple metrics are checked each epoch, and hitting any threshold ends the run. |
| Economy | Like a data pipeline cost model — each stage consumes resources, and a deficit forces pruning. |
Discussion Questions
- Scope as the Enemy: 4X games are notorious for scope creep. How do you decide which systems are essential for the core loop? What is the smallest set of interlocking systems that still feels like a 4X game?
- The AI Problem: A competent 4X AI is one of the hardest problems in game development. For the MVP, what is the minimum AI behavior that makes the game feel like a contest rather than a sandbox?
- The Pacing Trap: 4X games often start exciting and become tedious in the midgame. How would you design a 30-minute 4X game that keeps the pacing tight throughout?
- Information Overload: A 4X game presents dozens of numbers per turn. How do you surface the right information at the right time without overwhelming the player?