March 2026
Uno AI Agent
Two Uno agents that choose moves by simulating possible futures with Monte Carlo Tree Search, built from scratch in Java.
- Java
- Monte Carlo Tree Search
- UCT / UCB1
- Q-value estimation
- Java Swing
- 2 agents
- 2 and 4 players
- Anytime search
Problem
Uno breaks fixed rules: hidden hands, random draws, several opponents.
There's no clean formula for scoring a hand, and writing a rule for every situation doesn't scale. Each agent has to learn which moves win by simulating possible futures.
Hidden information
Opponents' hands are hidden during play. This teaching lab uses the engine's full-state rollout model, including opponents' hands. It is not an information-set search.
Logic
The agent plays out many imagined games before each move.
Each rollout plays the game forward at random. Averaging many rollouts estimates each move's value, and more time gives sharper estimates.
Why random play works
With enough rollouts, random play averages out. States that are genuinely better produce more wins, however noisily they're played from.
Watch the agent think
Browser re-implementation. Browser version of the search I wrote in Java. Reported results come from the original project.
- Pick UCT or flat Monte Carlo.
- Run or step through rollouts.
- Watch visits and values settle on a move.
Discard pile
Red 9Colour in play: Red
Agent's hand
- Yellow 9
- Yellow Skip
- Green Reverse
- Blue 4
- Red 6
- Red 9
- Wild Draw Four
Opponents hold 7 cards. Deal 9.
Legal moves
- Yellow 90 visits · Q 0.00
- Red 60 visits · Q 0.00
- Red 90 visits · Q 0.00
- Wild Draw Four0 visits · Q 0.00
Run or step to start the search.
This run in your browser: 0 iterations, 0 rollouts, 1 tree nodes. UCB1 uses c = √2. Not a project result.
Reported result: agents consistently outperform random baselines in 2-player and 4-player games.
Logic
Four steps, repeated until time runs out.
Select a path by UCB1, expand one new move, simulate random play, then backpropagate the reward along the path.
Reward shaping
A win scores +1 and a loss -1. When a rollout hits the depth cap first, the reward is the opponents' cards minus mine. The agent learned aggressive card shedding from that signal alone, with no Uno strategy hardcoded.
Anytime search
The search loop runs until the per-move time budget expires, so the agent can stop and return the best legal move found so far.
Select and expand
UCT AGENTUpper Confidence TreesDynamic tree built using UCB1 to balance exploration and exploitation at every node level.
The tree grows one node at a time. At each fully-expanded node, the next child is chosen by maximizing the UCB1 formula. The left term exploits high-performing moves. The right term boosts underexplored ones: nodes with low visit count get a large exploration bonus.
UCB(s,a) = Q̄(s,a) + √(2·log(N(s)) / N(s,a))
Unvisited nodes have N=0, giving UCB=∞, so they are always tried first. Selection walks down until reaching a not-fully-expanded node, then expansion adds one new child.
Simulate
ROLLOUT POLICYUniform random simulationEvery player draws from their legal moves at random. With enough rollouts, the signal rises above the noise.
During simulation, every player, including opponents, selects uniformly at random from their legal moves. The rollout runs until the game ends or a depth cap is hit, then returns the outcome.
With enough rollouts, random play averages out, and states that are genuinely better produce more wins regardless of how noisily they're played from.
Law of large numbers (intuition): With enough random rollouts, the empirical mean reward at a node approaches the expectation under the rollout policy.
REWARD SHAPINGHeuristic when depth cap hitsWin=+1, loss=-1 at game end. Mid-game cap: r = opponents' cards minus my cards. No domain knowledge needed.
Terminal states are unambiguous: win returns +1, loss returns -1. But rollouts often hit the depth cap before anyone goes out. When that happens, a lightweight heuristic fills in.
r = (sum of opponents' hand sizes) − (my hand size)
The agent learns aggressive card-shedding behavior purely from this signal. No Uno strategy was hardcoded.
Backpropagate
BACKPROPAGATIONQ-value propagation up the pathAfter each rollout, outcome flows back through every node visited, root included. UCT's key advantage over flat MCTS.
After a rollout completes and returns outcome r, the update doesn't just touch the leaf. It walks back up the entire path: every (node, action) pair that was traversed gets its Q-value updated.
setQValueTotal(a, getQValueTotal(a) + r) · setQCount(a, getQCount(a) + 1)
This is the defining difference between UCT and flat MCTS. Nodes deeper in the tree accumulate evidence. Future selection decisions are informed by what happened below them.
Repeat until time runs out
ANYTIME ALGORITHMBudget-driven searchThe outer loop runs until the time budget expires. More compute = better Q-value estimates. Stop whenever.
The outer search loop checks the wall clock against a budget derived from the engine's per-move thinking limit. Each iteration adds one rollout sample.
while (currentTime - startTime < maxThinkingTimeMS - buffer)
The agent can be stopped at any point and still return a valid move. This is fundamentally different from minimax, which needs to reach full depth before returning anything useful.
Logic
Two agents, one smarter search.
Flat Monte Carlo spreads rollouts evenly across the legal moves. UCT grows a tree and reuses what it learned deeper down.
Why UCT wins
After every rollout, UCT updates every node on the path, not just the root. Deeper nodes collect evidence, so future choices are informed by what happened below them.
EXPECTED OUTCOME AGENTFlat Monte Carlo evaluationOne child per legal move. Equal rollouts on each. Root Q-values updated after every simulation.
All legal moves at the root are enumerated upfront. For each, the agent runs hundreds of full random rollouts: complete simulated games where every player picks uniformly from their hand. After accumulating outcomes, the move with the highest average wins.
Q(s,a) = total reward / visit count
Simple and effective. The flat structure means every root move gets equal attention: no tree growth, no UCB selection, just repeated sampling until time runs out.
Result
Both agents consistently beat random opponents in 2 and 4-player games.
UCT gets sharper as the tree deepens, and Q-value estimates settle within the time budget for reliable move selection.
The visualizer
I rebuilt the Java Swing frontend at 1280×720 with real Uno card art, smooth rendering and a live metrics strip that tracks each player's hand strength in real time.
The search and the Swing visualizer are my Java; the game engine came with the course.