Monday, January 7, 2008

Better AI – Only Creatures

For lack of anything else on my mind, let’s talk about the AI. Currently the computer tries to play the highest costed card in his hand. The idea is that the highest costed cost is the greatest threat that the computer currently can play. Also, the computer’s library is manipulated so that he will draw a land every few turns until he gets to 7. Hopefully this gives the computer the choice of more cards which will help him play better. The rest of the lands are artificially put at the bottom of the computer’s library.

The problem is that the computer is still pitifully weak sometimes. At times the computer plays like this. Turn 1, plays a land. Turn 2, plays a weak non-creature spell. Turn 3, play another weak non-creature spell. By this time I usually have 2 or 3 creatures in play and the computer is woefully outnumbered unless he draws a big 5/5 creature or Wrath of God. (This happens rarely, but not often enough.)

On a side note, MTG Forge cannot allow the computer to go first, it was a programming mistake, so 50% of the time you should just skip your first turn and not play anything. This will allow the computer to go first, which should make the computer a little harder to beat.

It seems like the computer really needs to use a deck with only creatures. Playing a random creature is more threatening than playing a random spell. And maybe the ideal computer deck should be 95% creatures and only 5% spells. A few spells do spice up the game. Cards like Giant Growth are still beneficial to the computer even though he can’t play them during combat.

I hate to just stop there, but I don’t have anything else to say. Well that wraps up my 20th or so, “How to make the AI better” column.

So long and thanks for all the fish,


Rob Cashwalker said...

I think you need to allow for different AI for different decks....

Like, if you give the computer a control deck, it should make different decisions and different 'cheats' than playing an aggro deck, which is what it seems like how it plays now.

Rob Cashwalker said...

As an aside from this, in general, I have an issue with the card programming model you use.

Since finding this game, I have given much thought into making yet another version of this here Magic game.

I started a framework and some graphics routines to handle the cards using VB6. I defined many types of classes and collections to handle as many of the game control elements as I could think of. For example, there's a card class, which contains ability classes and type classes.... There's a stack collection, and the play field for each user is represented by a collection, as is the hand, graveyard, and library.

But the goal of this is to contain the card programming in small modules of VBScript, which can be parsed in real time, using some MS components. The VBScripted code would manipulate the stack or mana pool, and each ability would be an indexed array of methods....

Then anyone can make/fix the cards, one at a time, in small concise pieces.

The AI would be similarly distributed, so that the different styles of play would be scriptable.

Yannick said...

I agree with rob's first comment : as I said before (in my comment in your previous AI-related article, "Curve my mana"), cards may be played differently depending on the kind of deck you're using.

Maybe there should be a "standard behaviour", implementing the typical decisions that a player would make while using a "classic" deck (ie a deck that uses creatures to defeat the opponent, with a bunch of spells/artifacts to assist them). And then, for decks that use a different logic, you override those IA mechanisms, to make the computer think differently in different situations.

But for a start, you should just work on improving the basic Artificial Intelligence of your program. Kinda like when we started playing magic : we first started playing simple decks, before moving to more complicated things. Well I think your AI should first be able to play creature decks, before moving to control deck etc.

Now, how can you improve your AI? Well, let's see how a real player thinks. For this example, we will pretend that you can't plan anything in advance (you can't think : I do that then next turn I'll do that).

Basically, when you look at your hand, you think : which land am I gonna play? And you pick the land that will let you cast a spell from your hand. Then when you want to cast a spell, you use the spell that is better for your current situation, depending on what you have in play, and of course, what your opponent has in play.

For example, you have a Craw Wurm and a Giant Spider in hand, and your opponent has a flying mob, let's say it's an Hypnotic Specter to make things more obvious. If your AI always plays the most expensive card, it will play the Wurm, but the Spider would probably be better in that situation, since it can block the Specter.

So, my idea for this : your AI should evaluate all the options it has, and give them a "rating" (arbitrary value showing their "efficiency"), depending on the situation. Then it chooses the solution with the highest rating. I think this idea can be easily applied for :
- playing lands (choose land depending on other cards in hands, fairly easy to implement, and I suppose it's already implemented that way in your game)
- playing creatures (look at the creatures your opponent controls, and play the best thing you can play ; also check things such as Nevinyrral's Disk, etc)
- Attacking / Blocking
- Countering spells (how threatening is the spell my opponent is trying to cast?)
- ...

Things become more complicated when you also have to consider special abilities of cards in play, though.

Sorry for the long post :)

Forge said...

Different AI's for different decks is what I am trying for in MTG Forge 2.0 It would be relatively easy to program an aggro or control AI if the program is setup in that way.

About rob cashwalker, the way I program cards was just fast and easy. It made the best sense to me at the time. The ideal would be to read the cards at runtime like you are suggesting. In MTG Forge 2.0 each card has it's own separate class instead of one long, insane method.

yannick, yeah the computer should evaluate the board and is hand, but right now the computer executes about two lines of code and determines whether to play a card or not. It would be a big improvement if the computer could decide between playing a instant/sorcery or a creature. This decision is very basic, but it crucial to many games. Theoretically it would be nice if the computer was more advanged than creature or instant/sorcery but right it isn't, ha. Thanks for the comments.