How to Create an AI for a 4X Game

I have been playing around with making a 4x game for some time, and recently stalled out when I had to figure out how to program the AI. 4X games are called 4X because the goals of the game break down into the the following categories eXplore, eXpand, eXploit, and eXterminate. With all of the combinations of things that could happen at any given moment it seemed a bit overwhelming to figure out how to pick which option is the best. The rules design pattern seems perfect for this circumstance. If you don’t know, or need a refresher on what the rules design pattern is, then give the rules design pattern a quick read.

Following this same concept, we create a score for each rule on a scale between 0 and 100. Then we pick the rule that has the highest value. I know, in concept it sounds really easy, but it is kind of hard to follow – how do you convert a move into a value? Let’s see how this works with an example.

All of the moves include a distance, whether it’s the distance to a resource, an enemy or anything else. The farther away the distance is, the lower the score the end result should be since it will require more turns/effort to get where you are going. To recap, if we have the same resource, one close, and another farther away, I want the close resource to have a higher score. In order to accomplish this I need to implement an algorithm that looks something like the following:

A Graph for the Artificial Intelligence Algorithm with the relation of Distance to Score

This graph came from the equation y=sqrt(-x+10) *10. If you don’t remember your algebra days, you can modify the multiplier to stretch the line vertically, and the addition to move the line horizontally. When using this equation, remember to clamp your results to a maximum and minimum amount in case the results falls out of the expected range.

I use this same style of equation to determine how much resources are needed. This may need to be adjusted to take into account how many resources are being used, but if they get taken away it should balance itself out over time.

To get a score for the resources with distance included I use the source below

 double resourceNeed = Math.Sqrt(-resourceAmount + 10) * 20;  // 0 is about 33
 double distanceModifier = Math.Sqrt(-distance + 20) * 10; //0 is about 22

We then need to add the results of those two pieces together. In order to make sure that we stay within our scale we then use the Math.Clamp function to constrain our results to the scale size

 double score = Math.Clamp(
                (Double.IsNaN(resourceNeed) ? 0 : resourceNeed) + (Double.IsNaN(distanceModifier) ? 0 : distanceModifier), 
                0, 
                100);

We can extend this same idea to every aspect of the artificial intelligence. For instance I would extend this to have scores for attacking, running if gravely injured, exploring, etc.

As always you can look at a working example in my repository https://bitbucket.org/AndySattler/aifor4xgame/src/master/

Did you enjoy this post, or want me to go into more depth on these topics? Let me know in the comments below.