Q-Learning: Compass in Every Tile
Scoring each choice in every place so the best route emerges before your eyes—even when nobody handed you a map of the world.
In the last two chapters, we watched value glow across a maze—each tile holding a single score that whispers how promising it is to stand there. That works wonderfully if you already own a perfect map of the rules. Standing on a tile, you can peek at every neighboring square, see which neighbor glows brightest, and step straight toward it.
Now pause and ask a sneaky question: what if you don't have a map?
Imagine waking up inside a dimly lit puzzle house. Each room has four unlabeled levers on the wall—North, East, South, and West—and pulling a lever whisks you somewhere else, maybe through a secret trapdoor or across a slippery floor. Even if a helpful ghost told you the exact worth of every room in the house, you would still be stuck staring at four blank levers, wondering which lever actually takes you to the good room.
Knowing how good a place is only helps if you know where each action leads. When the world is a mystery, you need to score each choice inside each place.
Splitting Every Tile Into a Compass
The fix is wonderfully visual. Instead of painting each tile with a single solid color, slice every tile diagonally into four glowing directional triangles—one pointing Up, one Right, one Down, and one Left.
Each triangle keeps track of a single hunch: "How good is my future if I stand here and step in this direction?"
- When a move leads toward the golden treasure, that triangle warms from cool slate into luminous cyan and eventually bright gold.
- When a move dumps you into bubbling lava, that triangle burns a warning coral red—even if the other three triangles in the very same tile are glowing cheerfully!
Look what happens the moment you do this: every tile becomes its own built-in compass. You never need to know the rules of the maze in advance. Whenever you want to make your best move, you simply look down at the four triangles under your feet and follow the brightest wedge. (Researchers call these directional scores Q-values—short for the Quality of taking a particular action in a particular spot.)
The Clever Trick of Q-Learning
So how does an agent fill in all those triangles just by wandering around? It borrows the one-step-at-a-time hunch from the previous chapter, and adds one remarkably clever twist.
Suppose you are exploring the maze. Out of sheer curiosity, you try a random step rightward and land on a new tile. Maybe your very next move will be another goofy exploratory detour. Should you punish the rightward triangle for your future clumsiness?
Q-Learning says no! As soon as you land on the new tile, you glance at its four triangles and spot the brightest direction leading onward. You then nudge the triangle you just stepped from toward whatever reward you felt, plus the glow of that best imagined next move.
Q-Learning separates how you explore from what you learn. You are free to wander curiously, stumble down dead ends, and test wild shortcuts—yet every single step updates your compass as if, from the next tile onward, you will play like a grandmaster.
Watch this happen live. In the arena below, every walkable tile starts completely dark—the agent knows nothing. Press Play (or hit Train +25 Episodes for a fast burst) and watch the golden light ripple backward from the treasure, carving a crisp white arrow highway through the maze.
Even better: click any open tile to drop new lava or walls right onto the agent's favorite route and watch its glowing compass sense the danger and reroute around you in real time!
Every tile is split into 4 directional triangles (Up, Right, Down, Left) with a white arrow pointing to the brightest choice. Click any interior tile to cycle Empty → Lava → Wall and watch the agent adapt.
Notice the sparkline on the right of the arena. Early on, the agent pinballs around the corridors, taking dozens of steps—or stumbling into lava—before stumbling onto the goal. The instant it touches the gold once, the triangle pointing into the treasure lights up. On the next visit, the tile behind that one borrows its glow. Episode by episode, the golden wave flows backward all the way to the start, and the "Steps to Goal" curve drops cleanly toward the shortest path.
Try dragging Curiosity down to 0% once the compass has formed: the agent snaps onto the white arrows and glides straight to the treasure every single time. Now click a tile right on that golden highway to turn it into a wall—without curiosity, the agent may take a moment to bump and unlearn, whereas a little curiosity helps it discover the fresh detour right away.
When Optimism Gets Risky: The Cliff Walk
That "assume I'll play perfectly from the next step onward" trick makes Q-Learning a Bold Optimist. Usually, optimism is a superpower—it lets you learn the ideal route even while you are still fumbling around exploring.
But what happens when a single clumsy exploratory slip is catastrophic?
Picture a narrow stone trail hugging a sheer coral cliff, leading straight to a golden treasure on the far side. There are two ways across:
- The Cliff-Edge Shortcut: Step straight along the rim. It is the fastest route in the world—if you never make a mistake.
- The Safe Upper Detour: Climb away from the drop-off, walk across the high ridge, and descend at the far end. It takes a few extra steps, but a random stumble won't send you tumbling into the abyss.
Now put two different learners on this cliff, both walking with the exact same level of Curiosity:
- The Bold Optimist (Q-Learning) always updates its compass using the brightest possible next move. Because it grades the cliff rim as if it will walk a tightrope without ever slipping, its compass locks right onto the dangerous edge. While curiosity is still turned on, an occasional random step sideways drops it off the cliff again and again!
- The Cautious Walker (SARSA) updates its compass using the move it actually ends up taking next—random exploratory slips included. After falling off the cliff a few times during its curious detours, the entire lower ledge starts tasting sour. Its compass naturally curves upward and carves a wide, peaceful highway along the safe top wall.
Both learners are completely rational—they are simply answering slightly different questions. The Bold Optimist asks, "What is the best path if I eventually turn off curiosity and play flawlessly?" The Cautious Walker asks, "What is the safest, highest-scoring path given how I am actually behaving right now?"
Both agents train on the exact same cliff with the exact same Curiosity. Watch the glowing visit trails and directional triangles diverge: the Bold Optimist hugs the cliff edge while the Cautious Walker retreats to the high ridge.
Hit Train +50 Episodes three or four times with Curiosity around 20% and look closely at the two glowing trails. The Bold Optimist draws a razor-thin line right above the coral cliff—and its cliff-fall counter keeps ticking upward whenever curiosity nudges it downward. Meanwhile, the Cautious Walker paints a glowing arch up along the top wall and earns a much steadier average score during training!
Now try one more experiment: slide Curiosity all the way down to 0% (or 5%) and train another burst. Once random slips disappear, the Cautious Walker realizes the danger is gone and gradually drifts closer to the cliff shortcut too—while the Bold Optimist immediately cashes in on the tightrope route it had quietly mastered all along.
Key Takeaways
- Score actions, not just places: Splitting every tile into four directional triangles builds a self-contained compass, so an agent can choose its best move without ever needing a map of the world's rules.
- Value ripples backward into the compass: Each time the agent reaches a reward, the winning triangle lights up, and neighboring tiles borrow that glow on future visits until a full route crystallizes.
- Q-Learning is a Bold Optimist: It updates each move using the brightest possible onward direction, allowing it to discover the ideal route even while still exploring curiously.
- SARSA is a Cautious Walker: It updates each move using the action it actually takes next, steering clear of hazards whenever curiosity makes slips likely.