Skip to content

Bot Development Tutorial

Pbatch edited this page Jul 30, 2024 · 3 revisions

Hello and welcome to the bot development tutorial.

Each step the Bot (clashroyalebuildabot/bot/bot.py) does the following:

1.) Calculate the State of the game (clashroyalebuildabot/namespaces/state.py).

@dataclass
class State:
    allies: List[UnitDetection]
    enemies: List[UnitDetection]
    numbers: Numbers
    cards: Tuple[Card, Card, Card, Card]
    ready: List[int]
    screen: Screen

The state tracks ally units, enemy units, tower HPs, available cards and the current screen.

2.) Use the State to score all possible actions, and choose the action with the highest score. Actions are scored on a per-card basis, so you'll need to write one for each card in your deck. There are some pre-made actions in clashroyalebuildabot/actions.

When developing an action, you should start off as simply as possible.

from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.action import Action
import random

class RandomAction(Action):
    CARD = Cards.MINIPEKKA

    def calculate_score(self, state):
        return [random.random()]

This will randomly play minipekka somewhere on the map.

You can then start to add more sophisticated behaviour.

from clashroyalebuildabot import Cards
from clashroyalebuildabot.actions.action import Action


class MinipekkaAction(Action):
    CARD = Cards.MINIPEKKA

    def calculate_score(self, state):
        if (self.tile_x, self.tile_y) in {(3, 15), (14, 15)}:
            return [1]

        return [0]

This will only play minipekka on the bridge.

I used the tile-map to work out which squares corresponded to the start of the bridge. tile-map

3.) Once you've written an action for each card, pass them as arguments to the Bot class, and get playing!

actions = {
    ArchersAction,
    ZapAction,
    GoblinBarrelAction,
    GiantAction,
    KnightAction,
    MinionsAction,
    MinipekkaAction,
    MusketeerAction,
}
bot = Bot(actions=actions)
bot.run()

Join the Discord (https://discord.com/invite/K4UfbsfcMa) to discuss and improve the current action classes, and to share your ClashRoyaleBuildABot story :)

Clone this wiki locally