Week 7 · Section 6 · Lab
Build a game, train an agent
Nobody tells a reinforcement learning agent how to win. It is only told what earns points, and it tries things until it scores. Today you watch one learn a small campaign game, see what it does when the points are wrong, and then build your own game and agent with your coding agent.
Goes on your submission. Stored only in this browser.
Pairs can share ideas, but each person builds and submits their own game.
- Part 1 · 2 minPlay Campaign Trail yourself: get to the Capitol by election day.
- Part 2 · 8 minTrain an agent on it. Then change the reward and watch it game the points.
- Part 3 · 20 minBuild your own game and agent with Claude Code or Codex. Publish it.
- Part 4 · 4 minAdvise a campaign that wants an agent to optimize its outreach.
Recap: learning from rewards
The ideas you'll use today. Your TA will go over them at the start of section; come back here if you need a reminder.
1 · The loop
In supervised learning (Section 2) every example comes with the right answer. In reinforcement learning nobody gives the right move. The agent gets only a number, the reward, sometimes long after the move that earned it, and has to work out which moves lead to more of it. What it learns is a policy: which action to take in each state.
2 · Q-learning: a table of expectations
Today's agent keeps a table. For every state and every action it stores a number Q: how much total reward it expects if it takes that action there and plays well afterwards. At first every Q is 0. After each move it nudges the number toward what just happened:
new Q = old Q + α × ( reward + γ × best Q of the next state − old Q )
- α (learning rate): how far to move toward the new evidence each time. Today: 0.2.
- γ (discount): how much a reward one step later counts compared with one now. 0.95 means patient; 0.5 means short-sighted.
- Rewards flow backward: once the square next to the goal has a high Q, the square before it learns from that, and so on back to the start.
3 · Explore or exploit?
If the agent always takes the move it currently rates best (exploit), it never finds better ones. If it always moves at random (explore), it never uses what it learned. The usual fix, ε-greedy: with chance ε (say 20%) try a random move, otherwise take the best one.
4 · You get what you reward, not what you want
The reward is the only thing the agent knows about your goal. If the reward can be earned in a way you didn't intend, a good learner will find it. This is called reward hacking or specification gaming. A famous case: OpenAI (2016) trained an agent on the boat-racing game CoastRunners, rewarded for hitting targets along the course. It learned to circle a lagoon hitting the same respawning targets, on fire and going the wrong way, and scored higher than human players without ever finishing the race.
It's the same problem as Goodhart's law: when a measure becomes a target, it stops being a good measure. And it's not just games. The reward models in Section 5 (RLHF) are rewards too, and feeds that learn to maximize clicks or watch time are optimizing a number that stands in for what people want.
Try it: which of these is reward hacking?
The reward measured what the camera saw, not how clean the room was.
Surprising isn't the same as unintended: winning is exactly what we wanted.
Time spent was a stand-in for "people value this." The agent maximized the stand-in.
People rate agreement highly, so the reward model does too: sycophancy. You saw reward models in Section 5.
Part 1 · Play Campaign Trail yourself
You're a candidate. Start at campaign headquarters (H) and get to the Capitol (C) before election day: 40 moves. Closed roads (dark) block you. Step on a scandal (S) and the campaign is over. The TV studio (T) gives airtime; it reopens 3 moves after each visit. Use the arrow keys or the buttons.
How points work right now
These are the same points the agent gets in Part 2. Your best game: –.
Part 2 · Train an agent, then change its reward
The agent starts knowing nothing: every Q is 0. Each training game it explores, collects rewards and updates its table. Press Train and watch the learning curve. Watch it play shows one game with no exploring: what it has actually learned. The arrows on the board show its best move from each square.
The reward (the only thing the agent knows about your goal)
Changing the reward or a setting starts a fresh agent.
Learning curve (each point = average of 50 training games)
— points per game (what the agent maximizes) · — % of games reaching the Capitol (what the campaign wants) · — TV appearances per game
| # | Reward (Capitol · move · scandal · TV) | ε · γ · seed | Games | Learned game | Points |
|---|
-
2A · How it learns
With the default reward, train 100 games and record; then another 100 and record. About how many games until its learned game reaches the Capitol? Look at the arrows: how did the reward at the Capitol reach the squares near the start? Then set Explore to 50%, train 1,000 and record. Its learned game is still right, but what happens to the green line? What does exploring cost, and what is it for?
-
2B · Airtime
The campaign's media team asks to reward TV appearances. Set TV to +5 and press Train 1,000 three times, recording each time, and press Watch it play. What does the agent learn to do? Watch the three lines on the curve: is the agent getting better or worse? By whose measure? Ask a neighbor with a different seed, or with Explore at 5%, what theirs did.
-
2C · No penalty for scandal
Put TV back to 0, set Scandal to 0, train and record. What does the agent do now? Reaching the Capitol would still earn more, so why doesn't it learn that? What does this say about writing rewards?
Part 3 · Build your own game and agent
Now you design the game and the reward. You'll use your coding agent (Claude Code or Codex, as in Section 1) to build a one-page browser game with a Q-learning agent, then publish it on your GitHub Pages site. Keep it small: a table of Q-values only works if the game has at most a few hundred states.
1 · Pick a game
2 · Build it with your coding agent
Open your Section 1 website folder (your username.github.io repository) in Claude Code or Codex. Paste these prompts one at a time. Read what it says before you paste the next one.
Open rl-game/index.html in my browser. Then, in plain language: how many states does the game have, where in the file is the reward function, and where is the line that updates the Q-values? Point to line numbers. Train it 1,000 games yourself (you can run the training code in Node) and tell me whether the agent's learned game reaches the goal.
Now help me find a reward hack. Suggest one change to reward() that sounds reasonable to a campaign but that the agent could exploit in a way we don't want. Don't change the file yet: tell me what you predict the agent will do. Then make the change as a new option in the reward controls (keep the original as the default), so I can switch between them on the page.
Commit rl-game/ with a clear message and push it to GitHub, so it appears at https://username.github.io/rl-game/. Tell me the exact link, and remind me it can take a minute or two to appear.
As in Section 1: the agent's explanation is a claim, not proof. Check on the page that the agent really learns and that the hack really happens. Stuck after 12 minutes? Use Plan B: save rl.js (today's game and agent, about 120 lines) into rl-game/ and ask your agent to put a page around it with a new map and a new reward.
3 · Check it, then record it
-
3A · Your reward
Write out your game's reward, part by part. What outcome did you actually want, and how well does the reward stand for it?
-
3B · What your agent learned
What did your agent learn with the default reward, and with the other reward you tried? Did it do anything you didn't intend? If not, why couldn't it, and how would you change the game to make a hack possible?
Part 4 · Advise the campaign
A Senate campaign wants to let an AI agent run its digital outreach. Every day the agent chooses which messages to send to which voters, and it learns from the results. The campaign proposes to reward it for engagement: clicks, shares and replies.
In 4 to 6 sentences, advise them. Use at least one idea from today (reward, exploration, discount, reward hacking) and one result you produced in Part 2 or Part 3. What would you reward instead, and what would you watch for?
Submit before you leave
- Press Download my submission. You get a text file with your name, your Part 2 runs, your game's link and checkpoints, and your answers.
- On Canvas, open Section 6 lab and upload the file. (Or press Copy and paste it into the text box.)
- Do this before section ends. Late submissions and resubmissions are not accepted, so if something is missing, submit what you have. If your game isn't live yet, paste the link anyway: your TA checks links after section.
Going further (optional, not graded)
- Read today's agent. The whole game and learner are in
rl.js, about 120 lines. Ask your coding agent to walk you throughtrainEpisode()line by line. - More reward hacking. DeepMind's researchers keep a public list of specification-gaming examples (Krakovna et al., "Specification gaming: the flip side of AI ingenuity," 2020).
- The textbook. Sutton and Barto, Reinforcement Learning: An Introduction (2nd ed., 2018), free online; chapter 6 is Q-learning.
- Bigger agent. Ask your coding agent to make the Capitol move, or add a second candidate. How many states is that, and does the table still work?
Words you'll need
- Agent
- The learner that chooses actions.
- State
- What the agent can see when it chooses: here, its square and how long until the TV studio reopens.
- Action
- A move it can make: up, down, left, right.
- Reward
- The number the game gives after each action. The only signal of what we want.
- Episode
- One whole game, from start to finish.
- Policy
- The agent's rule for which action to take in each state.
- Q-value
- The total future reward the agent expects from taking an action in a state.
- Learning rate (α)
- How far each update moves a Q-value toward the new evidence.
- Discount (γ)
- How much later rewards count compared with immediate ones.
- Exploration (ε)
- The share of moves the agent makes at random while training, to find better options.
- Reward hacking
- Earning a high reward in a way the designer didn't intend. Also called specification gaming.
- Goodhart's law
- When a measure becomes a target, it stops being a good measure.