← Back to Work
focused focused

Building a Pac-Man Level Generator (That Doesn't Build Broken Maps)

The premise of this project was simple: investigate whether a machine-learning-inspired probabilistic approach could automatically generate new, playable Pac-Man levels.

Instead of hand-crafting every map, I wanted a system that could look at original retro layouts, learn why they worked, and spin up brand-new ones on demand.

The Strategy: Spatial Markov Chains

A standard Markov Chain is usually used for sequences, predicting the next state based on the previous one. To make this work for a 2D grid-based arcade map, the generator had to predict what tile should appear at a specific position based on its local neighbors.

If a pure local model is left to its own devices, it completely loses the plot and forgets the global structure — it starts putting ghost chambers in random corners. To fix this, I engineered an enhanced contextual state by looking at five distinct features:

By adding those horizontal and vertical regions, the generator learned global context: top regions tend to contain long horizontal wall structures, center regions need a dedicated ghost chamber, and lower regions should be more open for player traversal.

C#
// The DNA of a tile position
markovState += leftTile + upperTile + upperLeftTile + yThird + xThird;

During training, the system crunched existing maps, counted which tiles followed which states, and normalized those numbers into valid probability distributions. When generating a level, it samples these probabilities tile-by-tile, introducing controlled randomness without creating utter chaos.

The Core Challenges

The final evaluated models generalized beautifully, hitting a test score of ~0.798. This proved the system was actively capturing meaningful structural layouts rather than just memorizing and copying the training maps.

Going Rogue: The Rule-Based Generator

Alongside the learned Markov model, I implemented an extra-credit procedural generator that took the opposite approach: strict, handcrafted rules.

Instead of relying on probabilities, this generator guarantees a classic arcade feel by using clean, hardcoded layout constraints:

Future Improvements

While the current generator builds some incredibly nostalgic layouts, there are a few things I'd love to add next:

The Takeaway

Local statistical models can generate surprisingly believable, highly replayable game content if you design the underlying state representation with enough care.


The project is fully functional inside Unity, proving that you don't always need a heavy, unoptimized neural network to generate smart content. Sometimes, a well-tuned probability matrix and some clever spatial logic are all it takes to keep the ghosts moving.