TeamFightTactics - Rules & Strategy - Coding an AI - Part 2


Description

This part 2 of my series on coding an AI to play TFT. This explains the rules of the game and how the various strategy elements will affect my AI design. Topics covered: Unit traits, Augments, Items, XP, Income, board placement, Statistics, Neural Networks, Fuzzy Logic, Training Data Gathering.


Transcript

Hey it’s AI Gamer here where I teach you to code by building AIs to play video games.This is part of my series on TeamFight Tactics. This video will explain the rules of the game and how they will affect my AI design.


The easiest way to explain is to actually hop into a game. If you want to follow along, you can download the game on Mobile or PC from teamfighttactics.leagueoflegends.com. It’s free to play. Although some cosmetic upgrades do cost real money, but they’re completely optional.


TFT is a turn-based strategy game in the AutoChess genre where eight players go head to head building armies and fighting each other. It’s easy to play, but hard to master. There’s no storyline or progression: Each game starts over fresh, with all players on an even playing field. I appreciate this, because there’s no need need to catch up to the veteran players.


The game starts out on a carousel screen where players race to pick their starting unit. I chose to pick the character Sett because he is tanky and he’s holding a Giant’s Belt item, which gives him extra health to help him win the early rounds.


In each round you make a series of decisions to increase the strength of your army before watching them fight. Decisions in previous rounds affect future rounds and the strongest army during the last round wins.


Let me explain all the widgets on the screen.


During each battle, the right side of the screen shows damage dealt, blocked, shielded, & healed, this will help me analyze what changes to make to my team for the next round.


The blue question mark icons are drops during monster battles, you can right-click to walk over them with your avatar which grants you a random reward, like an item.


The center of the screen is the battlefield. Below that is the shop with a list of units available to buy. Purchasing puts them in reserve on my bench until I deploy them to the field. Acquiring three or nine copies of a unit causes them to combine into a much stronger unit. You have limited gold so you should be methodical about which units you purchase.


You can think of the store like a random deck of cards. There are only a certain number of each card available and all players share the same deck. You’ll notice that right now all the units cost only one gold. Each round, stronger cards are shuffled into the deck, so late-game you’ll have access to stronger units. Above the shop shows the statistics on how likely I am to be offered the more expensive units in my hand. There’s also a “Refresh” button, where I can spend gold to shuffle the deck & get new choices in my shop.


On the left are the traits I have activated on the battlefield. Most units have 2 traits & when units with a shared trait battle together, they get a bonus. Each trait bonus is completely unique. Bonuses are tiered, so you might get a small bonus for 2 matching units and a large bonus for 6 matching units. Hovering the icon shows a description of what the trait bonus will give you, it also highlights all units of that type so you can determine which benched units to deploy to the field. Because units have 2 traits, they can potentially have both traits activated at the same time.


You can see when I move units in and out of the battlefield that the widget changes from greyed out to highlighted to show which traits are active. You’ll also notice that I’m initially restricted to a small army. On the left side of my shop you can see my experience level, which represents how many units I can deploy to the battlefield. I earn experience as the game progresses, but I can also click the “Buy XP” button to increase my experience by spending gold.


On the left of the bench are unequipped items. Dragging them onto a unit grants their stats to that unit. Putting two basic items together onto the same unit causes them to merge into an advanced item. You can have a maximum of three advanced items equipped per unit. You gain items during the carousel and monster rounds. Note that you can’t remove an item from a unit once it’s equipped, unless you sell the unit.


The top of the shop shows my current gold. If you hover it you can see my projected income at the end of the round. Income is increased by interest on unspent gold, up to a maximum of five, as well as by winning or losing multiple rounds in a row. You can see your opponents’ interest by the gold coin slots on the right edge of the battlefield.


If you hover over a unit, you can read a detailed description of their stats. Each one is completely different and some synergize with each better than others. For example, you may want a tanky unit in front of your damage dealer to protect them. Each army has strengths and weaknesses, there is no perfect team. For example, a unit with armor might be strong against physical damage but weak against magic.


The right side of the screen shows how much health I have remaining. At the end of each round all units are revived, but losing a battle causes my main avatar’s health to be reduced. Early losses are less impactful than late-game losses. If I lose too many rounds, I’m eliminated from the game.


The last important concept is augments, which are team-wide effects on your army. You get to select one of three choices. You get additional augments periodically throughout the game. You can see my selected augments on the center left edge of the battlefield.


Each round lasts about one minute and they rotate between carousels, monster battles, & Player vs Player battles. Games can last anywhere from fifteen to fourty-five minutes, depending on how quickly you are eliminated.


The game probably doesn’t feel so simple anymore, but remember, none of this is mechanical, you could easily play the game by just making random choices, you’ll get better as you learn the strategy elements over time.


I’m going to keep playing the game in the background while I discuss how I’m going to design the AI based on these strategy elements.


Statistically speaking, there are billions of possible team combinations. It'd be practically impossible to test every one of them, so new strategies are always being discovered. In addition, the game developers make periodic changes to the game to force everyone to come up with new strategies.


Ideally, I would try to gather data on every possible game and let the computer memorize which decision worked best. There are a few problems with this approach. First, the random elements in the game make it impossible to intentionally build the exact combination you want. There are too many possible armies, so it wouldn't be possible to play enough games to see them all. Third, some units are strong or weak against others so part of the strategy is adjusting your playstyle based on your opponents’ moves. There isn’t a perfect combination that wins every game.


My plan for the AI is to break the game down into a series on tiny decisions. For each decision, it needs to look at all the possible moves it could make, and decide which one improves its chances of winning the most. So, instead of thinking about the AI or game as a whole, if I just focus on making the best possible decision each round, hopefully it will lead to winning the game in the end.


Let’s start with a relatively simple decision. At the start of the game, you are limited to a small number of units on the battlefield. Let’s start with 2-unit armies. If the AI can compare every possible group of 2-unit armies to determine which is strongest, it can use that to pick which of the units on my bench to deploy.


In this situation, I want to purchase all of these units, but I only have two gold. Duplicate Sennas is appealing because it’s only one copy away from a Tier-2 unit. The two units with Astral trait are also interesting, because they’re only one unit away from a bonus. However, double bruisers is the most appealing because the trait will be active immediately. The gambling personality in our human nature might take the risk with the double Senna, because early Tier-2 units can be very strong, but there’s a risk I lose several battles while I wait for the third copy.


This decision is a little tricky, but easy enough that I could probably hard-code it with Fuzzy Logic.  However, once I get to large armies, hard-coding will not be possible. So, I need a technique that can analyze data and make educated guesses. In addition, even simple decisions are no longer simple once you take into account items, augments, traits, etc.


A Neural Network is a good option to solve this type of problem. For the previous example, I could design a neural network like this:


I provide two different unit types as input, the data flows through the neurons from left to right. Each node runs a formula to manipulate the data before forwarding it on to the next layer. The final node outputs the confidence that this set of two units is strong together. To make the decision, I simply make a list of every possible choice, send each one separately to the neural network, and choose the one that outputs the highest confidence.


Before this network will function, I first need to train it. This requires me to gather data from as many 2-unit battles as possible and label them by how well they performed. When I feed this data into the network in training mode, it will adjust its internal formulas until it gets as close to my desired result as possible. Since I can’t train it against every possible combination, my hope is that it will give a good guess for data that it has never seen. It’s important that the network doesn’t “memorize” the training data, but instead finds common patterns, otherwise it will fail when it’s tested against new data. 

For example, it should learn that units with a shared trait are generally stronger together than units which don’t share a trait at all. It shouldn’t need to see every possible combination of units in order to recognize this pattern.


This is the basic concept of how a neural network functions. They’re a little like magic and when they’re not working it’s kind of hard know why. If it’s unable to learn any patterns, I will need to keep changing my network design until it works.


You might think that my network design is too simple, for example, it doesn’t include other strategy elements, like items, traits, and augments. It also doesn’t handle teams larger than two units. This is actually intentional. A more complicated network will require more training data, which I know will be hard to gather, Instead, I’m making multiple simple networks that each focus on learning one thing. Then, I’ll combine the output of all of them to make the final decision. For example, one network to determine the best units and a separate network to determine which items to put on each unit.


A neural network is only one possible AI technique. It is able to learn some types of patterns well, but not all of them. I may have to employ additional techniques to help the AI avoid making early game decisions that will ruin the late-game strategy, For example, in TFT you cannot move items from one unit to another unless you sell the original unit first. Equipping items immediately will help you win early rounds, but it’d be better to save them for your stronger 5-cost units that are only available late game. A workaround is to only put them on dispensable units that I plan to sell later. It’s not always easy to know which units are dispensable, because even 1-cost units can provide important trait bonuses to a late-game team.

This type of strategy will be hard for a neural network to learn, it’ll probably be easier to code this with Fuzzy Logic.


That’s an overview on the complexities of building an AI to learn the TFT rules. In a future video I’ll discuss how I’m collecting training data. Let me know in the comments below if there’s any important strategy elements that you think I’m overlooking. 


Hit that subscribe button to be notified as I post progress updates.


I’m going to fast-forward the rest of the gameplay so you can see how it turned out.