In the past days, I commited the code that allows the AI to block attackers. I don’t like saying “the blocking AI” because it doesn’t in anyway specify whether the AI should or should not block in a given situation. What it does is simply make it possible for the generic AI code to consider blocking. Before, the AI didn’t know that there was any other possible action than to pass on the declare blockers step. Now, it’s gonna evaluate all the possible blocking situations.
Speaking about that, the number of possible blocking combinations grows very fast with the number of creatures available in play. I hope I can get away with evaluating all of them. If not, I’ll have to insert a very biased heuristic in an otherwise generic minimax algorithm!
When writing some functional tests, I stumbled upon an interesting case. If a player A has two 1/1 creatures while player B has only one 1/1 creature. Should player A attack player B? The answer is “it depends“.
- If player B has 1 life left, then by all means attack, you have a great chance of winning.
- If player B has 20 life left, then you shouldn’t attack, you’ll just lose one of your creature and the opponent will still have 19 life. It’s just not worth it.
Now, where does the switch happens? Would you do the trade if player B had 2 life? 3 life? 5 life? This example made me realize that the value of life actually depends on how much you have. The more life you have, the less each “unit” of life is worth. And I realized that my heuristic method did not take that into account. So I changed my heuristic: it is now proportional to log(life). This function has the properties we are looking for:
- It’s continously increasing (which basically means more life is better).
- It’s derivative is continuously decreasing and tends to 0 (meaning that life is less and less worthy the more you have).
Using that heuristic, going from 2 life to 1 life is considered much more hurtful than going from 20 life to 19 life, as you would expect.
The exact parameters (constants) are very hard to tweak as there are many more factors to consider (creatures, lands, etc). Nevertheless, I expect that the heuristic computation will be a great way to make different kinds of AI with different behaviors and strengths (i.e. AIs that value creatures more than spells, suicidal AIs, etc…).