Game automation · Computer vision
Clash Farm Bot
A personal automation tool that reads the Clash of Clans screen, judges each base by its loot, plays an attack and goes again.
- 39 msper captured frame
- 4 / 4live loot reads after colour-aware OCR
- 2OCR engines that must agree
- 15 sto read a 9,117-character village export
The objective
Farming resources in Clash of Clans is repetitive: search for a base, judge its loot, attack, collect, repeat. The goal was a bot that does this by looking at the screen like a player does, with no game files modified and every decision based on what is visible.
What it does
- 01
A state machine, not a macro
Every tick, the bot works out which screen is showing (home, attack menu, searching, scouting, battle or results) and acts from that. A mis-tap or a lag spike costs one tick instead of derailing the run.
- 02
Reading loot it can trust
Two OCR engines read each loot number and check each other. Keeping only bright, near-neutral pixels separates white digits from gold and purple bars: on live numbers this took reads from 1 of 4 correct to 4 of 4.
- 03
Refusing to guess
If the engines disagree, or a read would start with a zero, the number is rejected and the base skipped rather than attacked on a misread.
- 04
Learning which raid pays
A Thompson-sampling learner varies a few raid choices (collector pairs, goblins per pair, storage pushes) and credits each by the share of the base's loot that came home.
- 05
Recovering on its own
Disconnect dialogs, stuck searches and frozen screens are detected and handled, up to restarting the game with an hourly cap. A run that goes silent logs exactly where it is waiting.
- 06
Run from anywhere
A Qt console runs several accounts on separate emulator instances, keeps a stats ledger, and mirrors controls and alerts to a Discord panel.
How the loop decides
This is the run loop from the project's README. Step through it, or let it cycle. The diagram is drawn for this site; it contains no game art.
Step 1 / 8 · screen
Home
- Recognised by
- the Attack button
- Does
- Every N attacks, runs the recorded chores: collect, train, donate, walls. Then taps Attack.
How it's built
Speed decided the architecture. Two capture methods were measured: an adb PNG screencap took 1,080 ms per frame; PrintWindow on the emulator's render surface took 39 ms (about 26 fps) and still works when other windows cover the game. Frames are read from the window; taps go out over adb, and the two coordinate spaces were verified identical.
Screens are recognised by normalised cross-correlation against templates the user captures once, searched near where each was captured. The game draws its UI at fixed pixel sizes, so exact-scale matching is both accurate and cheap.
An upgrade planner reads the game's own village export, prices every upgrade from a catalogue cross-checked against a third data set (1,219 of 1,221 costs agree), and reasons about what each builder should do next. It does not spend anything yet.
An on-device Android version uses public APIs only (an overlay, AccessibilityService gestures and MediaProjection capture) and needs no root, PC or emulator.
Verified technologies
- Python 3.13
- PySide6
- OpenCV
- NumPy
- Tesseract (optional)
- pywin32
- discord.py
- Kotlin
- MediaProjection
- AccessibilityService
Worth knowing
- Not affiliated with Supercell. Automating Clash of Clans is against its terms of service and can get an account banned; the project's own README says so.
- The source repository is private, so there is no public link.
- Game screenshots from development were left out: they contain Supercell artwork and other players' names.
