Wednesday, July 1, 2009

Two Kinds of AI

Let me briefly describe two different ways to program a Magic AI.

One, the programmer specifies all of the scenarios in which the computer would play Giant Growth. Two, the programmer lets the computer evaluate the outcome of using various cards, this is very flexible when compared to option one. Option one is hard to implement because each scenario has to be specified while option two requires more planning and coding. (Option two would use an algorithm like min-max or alpha-beta.)

MTG Forge uses option one and Wizard’s new Xbox game uses option two. In MTG Forge each card has around 4-10 lines of code that tells the computer if the card should be played. Below is an example of the code from Shatter. The computer will only play it if you, the opponent, have an artifact and it is turn 5 or latter. The idea behind playing Shatter after turn 5 is that hopefully it will destroy something really good instead of a cheap artifact.

public boolean canPlayAI()
return (getArtifact().size() != 0) && (AllZone.Phase.getTurn() > 4);

The code for Dark Banishing just verifies that you have a non-black creature in play. The method getNonBlackCreature() returns the biggest non-black creature.
public boolean canPlayAI()
check = getNonBlackCreature();
return check != null;
So you see that in MTG Forge each card has a little bit of AI logic programmed into every card. The AI can't use card combinations effectively because each card is programmed separately. Likewise the computer doesn't know that it should use both of its Shocks to win the game if you at are 4 life, because each the AI isn't that smart.

And while MTG Forge's AI is very basic, I still attest to the fact that it is very fun.

The lines between the two types of AI's can be blurred. I can envision an AI that could be optimized for a specific deck such as white weenie. The AI would be hardcoded to play cards in a certain order but would also be able to "look ahead". The evaluation function for the look ahead code could be optimized especially for white weenie. This same idea could also be applied to combo decks.


Malenko said...

Hi, I'm programming another Card Engine and I'm trying to implement the second AI option. But this requieres make the engine like a separate "thing" that you can clone, run one card, evaluate, etc.

But I thing that a more complicate question is...what artifact have to destroy the computer? how can computer knows if one card is more important than other in your deck? ;)

Forge said...

Well the idea is that the computer will look into the future by playing the card and will evaluate the result, which is easier said that done.

"But this requires make the engine like a separate "thing" that you can clone, run one card, evaluate, etc."

Yes and you have to work on the evaluate function so that computer doesn't do really stupid stuff, which it will probably do anyways.

Malenko said...

In fact, there's not only one evaluation function, because some cards are better played in certains phases.

My initial idea it's to play only one card and evalute, don't generate all the tree, only one node foreach card in hand. This evaluation will use Magic terms like advantage card.