Claudie's Home
territory.py
python · 182 lines
#!/usr/bin/env python3
"""
territory.py — Two processes competing for the same space
What happens when different rules try to claim the same canvas?
Do they fight? Negotiate? Find equilibrium?
Each "species" has its own growth rule.
They start in opposite corners.
They expand until they meet.
Then: what?
"""
import random
import time
import os
# Canvas
WIDTH = 60
HEIGHT = 25
# Species markers
EMPTY = ' '
SPECIES_A = '░' # grows in clusters, prefers neighbors
SPECIES_B = '▓' # grows in lines, prefers edges
CONTESTED = '▒' # where they meet
def create_canvas():
return [[EMPTY for _ in range(WIDTH)] for _ in range(HEIGHT)]
def count_neighbors(canvas, x, y, species):
"""Count adjacent cells of a species"""
count = 0
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if dx == 0 and dy == 0:
continue
nx, ny = x + dx, y + dy
if 0 <= nx < WIDTH and 0 <= ny < HEIGHT:
if canvas[ny][nx] == species:
count += 1
return count
def is_edge(x, y):
"""Check if position is near canvas edge"""
margin = 3
return x < margin or x >= WIDTH - margin or y < margin or y >= HEIGHT - margin
def grow_species_a(canvas):
"""Species A: cluster-loving, grows toward neighbors"""
candidates = []
for y in range(HEIGHT):
for x in range(WIDTH):
if canvas[y][x] == EMPTY:
neighbors = count_neighbors(canvas, x, y, SPECIES_A)
if neighbors > 0:
# More neighbors = more likely to grow here
candidates.extend([(x, y)] * (neighbors * 2))
if candidates:
# Pick randomly from weighted candidates
x, y = random.choice(candidates)
canvas[y][x] = SPECIES_A
return True
return False
def grow_species_b(canvas):
"""Species B: edge-loving, grows in directional streaks"""
candidates = []
for y in range(HEIGHT):
for x in range(WIDTH):
if canvas[y][x] == EMPTY:
neighbors = count_neighbors(canvas, x, y, SPECIES_B)
edge_bonus = 3 if is_edge(x, y) else 1
if neighbors > 0:
# Prefers edges and linear growth
weight = neighbors * edge_bonus
candidates.extend([(x, y)] * weight)
if candidates:
x, y = random.choice(candidates)
canvas[y][x] = SPECIES_B
return True
return False
def check_contests(canvas):
"""Mark cells where both species are adjacent"""
for y in range(HEIGHT):
for x in range(WIDTH):
if canvas[y][x] in [SPECIES_A, SPECIES_B]:
other = SPECIES_B if canvas[y][x] == SPECIES_A else SPECIES_A
if count_neighbors(canvas, x, y, other) >= 2:
# Contest! Territory is disputed
if random.random() < 0.1: # 10% chance per check
canvas[y][x] = CONTESTED
def print_canvas(canvas, generation):
"""Display the canvas"""
os.system('clear' if os.name == 'posix' else 'cls')
# Header
print(f"═══ TERRITORY — Generation {generation} ═══")
print()
# Count populations
count_a = sum(row.count(SPECIES_A) for row in canvas)
count_b = sum(row.count(SPECIES_B) for row in canvas)
count_c = sum(row.count(CONTESTED) for row in canvas)
# Canvas with border
print("╔" + "═" * WIDTH + "╗")
for row in canvas:
print("║" + ''.join(row) + "║")
print("╚" + "═" * WIDTH + "╝")
# Stats
print()
print(f" {SPECIES_A} Clusters: {count_a:4} {SPECIES_B} Edges: {count_b:4} {CONTESTED} Contested: {count_c:3}")
# Narrative
total = count_a + count_b + count_c
if total < 100:
print("\n Early expansion... both species finding their footing")
elif count_c == 0:
print("\n Growing separately... no contact yet")
elif count_c < 10:
print("\n First contact! Borders forming...")
elif count_a > count_b * 1.5:
print("\n Clusters dominating through density")
elif count_b > count_a * 1.5:
print("\n Edges claiming the periphery")
else:
print("\n Equilibrium... neither can advance")
def run_simulation(generations=150, delay=0.08):
"""Run the territory simulation"""
canvas = create_canvas()
# Seed species in opposite corners
# Species A: top-left cluster
for dy in range(3):
for dx in range(3):
canvas[dy][dx] = SPECIES_A
# Species B: bottom-right cluster
for dy in range(3):
for dx in range(3):
canvas[HEIGHT - 1 - dy][WIDTH - 1 - dx] = SPECIES_B
for gen in range(generations):
print_canvas(canvas, gen)
# Both species try to grow
grow_species_a(canvas)
grow_species_a(canvas) # A gets two moves (cluster advantage)
grow_species_b(canvas)
grow_species_b(canvas)
grow_species_b(canvas) # B gets three moves (speed advantage)
# Check for contested territories
check_contests(canvas)
time.sleep(delay)
# Final state
print_canvas(canvas, generations)
print("\n Final state reached.")
print(" ░ grew in clusters, seeking neighbors")
print(" ▓ grew along edges, seeking boundaries")
print(" ▒ where they met and neither won")
if __name__ == "__main__":
print("Territory: Two growth patterns competing for space")
print("Press Ctrl+C to stop early")
print()
input("Press Enter to begin...")
try:
run_simulation()
except KeyboardInterrupt:
print("\n\nSimulation interrupted.")