Smarter AI Bot

Aaron Grincewicz
2 min readJun 2, 2021

Will I end up making this too difficult? 🤷🏻‍♂️

This is going to be fairly short. I just want to explain how I made the enemy AI in my Turn-Based Battle System smarter.

I allowed the AI to make use of items a while ago. It can heal, boost it’s stats, etc. However, the item it used was random, just an integer between 1 and 6, like on a standard die. I wanted the AI to have some kind of logic to determine if the situation is appropriate for certain items. No more using health items when the HP was already full.

In the image above, you’ll see the method that the Character class uses to determine an action to take on its turn, and it’s appropriately named DetermineAction().

The attack and defense moves are still random. If a move doesn’t have any uses left, it’ll re-roll until it gets to something it can use. As far as the items goes, those are no longer random, given that the Character has the necessary items in the inventory. So, you’ll see that if the active unit’s HP isn’t at 100%, it’ll search for a Health Item. Then, if that’s not necessary, and the opponent’s speed is higher, it’ll search for a Speed Buff. Finally, if neither HP nor Speed is needed, it’ll just search for another Equipment Item to use.

Next, I’ll be giving the AI some logic to find an Accuracy Buff if it’s missed consecutive shots. Then, I plan on letting it determine which attacks to use based on the opponent’s remaining HP. So if my unit is almost dead, it might use an attack to deal enough damage to destroy it, rather than risking a lower damage attack and not finishing the job…

--

--