In 1980, Japanese video game designer Toru Iwatani created one of the most famous arcade games of all time: Pac-Man.
Many players assumed the ghosts moved randomly, but the reality is pure mathematics: arcade Pac-Man is 100% deterministic! Each ghost follows algorithmic targeting rules and timed state machines without random number rolls. Because the game's behavior is completely predictable, players in the 1980s famously discovered that following an exact sequence of moves produced the exact same game every single playthrough.
The Maze Grid & Cartesian Coordinates
Our maze is built on a $28 \times 31$ grid:
Columns ($x$): $0$ to $27$ (from left to right).
Rows ($y$): $0$ to $30$ (from top to bottom).
Pac-Man begins at tile $(x = 13, y = 23)$ facing West (Left).
Blinky (Red) begins outside the ghost house at $(13, 11)$.
Pinky (Pink) begins inside the house at $(13, 14)$.
Inky (Cyan) begins inside the house at $(11, 14)$.
Clyde (Orange) begins inside the house at $(15, 14)$.
Controlling Pac-Man with Code
Instead of twitching a joystick, you program Pac-Man's trajectory in advance:
pac_game() initializes the maze with all 4 ghosts and starts the deterministic engine.
left(n), right(n), up(n), down(n): Move Pac-Man in that direction by $n$ tiles.
wait(n): Pause in place for $n$ units while the world clock continues ticking.
pac_mode("tiles") or pac_mode("seconds"): Choose whether $n$ is discrete grid distance or continuous duration!
Distance vs. Game Time: $d = v \cdot t$
Pac-Man moves at a constant speed of $v = 4\text{ tiles per second}$ ($250\text{ ms}$ per tile).
Tile Mode (Default): $n$ is the number of grid cells. Moving $4$ tiles advances the game clock by $1.0\text{ second}$.
Time Mode: $n$ is duration in seconds or milliseconds. Moving for $1.5\text{ seconds}$ travels $1.5 \times 4 = 6\text{ tiles}$ (or stops against a wall if a dead end is reached).
Scatter Window: During the first $7.0\text{ seconds}$, ghosts are in Scatter Mode, retreating to their home corners (Blinky heads toward the top-right corner). Use this predictable window to harvest pellets safely!
pac_game() left(5) up(3) right(4)
Move the mouse over a dotted box for more information.
Interactive Playback: When you click Run Code, the simulation animates your plan. You can use Play/Pause, Step (⏭), and Reset (↺) to study each position frame by frame.
AI Target Vectors: Check the Vectors box above the canvas to see real-time dotted lines tracking all four ghosts' exact mathematical target tiles!
Iterative Code Planning: If a ghost intercepts you, the status bar displays the exact collision coordinate and timestamp ($T$). You can refine your path, detour down a side corridor, or time your moves to slip past!
Now you try. Expand the path to navigate around the entire lower loop: add left(5), up(3), right(4), up(3), and left(4)!
Type your code here:
See your results here:
Share your code
Show a friend, family member, or teacher what you've done!
Here is a share link to your code:
Need a hint?
💡 What now?
The code has ???? for the 4th move. Replace ???? with 4, then click Run Code to watch Pac-Man navigate the lower corridor and loop back around!
1. Observe how Pac-Man collects all the dots along the path.
2. Notice Blinky in the upper quadrant heading toward the top-right corner during his 7-second Scatter phase.
3. Click Step (⏭) to examine tile alignments step by step.