I have gotten questions about how the computer AI is programmed. I presume that the AI seems believable and even smart sometimes. The AI is divided up into 2 parts: cards and combat. A different component handles each one.
The AI that plays cards is essentially very simple, it just randomly plays a card in his hand. Most cards have some AI code hardcoded into them. The computer will only play Wrath of God if you have a creature in play. Elvish Piper’s ability will choose the biggest creature in hand to put into play. The code in Giant Growth will only target a creature that will attack. The AI for each card is usually pretty simple, but combined together it makes the computer seem alive.
Some cards and abilities the computer cannot play because I wasn’t sure how to evaluate them. The computer cannot play Thought Courier’s draw a card, discard a card ability or Remove Soul, I couldn’t get the code to work correctly. Thankfully some cards like Pestilence the computer uses very efficiently.
Combat is divided up into attacking and blocking. Basically the computer trades creatures when attacking or blocking. (Trading means that both creatures die.) The computer will not block a non-flyer with a flyer, which is usually the correct thing to do. The AI combat routines sound simple but they were really hard to program. My mind felt twisted after I got done with them. It is really hard trying to handle all attacking and blocking situations. The object ComputerUtil_Attack2 is 125 lines and ComputerUtil_Block2 is 210 lines.
The code blocks creatures in this order.
-safe block: attacker dies, blocker lives
-shield block: attacker lives, blocker lives
-trade block: attacker dies, blocker dies
-chump block: attacker lives, blocker dies
-multiple blockers: attacker dies, some or all blockers die
p.s.
In the future it would be nice if the computer was playing with a control deck that the AI would attack/block differently than if the computer was playing an aggro deck. Currently the computer always uses the same code to attack and block.
Another fundamental question about combat is "Does the AI want the game to go long or short?" If the AI is winning, it will want the game to end quickly so he will attack more aggressively. If the AI is losing and wants the game to go long (in hopes that he draws a card to save him) he would block more passively and wouldn't trade creatures during combat.
As a player, you should often be asking yourself the same question, "Do I want the game to end now or later?"
Attacking and blocking is boring, complicated code that is hard to get to work correctly. I spent a long time hammering out the details and I'm very happy with the results.
ReplyDeleteIn order to make the AI attacking/blocking code as simple as possible the code loops through all the creatures and makes decisions based on that one individual creature.
ReplyDeleteA more sophisticated method would look consider groups of attackers and blockers, like a real human player would.
Cool story you got here. I'd like to read more about this theme. The only thing that blog misses is a few pictures of some gizmos.
ReplyDeleteAlex Watcerson
Mobile phone jammer