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
Friday, June 15, 2007
Subscribe to:
Post Comments (Atom)
3 comments:
A comment about the number of lines of code. You have to, at least, go through it once. Get it working, then when you are comfortable with it and the bugs/kinks are worked out optimize it. That is, figure out a different system to handle it , whether it be state machine or some unique number mapping system. Heck, thats the fun with programming, making the best creation possible. Hmm, or was that being an artist...(http://www.devx.com/devx/editorial/11659)
Thank you for your package
My approach to the number of lines approach is to try to program as compactly as possible to begine with, although I do not sacrifice readability, because it is hard enough debugging just your own code. The smaller the number of lines, the less possible bug (hopefully), and the logic should be more clear.
In this project, I've never re-written any code just to make it shorter. Knuth, I forget his first name said "premature optimization is the root of all problem."
Post a Comment