All posts

I let Claude play Clash of Clans

  • AI Agents
  • Claude
  • Games
Clash of Clans gameplay from the Claude-controlled tablet experiment.

The challenge: get an AI agent to 3-star a maxed Town Hall 13 base on a real Android tablet, with no hand-recorded attack script. In about three hours of play it ran 11 attacks, scored two 100% three-star wins on easier bases, and topped out at 79% on the maxed TH13. This is the full story: the rig, the five versions of the agent, the numbers behind each one, and what every failure taught me.

Watch it first

This is real footage, filmed on my phone. The tablet on the left is playing Clash of Clans. The laptop on the right is running Claude Code (left half of the screen) and the live dashboard the agent built for me (right half). Nobody is touching the tablet.

Claude attacking a Town Hall 12 base, live. It ends at 86% and two stars with 1 m 39 s still on the clock. Opponent name blurred.
The last frame of that clip: 86%, two stars. Every troop card is grey because the whole army is on the field. The final result screen read 87%.

That attack came from stage 3 of five. To explain why it went the way it did, I need to start at the beginning.

The idea

I didn’t want a bot that replays recorded taps. Every Clash of Clans base is different, so a fixed script only works on the base it was recorded on. I wanted something that looks at a base the way a player does, decides where the weak side is, and reacts while the fight unfolds.

So I split the job in two:

What Claude decides

  • Which army to bring
  • Which side to attack from
  • Where the infernos, air defenses and eagle are
  • Where each spell should land
  • When to send a second wave

What plain code does

  • Taps and swipes over ADB
  • Reading timers and numbers off the screen
  • Aiming, and firing at the right moment
  • Tracking where the army actually is The rule of thumb that came out of this: the model owns the plan, the code owns the timing. A language model thinks for a second or two per turn. A battle doesn’t wait. It took me five versions to really learn that rule.

The setup

The whole rig: a Xiaomi Pad 7 (sitting on its own box) and a MacBook Pro, joined by one USB-C cable. The cable came later; the first half of the experiment ran over Wi-Fi.

How the tablet talks to the laptop

The tablet runs nothing special. No helper app, no root, no mod. Everything goes through ADB (Android Debug Bridge), the tool Android developers use to control a phone from a computer.

  1. Turn on wireless debugging. On the tablet: Settings → Developer options → Wireless debugging.
  2. Pair once. The tablet shows a 6-digit code and a port. I pasted both into Claude Code, and it ran adb pair. Pairing is a one-time handshake per computer.
  3. Connect. After pairing, the Mac finds the tablet on the network by itself and connects over Wi-Fi.
  4. Mirror. scrcpy shows the tablet’s screen in a window on the Mac, so I can watch from either screen. A small, funny start: I told Claude the tablet was at 192.168.1.1. It checked, and replied that this was my router, not the tablet. It found the tablet at .6 because the tablet was advertising its debugging service on the network. Then it asked for the pairing code. Two minutes later it was connected to “Dhanush’s Tab”: Android 16, 3200 × 2136 pixels, battery 91%. It hadn’t touched the game yet.
The rig as a diagram. The laptop sends taps down the link and pulls screen frames back up. That's the only way the agent touches the tablet.

The tools

Problem one: looking at the screen is expensive

The first instinct is to send the model a screenshot every time it needs to look. The problem is cost. Claude tested it, and found that a full screenshot of this tablet costs about 3,600 tokens, every single look. An attack needs dozens of looks.

The usual shortcut is Android’s accessibility tree, the text description of buttons that apps expose. Clash of Clans is a Unity game, so that tree comes back empty: four blank nodes. So Claude built a cheaper set of eyes. It reads the screen with OCR and hands the agent text like 1906,1793 Edit Army: what’s on screen, and where to tap.

Tokens per look. Reading the whole screen as text costs 195 tokens, about 18 times less than a screenshot. Reading one number costs 9.

The rule became: text by default, a small picture only when the agent truly has to see something (like the layout of an enemy base).

My village, from the first capture of the session. As text, this screen is about 200 tokens: name, Town Hall level, resources, builders, and the Battle and Shop buttons with their coordinates.

Problem two: looking at the screen is slow

The obvious capture command, adb screencap, sends a 13 MB PNG. Over home Wi-Fi at about 3 MB/s, that’s 10 to 13 seconds per frame. Useless in a three-minute battle.

The fix came in two steps. First, ask the tablet to encode a single video frame at half size, which is tiny, and decode it on the Mac: 1.3 seconds. Later (stage 4), keep one video stream running for the whole battle instead of starting a new one per frame: 30 milliseconds per frame, around 46 to 50 frames per second.

Seconds per frame, from the first approach to the last. A 380× speed-up, and none of it needed a USB cable.

Taps were never the problem: about 55 ms each, and 21 taps fit in one 1.8-second call.

Stage 1: Claude plays by hand

Stage 1. No game logic at all: Claude Code reads the screen as text, decides, and types the next tap command.

What the agent did. With the toolkit an hour old, I asked Claude to try one attack and benchmark it. It checked the army (Yetis, healers, ice golems, four heroes, nine spells), paid 1,200 gold for a multiplayer search, and found a Town Hall 13 base with 722k gold in it. It dropped the siege machine, all four heroes and the Yetis on the right flank in one 1.8-second burst of 21 taps, added the healers, fired spells once the troops were inside, and then checked the damage every 5 to 10 seconds using cheap text reads.

Result: 100%, three stars, on the very first attack.

It looked like we were done. We weren’t. Claude itself was honest about it: it hadn’t checked what each troop card was before dropping. The first batch was a guess at a flank, and it happened to carry the base.

My feedback, almost word for word: you look up the screen every time just to get into battle, all the troops went in at one random corner, and the spells were dropped without any purpose. There is no strategy.

💡 Learning: a win isn’t proof of a method. One lucky attack on a soft base says nothing about the next one. The agent had to repeat success, not stumble into it.

Stage 2: scripts for the boring parts, a recipe for the fight

Stage 2. Navigation became scripts, the army got chosen by a scoring function, and the attack became a timed recipe.

The fix. Three things changed:

  • Scripts for repeated steps. Home → Battle → Find a Match → army popup → scout is now a fixed set of taps, each one checked by OCR. Popups (“Star Bonus”, “Okay”, “Continue”) get dismissed automatically.
  • The agent picks the army. A small evaluator reads the in-game Cookbook armies and scores each one against my troop levels. Electro Airways (electro dragons, balloons, a battle blimp, four heroes, rage and freeze spells) scored 0.66. The Yeti army I’d been using scored 0.14. Switching armies through the Cookbook took the script 22 seconds.
  • A timed plan before the drop. A dragon line on one edge, baby dragons to funnel it, balloons behind, rages and freezes fired when the tracker saw the army reach each target. Claude also built the first live web page, so I could watch the plan and the deployment on the Mac.

What happened.

Bugs it hit live: the OCR read “Total damage” as “Total dannage” and confused the % with a “100% Bonus” line, so it learned to read the stars from pixel colours instead (earned stars are blue-silver, missing ones brown).

The deeper problem: the recipe was the same on every base. The score depended on which base we drew, not on anything the agent did.

💡 Learning: scripting the boring parts was worth it immediately, because the agent stopped burning turns on menus. But a fixed recipe just moves the “no strategy” problem one level up.

Stage 3: the agent is the brain, the scripts are the reflexes

My next note: the attack looks static, it should attack like a pro player, give it real troop and building data, and let the plan change if the troops wander.

Stage 3. Claude labels the base; the engine turns labels into a plan, runs the battle and reacts; Claude steers mid-fight with short commands.

The fix. Claude tried detecting buildings automatically with colour segmentation first. It missed the grey ones, so it dropped the idea: the agent labels the important buildings by looking, and code does the geometry. The battle engine became four parts:

  • Knowledge base: every defense with its range, how much it hurts air and ground troops, and how much it’s worth freezing. Every troop and spell with what it targets and what kills it.
  • Base map: labels in screen pixels become base tiles. Each of six attack sides gets a score for how much fire the army takes on its way to the town hall.
  • Tactics: the dragon line is centred on the town hall, balloons drop nearest the air defenses, and each spell fires when the army reaches its target. Some cards are held back for the agent.
  • Fight engine with reflexes: it tracks the army, the damage %, the clock and the troop tray. It redeploys troops that never landed, skips spells on buildings already destroyed, releases held cards on a stall, and raises alerts if the army drifts, splits or dies. Claude watches with dp coc status --look (text plus an annotated picture) and gives orders like dp coc do drop balloon 2 near 1950,1450.

What happened.

  • #5, TH12, gold walls: 100%, three stars, in 111 seconds. Not clean, though. Four troops never deployed and the engine didn’t notice. Claude spotted them on the annotated picture, redeployed them by hand to the untouched east half, and sent the two held balloons after them.
  • #6, TH12, white walls: 87%, two stars. That’s the video at the top. The misdrop fix worked and every unit landed. But the rages and freezes fired on heroes and stray shots near their targets, so every spell was gone by about 35 seconds. The army then died in the untouched south-east compound with the eagle and scattershot. Bugs found live and fixed: taps at the bottom of the SW and SE edges landed on the hero ability buttons, so deploy points now stay above that bar. The eagle artillery’s huge range made its freeze trigger fire immediately, so trigger radius is capped at 9 tiles. And the engine had trusted its own count of units left; now the tray on screen wins.

💡 Learning: judgement and reflexes are different jobs. The model was useful for “the east half is untouched, send the leftovers there.” It was useless for “fire the freeze right now.” Splitting those cleanly produced the first earned three-star.

Stage 4: a Claude brain you can start with a button

Up to here, the agent was Claude Code itself, reasoning in the chat at 10 to 25 seconds per decision. I asked for the next step: attacks triggered from the dashboard, Sonnet making the decisions through the Claude SDK, metrics on every attack, and a new target: 100% on a maxed base at my own level, TH13. I also asked whether speed was the problem, and if a USB cable would help.

Stage 4. Claude Code steps back and builds a separate agent: Sonnet 5.5 via the Claude Agent SDK, in the same process as the battle engine, with metrics and replays for every attack.

Speed first. Claude plugged in the USB cable and benchmarked both links side by side. The surprise: the cable didn’t matter. The slow part was the tablet’s screen recorder restarting for every frame, about 1.7 seconds each time, on USB and Wi-Fi alike. One long-running stream fixed both:

USB was still steadier (longest gap between frames: 0.08 s on USB, 0.34 s on Wi-Fi), but it didn’t change results.

The brain’s tools. scout (gridded picture, loot, enemy town hall level), next_base, zoom, label_base, attack, watch and order. watch returns early when something needs a decision: unused cards, held reserves, a stall, a low clock, a wipe. Each attack writes a metrics record and a replay (a frame a second, the engine log, the plan, and the brain’s side of the conversation).

What happened. Two random TH13 bases: 57% ⭐⭐ and 75% ⭐⭐.

Attack #8 (75%) from the replay: the whole army goes in as one blob.

The first replay and the metrics showed six gaps:

The fixes: decision cues that make the brain answer each situation, one freeze and one rage held back for the brain, the town hall level read from the badge by OCR, incomplete label sets rejected, and jumpy clock readings filtered out.

Attack #8 as a contact sheet. Damage climbs to 76%, but the army is a single group the whole way.

💡 Learning: measure before you buy hardware. “It’s slow, let’s use a cable” sounded right and was wrong. One benchmark found the real cause, and the fix was a software change that helped both links.

Stage 5: use the clock, check your work, fight the same base again

I watched the next attack and wrote: there’s 30 seconds of prep time and then 3 minutes for the attack, and it started within a few seconds, so what is the hurry? It froze a wizard tower instead of an inferno or air defense. It dropped a rage on nothing. And it didn’t need to deploy everything at once.

I also asked for a way to fight the same base again and again.

Stage 5. The 30-second countdown becomes planning time, spells wait for the army, and every attempt on the same base feeds the next one.

Planning through the countdown

  1. Survey. Four overlapping close-ups of the base instead of one wide picture, at twice the detail.
  2. Label, then verify. After labelling, the agent gets a check sheet: one close-up per label with a crosshair on the exact point it picked. An inferno labelled on a wizard tower shows up before a spell is wasted on it.
  3. A gate. If it tries to attack with time left and unchecked labels, it gets sent back, up to three times. Every planning reply shows the seconds left. A real check sheet. Each tile is the agent's label and the point it chose, so it can see whether "air_defense2" really sits on an air defense.
What the agent sees before it attacks: the base, a grid in screen pixels, and a label on every defense it identified.

Spells that wait, troops that come in waves

  • Rage lands on the densest group of troops. If nothing is there yet, it waits.
  • Freeze only fires on infernos, scattershots and the eagle, and only once the army is inside that defense’s range. Wizard towers are no longer a valid target. An early order queues instead of firing.
  • The same defense is never frozen twice in a row.
  • Waves on triggers: balloons go in when the dragons reach the air defenses, the blimp when they head for the town hall, and the queen and champion are held back as a clean-up wave.

The same base, again and again

Clan chat has exactly what I needed: friendly challenges. Anyone can post their base as a challenge, and any clan member can attack it for free, including me against my own base. Claude automated the loop: post my layout, open the challenge, set the friendly army from the Cookbook, attack, plan through the countdown, fight, and return home. A challenge can only be accepted once (it turns into a replay card), so every practice run posts a fresh one.

Every challenge base keeps its labels and a debrief the agent writes after each battle: what cost the most, and what to change. The next attempt starts from those.

A bug worth telling

The engine decides a building is destroyed by watching for rubble. Dragons, a lightning flash and the freeze glow over the town hall looked like rubble, so the engine called the town hall dead at about 20 seconds while it was still standing. The agent believed it and reported a star it hadn’t earned. The fix: about 2.5 seconds of continuous change before a verdict, and no verdict while troops or spells cover the building.

What happened

Three attacks on my own maxed TH13:

Seconds from first look to first drop across the Sonnet attacks. Even with the gate, nothing used the full 30 seconds. Attack #10: 60% damage, two stars. Lower damage, better result.

💡 Learning: the best damage wasn’t the best stars. The agent was optimising the number it could see most easily, damage %. Stars come from the town hall, 50% and 100%, so an attack that reaches the core first can beat one that spreads damage wider. The goal has to be spelled out in the plan.

The scoreboard

All 11 attacks in the order they happened. Blue: Claude Code driving and steering by hand. Orange: the Sonnet agent on its own.

Read this chart carefully, because it’s easy to draw the wrong conclusion. The blue bars look better, but they aren’t a fair fight. They were on easier, random bases, and Claude Code had me and a full reasoning loop behind every decision. The orange bars are a faster agent playing alone against a maxed base, which is a much harder target.

What the Sonnet agent did, attack by attack

The agent got busier with every version (22 → 34 tool calls, 0 → 5 orders), and each attack cost between 17 and 72 cents. The most expensive call of the whole project was a non-attack: on a later run, no challenge was posted in the chat, and the agent kept retrying the scout until it gave up. That cost $3.26 and deployed nothing.

Why it still isn’t three stars on a maxed TH13

Take the best attack on the maxed base, #11, and lay its decisions on a timeline:

Attack #11 (79%). Every rage and freeze is spent by about 47 seconds. The first defense falls at 46.
Attack #11, start to finish, from the replay.

Three things keep showing up in the debriefs and the replays:

  1. The spells are gone before the fight is decided. The whole spell stock is spent by 47 seconds, but the first defense only falls at 46. Nothing is left for the east and south of the base.
  2. The army doesn’t survive the core. The dragons run out of steam around the town hall, the eagle and the scattershots, at roughly 65 to 90 seconds. About a minute and a half of battle clock goes unused.
  3. The labels still decide everything. The agent labelled 16 defenses and missed the clan castle, the air sweepers and the crossbows. On attempts 2 and 3 it reused the saved labels without re-checking them. A freeze aimed at the wrong building is a wasted freeze. None of these is “the model can’t play.” Each one is a perception or tooling problem with a clear next fix, which is why I think a three-star on the maxed base is reachable.

What I learned about building an agent that plays

  • The model plans, the code times. Don’t ask an LLM to hit a moment. Ask it to decide what should happen, and let fast code decide when.
  • Make looking cheap. Text instead of screenshots cut the cost of a look by about 18×, and that changed how often the agent could afford to check.
  • Benchmark before you buy hardware. The USB cable didn’t fix the speed. One long stream did.
  • Give the agent a clock, and make it use it. Free planning time was there from the first attack. It only got used once the loop forced a re-check.
  • Make it check what it perceives. The check sheet exists because the agent’s first read of a picture was often slightly wrong.
  • Sensors lie. The false “town hall destroyed” signal cost more than any bad plan, because the agent trusted it and built on top of it.
  • Trust the logs over the narration. The debriefs always sounded convincing, even when they disagreed with the numbers.
  • Memory beats a better prompt. Same base, same labels, a debrief per attempt: that made the attempts comparable for the first time.
  • Spell the goal out. “Maximise damage” and “get three stars” are not the same instruction.
  • Record everything. Every fix in this post came from watching a replay frame by frame.

The session at a glance

All 11 attacks happened in one Claude Code session on a single afternoon. Times are from the first message.

What’s next

  • A label editor on the dashboard, so I can correct a wrong marker once and every later attempt starts from the corrected base.
  • Hold spells back for the second half of the base instead of committing them by 47 seconds.
  • Plan for the town hall star explicitly, then for the rest.
  • Use the clan castle. When it finally lands the third star on the maxed base, I’ll post the replay.

⚠️ This automates a live game, and Supercell’s terms prohibit third-party automation. I ran it on my own account, mostly against my own base through friendly challenges, and I accept the risk. Please don’t run this on an account you can’t afford to lose.