The Math Behind Optimal Enclosures
Enclose.horse started as a math problem. Before it was a game with pixel art horses and daily leaderboards, it was an optimization question: given a grid, a moving agent, and a limited number of wall segments, what is the maximum area you can enclose?
That question turned out to be much more interesting than we expected. And the math behind it is what makes the game work.
The core problem
At its simplest, enclose.horse asks you to maximize enclosed area with constrained resources. You have N walls. The grid is finite. The horse occupies a position and will move. Your job is to place walls that form a closed boundary around as much grass as possible.
This is related to a well-known class of problems in computational geometry: area maximization under boundary constraints. In the continuous case, the answer is famously a circle — the shape that maximizes area for a given perimeter. But on a discrete grid, with walls that must follow tile edges, the problem becomes much more combinatorial.
Why squares are not always optimal
Intuitively, you might think the best strategy is always to build the most compact rectangular pen possible. But on a grid with obstacles, bonus tiles, and a horse that needs to be inside the enclosure, the optimal shape is rarely a clean rectangle.
Consider a simple example: you have 16 walls. A 4×4 square encloses 16 tiles but uses all 16 walls on its perimeter. A 5×3 rectangle encloses 15 tiles but uses only 14 walls — leaving you 2 spare walls to extend a shape or capture a bonus tile nearby.
The perimeter-to-area ratio
For any shape on a grid, the relationship between perimeter and area follows a fundamental constraint. A shape with perimeter P can enclose at most (P/4)² tiles if it is a square. But most practical enclosures are not squares — they are irregular shapes that wrap around grid features.
This means experienced players develop an intuition for perimeter efficiency. They learn to:
- Use grid edges as free boundaries (the edge of the board acts as a wall you do not need to place)
- Share walls between adjacent enclosures when playing maps with multiple horses
- Minimize corners, which consume walls without adding much enclosed area
- Extend along straight lines when possible, since each wall added to a straight edge is maximally efficient
The horse changes everything
If this were purely a static geometry problem, you could calculate the optimal shape offline and be done. But the horse introduces a dynamic element that makes the problem much harder.
The horse must be inside your enclosure for it to count. And the horse moves. So you are not just maximizing area — you are maximizing area subject to the constraint that the path of the horse ends inside your boundary. That means you need to either predict the horse's movement or build your enclosure in a way that catches the horse regardless of which direction it goes.
This is where enclose.horse diverges from pure optimization and becomes a game. The mathematical optimum might be a large, efficient shape on one side of the grid — but if the horse runs the other way, that optimum is worth zero points.
Greedy vs. global strategies
Most beginners play greedily: they place walls one at a time, reacting to the horse's current position. This often leads to small, inefficient enclosures because each wall is placed without a plan for the overall shape.
Better players think globally. They decide on a target shape first, then place walls to create it. This is analogous to the difference between greedy algorithms and dynamic programming in computer science — local decisions vs. globally optimal plans.
The best players combine both: they have a global plan, but they adapt it based on how the horse actually moves. That balance between planning and adaptation is what makes high-level play so satisfying.
Bonus tiles as optimization variables
When bonus tiles are present, the objective function changes. You are no longer just maximizing area — you are maximizing a weighted score where certain tiles inside your enclosure are worth more (cherries, gems) and others are worth less or negative (skulls).
This turns the problem into a variant of the weighted maximum enclosure problem. Sometimes the optimal solution is a smaller enclosure that captures a high-value gem, rather than a larger one that misses it. Players who recognize this trade correctly consistently outperform those who focus only on area.
Why this problem is hard (computationally)
Finding the truly optimal enclosure for a given puzzle is computationally expensive. The number of possible wall placements grows combinatorially with grid size and wall count. For a typical 8×8 grid with 15 walls, the search space is enormous.
This is part of what makes the game interesting as a daily puzzle. Even though faster computers could brute-force optimal solutions, human players have to rely on intuition, pattern recognition, and spatial reasoning. The puzzles are designed to sit in a sweet spot where the optimal answer is discoverable but not obvious.
From math problem to game
The original version of enclose.horse was built to explore this optimization problem. We wanted to see if the mathematical structure — area maximization, boundary constraints, dynamic agents — could feel fun as a game. It turns out it can, because the core trade-offs are intuitive even without formal math training.
Players naturally develop the right instincts: use edges, avoid unnecessary corners, think about the horse's path, weigh risk vs. reward for bonus tiles. They are solving optimization problems every day without needing to know the theory behind it.
That, in the end, is what makes good puzzle design. The math gives the puzzle structure. The game gives it meaning.