**I Trained an AI to Play Clash Royale... It Worked Too Well — Transcript & Summary | SozAI**
Source: https://sozai.app/transcript/trained-ai-play-clash-royale/

Emeka Moemeka builds an AI bot to play Clash Royale, training it with Python and machine learning to compete against his brother.

## Key Takeaways

- AI can be trained to play complex games like Clash Royale using computer vision and reinforcement learning.
- Modeling the bot after Stockfish's tree search approach helps in decision-making despite game complexity.
- Training data from professional matches improves the bot's ability to recognize cards accurately.
- The bot can learn from wins and losses to improve over time but still faces challenges due to randomness.
- Practical application includes competing against human players to test AI effectiveness.

## What the video covers

- Emeka creates a Clash Royale AI bot modeled after the Stockfish chess engine to improve his gameplay.
- The bot uses screenshots and computer vision to interpret the game board and distinguish cards.
- He uses RoboFlow for card recognition and PyTorch to train the bot through reinforcement learning.
- The bot tracks elixir, match time, troop positions, and health to make strategic decisions.
- Clash Royale's complexity and randomness present challenges far beyond chess's fixed setup.
- Emeka tests the bot by having it play against his brother Tobe to see if it can mimic human play.
- The bot performs well, managing to damage towers and win some matches, though not perfectly.
- The video includes gameplay footage, coding explanations, and reflections on AI training.
- Emeka shares technical details and tools used, such as Reflector 4 for screen mirroring.
- The project aims to create a constantly improving AI bot for the 2.6 mirror match deck.

## Chapters

1. 00:00 Introduction and Problem Statement
2. 00:23 Bot Concept and Stockfish Inspiration
3. 00:52 Bot Vision and Board Representation
4. 01:20 Game Complexity and Randomness
5. 01:44 Starting the Bot Development
6. 02:08 Bot's Game View and Card Recognition
7. 02:35 Integrating Card Recognition and Logging
8. 03:26 Training the Bot with PyTorch
9. 04:02 Bot vs. Brother Gameplay Challenge
10. 06:46 Gameplay Analysis and Conclusion

Answers

## Questions about this video

How does the AI bot recognize different cards in Clash Royale?

The bot uses a computer vision model trained with RoboFlow on labeled frames from professional matches to distinguish between different cards on the board.

What machine learning framework is used to train the Clash Royale bot?

The bot is trained using PyTorch, employing reinforcement learning by playing multiple games and rewarding wins while punishing losses.

How does the bot handle the complexity and randomness of Clash Royale compared to chess?

The bot models the game state with a detailed grid and tracks many variables like elixir and troop positions, but the vast number of possible starting positions and randomness make it more challenging than chess.

## Full Transcript — Download SRT & Markdown

00:00

Speaker A

I love Clash Royale. Yes. The only problem is I suck. Oh man. Like a lot.

00:06

Speaker A

Let's change that. I made a Clash Royale bot that plays the game for me. If you want to see it in action, you can skip to the video chapter below. But for those of you who actually care about the process, let me

00:16

Speaker A

break it down. Am I the first YouTuber to make a Clash Royale bot using machine learning? No. Am I the first one to make one this accurate? I honestly think so.

00:23

Speaker A

My bot is a constantly improving AI that can play a game, make a few mistakes, and come back into the next one even stronger. And to do this, I will model my bot after Stockfish. Stockfish is a super powerful chess engine that's been

00:34

Speaker A

able to beat like the best players on Earth for years now. If I can make my bot even half as good, I call this thing success. Basically, Stockfish just works like a tree where each of the branches are different paths the game can go in,

00:43

Speaker A

and each of the leaves are different positions. Every move, Stockfish looks at the current leaf it's on, searches on all the branches connected to it, finds a position it likes, then comes all the way back and makes a move. To emulate

00:52

Speaker A

this, all I need to do is have my bot take constant screenshots of the match, convert it into some type of board, and then use the lessons it's learned from previous games to decide the best move.

01:00

Speaker A

Now, here's the issue. Okay, Clash Royale is a lot more complicated than chess. Chess has 64 squares, while Clash is 256. 512 if you count the opponent squares. These aren't the only playable tiles, though. This prince, for example,

01:10

Speaker A

isn't technically on any playable tiles, but it's still in the arena and needs to be accounted for. This means we'll need to count bridge tiles and river tiles, giving us a grand total of 544 tiles, a lot more than chess. There's also an

01:20

Speaker A

element of randomness here. In chess, both players always start out with the same 16 pieces. In Clash Royale, there's like a hundred cards, meaning that there's over 800 billion different starting positions. Now, you factor in random starting hands for both players,

01:30

Speaker A

and you get a three followed by 16 zeros. 16. I don't know the name of that number.

01:35

Speaker A

You don't know it either. So, neither of us know the name of that number. Where am I supposed to start? You know, I'm starting to stress myself out now. I really... This guy just keeps getting overwhelming. I might just end the whole

01:44

Speaker A

video. All right. Now that we've established our goal, creating the ultimate 2.6 mirror match AI bot, it's time to start. All right, guys. So, I wrote a basic Python script here to initialize each game. Basically, for those of you guys

01:55

Speaker A

who don't really know that much about Clash Royale, in order for the bot to have a full understanding of what's happening in the game, it needs to track its own elixir, match time, its own hand, the troops' positions, and healths

02:04

Speaker A

on the board, and then decide to either move, like place a card, or just wait and not do anything.

02:08

Speaker A

Coding is crazy, man. I'm also using Reflector 4 to mirror my phone to my MacBook screen. And then thanks to this chunk of code, when I run my Python script, boom, we get this little preview of what our bot actually sees. I also

02:19

Speaker A

added this fun little grid here, which is just a representation of how my bot sees and understands the game. When we show the bot this, it's seeing this.

02:27

Speaker A

Make sense? Okay. So, now that our bot can actually see the game board, we need it to be able to tell the difference between one card and another. To do this, I'll be using RoboFlow to create a computer vision model. And the data

02:35

Speaker A

I'll be using to train it comes from this mirror match played by two pros a few months ago.

02:41

Speaker A

So now by breaking this footage down into individual frames and giving the model some example labels, it can now tell the difference between cards. This is huge because other Clash bots I've seen can't tell the difference between a

02:50

Speaker A

hog and a musketeer, for example. Whereas mine can tell its opponent's hog from its opponent's skeletons, from its own ice golem, from each tower, and even tell this musketeer isn't dropped yet.

02:59

Speaker A

Now I just need to integrate this code into my Python script. I said it once and I'll say it again. And now when I start a game, we get this running log of all the cards that are currently on the

03:07

Speaker A

board. And just like that, my bot can officially see. Next up, it's time to... I had to buy a whole new laptop for this part because Apple won't let me play Clash Royale on my Mac. Steve Jobs had

03:17

Speaker A

to go, man. So, I did a little coding off camera and I wrote these two new Python scripts. One which can track the current state of the board. So, its hand, elixir, the match time, and the coordinates of all the troops on the

03:26

Speaker A

board, and one which can place cards randomly on the board. All right, to train the bot, I'm going to be using PyTorch and letting it play like 100 games, rewarding it for wins, and punishing it for losses. Let's do this.

03:36

Speaker A

[Music] And while I wait for my bot to get some practice, I must confess I've been hiding my true intentions for this video. This is my brother Tobe. And among the many other ways you like to stay competitive, playing Clash is

03:53

Speaker A

probably the most common. This was fine when I was younger and I had time to no life the game. But I can't compete anymore. I'm a full-time software engineer now. I'm washed up. That's where my bot comes in. See if I can

04:02

Speaker A

trick Tobe into thinking he's playing me when he's actually playing the bot. Maybe I can finally beat him and reclaim my throne as the best Clash player in the family. Oh, and would you look at that? The boss's ready to play.

04:16

Speaker A

Hello, Tobe. Hello. I'm sure you've heard that I've been working on a video these past few weeks.

04:20

Speaker A

Isn't that right? Mhm. Yeah. So, I just wanted to challenge you to a Magic Clash Royale one-on-one. No funny business. Okay.

04:25

Speaker A

All right. All right. You ready? You got the deck? Yep. What do you got?

04:30

Speaker A

Nothing for that. Don't even elixir trade. Hog coming down and ice. A little late to the cannon. That's fine though. Got ice golem. Counter push here maybe. Yes. Little counter push coming.

04:51

Speaker A

There we go. Oh, push the hog. Get the tower. What is that supposed to pull?

04:56

Speaker A

Let's go. The ice golem push the hog. Yo, I'm about to be in the lead.

05:00

Speaker A

Fancy, man. Come on. Come on. Cycle. Cycle. Cycle. Cycle. Cycle. Cycle. You're good. You're good.

05:06

Speaker A

Bro, you're just sweating, bro. How am I sweating? Give it a chance, bro. Bro, I'm replacing that, man.

05:15

Speaker A

I don't... It's fine. It's fine. I can take that tower, bro. Yeah. Yes. Yes.

05:26

Speaker A

Come on. Oh my god. Three more hits. Three more hits, bro. I'm like, why act like the bots are playing good? Like I'm just... Three more hits, bro.

05:34

Speaker A

I'm just missing candles. Are we going to set up another push? What are we going to do here?

05:41

Speaker A

Oh, that would have been the game fireball. Yo, there's still a chance to take a tower here, bro. Wait, he used his cannon. He needs to go now.

05:51

Speaker A

No, I didn't. That cannon's still alive from the other hog. Oh, no. Come on, man. 60 seconds left. All right. What do you got here with the fireballs?

06:10

Speaker A

These fireballs are not going to need a bait at all. All right. So, as you guys can tell, he did find out about the bot.

06:19

Speaker A

I act like the bots are playing good. Like I'm pretty quickly. And we did get three crown. But you know what? Considering we got one of his towers down to like a thousand HP, I'd say our little bot

06:27

Speaker A

didn't do too bad. Uh, what's that? I lost four other games and barely touched his tower for any of them. Okay. Well, I also managed to win this game when I fake going upstairs to grab my charger and then three crowned him by placing

06:38

Speaker A

and he wasn't looking. You're in the game. So, I think that balances out. Yeah, though. That's it for this video. Let me know what you guys want to see from me next.

06:46

Speaker A

Bye.

Topics: Clash Royale AI bot machine learning computer vision reinforcement learning Stockfish PyTorch game AI Python programming game automation

Study this video

- [Flashcards from this video](https://sozai.app/tools/ai-flashcard-generator/?from=transcript&id=80569)
- [Quiz on this video](https://sozai.app/tools/quiz-generator/?from=transcript&id=80569)

Free, made by AI from this transcript. No signup.


---
This is the markdown twin of https://sozai.app/transcript/trained-ai-play-clash-royale/ — the same content, without the markup.
Published by SozAI (https://sozai.app). Reuse and quotation are allowed with attribution and a link back.
Machine-readable index: https://sozai.app/llms.txt · data API: https://sozai.app/api/
