Thursday, August 30, 2007

Programming Magic 3

The AI (artificial intelligence) is the most important aspect of MTG Forge. Yes, Magic is a fun game, but you need to have a challenging opponent.

I’ll let you in on a little secret. I don’t have the Internet at home. Are some of you shocked? I don’t have cable either, ha. Double shock!! But don’t worry I have used Magic Online and at first I was very impressed with everything. The games played very smoothly and I understood the phase stops and everything. Unfortunately after awhile I got irritated waiting on my opponents and “random” disconnects when I was winning. The high card prices also frustrated me, so MTG Forge was born. My goal was to provide fast, fun games of Magic. I am very happy that MTG Forge is able to simulate drafting and sealed games. Currently sealed is my favorite and I often make 2-3 decks with the same pool of cards in order to see which configuration works the best. Sometimes I play “speed Magic” and see how fast I can click and still win.

OK, back to the nitty-gritty of the AI. The AI of course has many different parts. The main skeleton is named ComputerAI_General and it calls the other parts of the AI depending on the phase. This part of the AI has code which handles playing a land, paying for a card, attacking, and blocking. Each of these separate parts has its own code, for instance ComputerUtil_Attack2 decides which creatures the computer will attack with.

The AI is roughly divided up into 2 parts. Code that is built into each specific card and code that attacks, blocks, plays a land, and pays for cards. Some AI is built into each card. For instance, the computer will target creatures with a defense of 2 or less when choosing targets for Shock. The AI code in Shock chooses the targets and decides when the computer should play it. All of the cards and their AI is crammed in CardFactory, which is roughly 10,000 lines long. Yes it is really, really long.

The AI is sub-optimal sometimes when playing cards like Wrath of God and Giant Growth. When playing Giant Growth, the computer will target a creature that will attack but won’t target a 2/2 if you have a 2/3 blocker. The computer will also not use regeneration effects or counter spells unlike the old Magic game Duels of the Planeswalkers, also known as Shandalar. I find it impressive that Shandalar did do some things right and occasionally even pulls off some pretty good 2-card combos. I am very pleased how MTG Forge’s AI worked out. It is smart enough to provide a good game which was my goal. I have played MTG Forge more than probably anyone else, I’m guessing 100+ hours, but I still love it. Maybe next version I’ll add something that shows the “total time played” so you could see how much you really love Magic and of course for bragging rights. :)

p.s. Sid Meier is credited as both a game designer and programmer in Shandalar.

Monday, August 27, 2007

Programming Magic 2

Magic is a big game. Chess and checkers is a breeze to program compared to the juggernaut of Magic. Thankfully our brains often divide big tasks into smaller ones and that is how I programmed MTG Forge.

The user interface is one separate part and can be independently changed if needed. Phases are important, so a different part of the code (an object) keeps track of the phases and mostly just advances the phase to the next one. Clicking on a card in your hand usually means that you want to play a spell. The code then lets you tap your land for mana and is called Input_PayManaCost. If you click on a card in play, the card is checked to see if it has any abilities and if it has more than one ability the user is given a choice. If the ability has a mana cost, Input_PayManaCost is used again.

Combat is pretty complicated so different code handles attacking and blocking. Input_Attack lets you declare your attackers and checks for a number of things like is the creature untapped, does not have summoning sickness, not already attacking (it checks for duplicates), and does not have the keyword defender. Input_Block handles blocking and just checks to see if the blocker is untapped and able to block. Input_Block also checks to make sure a flying attacker can only be blocked by another flyer, or a creature like Giant Spider with reach. A few creatures like Daggerclaw Imp cannot block.

Combat is still not done because if there are multiple blockers you get to assign damage. Although this situation is rare it helps to solve the problem that “the biggest creature always wins” like in Yu-Gi-Oh. Once you get to the Combat Damage phase, if your creature is going to receive any damage, it will show how much damage after the “Assigned Damage” label in the upper right of the screen. Bouncing your creature during the Combat Damage step will still allow your creature to deal damage but not to receive any. Giant Growth effects should be played during the “After Declare Blockers” phase.

So MTG Forge is made up of these large pieces paying the mana cost, attacking, blocking, and the user interface. Currently MTG Forge cannot handle any X spell like Fireball or Blaze because the “pay mana cost” part of the program cannot handle it, also the card would have to know what mana was used to pay for it. In order to add First Strike only the combat portion of MTG Forge would have to be changed. Dividing up the program into large modules helps

Thursday, August 23, 2007

Constructed Blues

For some reason I don’t play MTG Forge’s constructed mode (all the cards) very often and in fact it is my least favorite mode. When you have all the cards, constructed mode feels fake for some reason. On the other hand sealed and draft games are super fun. Currently my favorite format is sealed since it takes less time and I enjoy playing different color combinations to see what colors are the strongest.

In real life when I played constructed on Magic Online it felt different because I never had all the cards that I really wanted. I never had some chase rare and only a handful of useful uncommons to choose from. For me it was trying to make the best constructed deck with my puny card pool, which is currently how I view limited games. It was exciting to win against a better more expensive deck with lowly commons and uncommons.

Pro Magic players have said time and time again that drafting is infinitely more fun that constructed. Basically constructed is solvable. By solvable I mean that there will be one or more tier 1 decks and the rest are rubbish. Ideally the tier 1 constructed decks would work like rock-paper-scissors. Deck A beats B, B beats C, and C beats A, so it all goes in a circular fashion. If there is only a single tier 1 deck then the format becomes stale. Personally Dragonstorm seemed like the best deck until 10th rolled around, stripping it of Seething Song. Back when Mirrodin was around, Arcbound Ravager decks ruled the roost and were eventually banned. But the damage was already done, sales and tournament attendance was down.

So back to limited games, they have more variety and that is why I love them. (I know there are a variety of alternative formats like peasant, all commons and uncommons, and singleton, where you only have 1 copy of each card.) What formats do you play?

Monday, August 20, 2007

Programming Magic 1

Programming some parts of Magic: The Gathering is simple while others are quite hard. First question, how many zones does Magic have? From the comprehensive rules 217.

“There are normally six zones: library, hand, graveyard, in play, stack, and removed from the game. Some older cards also use the ante and phased-out zones. Each player has his or her own library, hand, and graveyard. The other zones are shared by all players.”

Even though the answer is six, each player has his own library, hand, and graveyard, so the more correct answer is that Magic has a total of nine zones. MTG Forge uses a CardList, an ArrayList that only holds Card objects, for each zone. MTG Forge actually has more than 9 zones because it divides up zones into the stack, human hand, human graveyard, human removed from game, and corresponding computer hand, computer graveyard, computer removed from game. The zones are relatively straight forward to program because the only actions you can do is add a card, remove a card, and find a card.

Some complications occur when finding a card because multiple cards with the same name may be in play at the same time. How do you differentiate between different cards with the same name? The answer was in the old MicroProse game called Duels of the Planeswalkers, also known as just Shandalar, http://www.the-underdogs.info/game.php?id=1550 It had an option that allowed you to turn on the card’s number. So the answer is to give every card a unique number and to find cards based on that number, which is the method I use in MTG Forge. Magic Online most certainly uses the same method but it is transparent to the user since it is more user friendly and identifies the target with fancy arrows.

I know assigning cards different unique number isn’t exciting, but it is a critical part of programming Magic. Most programming is very, very boring, and it takes a great deal of time to get anything working. So I’m very happy with MTG Forge :)

Thursday, August 16, 2007

Wild Draft

Do you remember that old TV show that was named the “Wild Wild Wild West,” well I wanted to name this column “Wild Wild Wild Draft.” Thankfully the sane part of my brain booted up and I decided against it. What sort of draft makes you choose between a Mox and Meloku the Clouded Mirror? The choices are antagonizing. One of my draft decks featured 2 Moxes, 3 Serendib Efreets, Ancestral Recall, and Keiga, the Tide Star, which makes it the most expensive limited deck ever.

No I wasn’t drafting Alpha, but I was drafting some of the best cards from Alpha and from all of Magic’s history. I wrote this little program called MTG Forge that lets you play and draft against the computer. Someone suggested drafting all of the cards programmed with no rarity, so Juzam Djinn comes up as often as a common land. I thought it was an insane idea, but I tried it. This sort of crazy drafting is the most fun ever. Picking between Flametongue Kavu and Rorix Bladewing is just grueling. What is better, Wonder or Regrowth? Does Godless Shrine beat Terror or vice versa?

The version of MTG Forge that I'm talking about is named "“08-06-mtgforge-crazy-limited” As always you can download MTG Forge from http://sourceforge.net/projects/mtgforge

OK, so I open my first pack and what do I see. The standout cards are Birds of Paradise, Helldozer, Jokulhaups, Keiga, the Tide Star, and Kiki-Jiki, Mirror Breaker. Man, I wish I could open a booster like this in real life. I read once that a game, video or otherwise, is made up of interesting choices, unlike life. Drafting like this gives you a ton of interesting choices. Once you draft a Mox, you will never go back.

Monday, August 13, 2007

Remembering Card Names

I love reading and comparing Magic articles. Does this writer agree with that one? Do they have the same opinion on anything? But I do detest having to look up about half or more of the cards to see what they really do. Yes we all know Shock deals 2 damage, but what does Sudden Impact do? I remember most cards by their art and I presume most other people do the same. I do tend to remember the card better if I have actually played with it.

To look up cards I could just click on the card, but I’m not online all the time, so I just use Apprentice. It may not be fancy, but it gets the job done. A few online stores show you the card text when your mouse is over that card, which I think is great. You can quickly lookup the cards you don’t know without all the cutting-and-pasting that I do.

I could do a whole side project that takes a web page and adds all the info when your mouse hovers over a card. I probably won’t, but it still would be a fun project that hopefully would be used by other sites also. I personally don’t know much about Javascript but it is supposed to be easy right? Just my two cents. :)

How many card names do you know?

Friday, August 10, 2007

MTG Forge – Crazy Draft Version

I really enjoy playing limited games on MTG Forge. Sealed is a nice challenge and is fun because I win and drafting is a harder challenge, but still very winnable. And best of all no money is exchanged for little bits of colored cardboard, you know what I mean right? Ok down to my point, I released a new version of MTG Forge called “08-06-mtgforge-crazy-limited” that lets you play draft and sealed with all of the cards with no rarity. You will see a Mox Jet as often as Llanowar Elves. As always you can download MTG Forge from http://sourceforge.net/projects/mtgforge

This type of flat rarity makes draft/sealed games more explosive than monkeys fishing with dynamite. Do you draft a duel land or a big, game-winning creature? The decisions are hard, but a lot of fun. No real archetypes exist since you are seeing cards from a pool of 400. I wanted to call this release of MTG Forge “Johnny Edition,” but I doubted anyone would know what I was talking about. MagicTheGathering.com column “House of Cards” writer, whatever his name is, writes articles that related to “Johnnys.” Supposedly Johnnys like surprises and crazy combos, but I’m not sure.

Also a word of warning, it is harder to win. Instead of winning 95% of the time, you will drop to about 70% because sometimes the computer will drop some bombs and there is no way to recover. So yes you will probably lose a fourth of all your games, get ready to be served.

I did do a few bug fixes. Akroma, Angel of Fury now has flying. I thought Akroma, Angel of Wrath was hard to cast at 7 (5WW), but she really costs 8 (5WWW). Animate Dead and Beacon of Unrest didn’t set the controller of the card correctly, so things like Soul Warden would give your opponent life instead of you. And the computer could target artifact creatures with Ashes to Ashes, which is a no-no. Ashes is an insane card, it basically destroys TWO creatures for only 5 life. I think it is even more unfair than Wrath of God. I removed Ashes from the sealed and drafting card set.

Once I drafted two Serra Angels, two Moxes, and two Glorious Anthems. It was a thing of beauty. What was your craziest draft?

Tuesday, August 7, 2007

Bug Hunting: Akroma

Sometimes I wish I was a treasure hunter that searched for Spanish gold hidden deep in the watery deeps but instead I have to search through text (Java programming code). Hi all, this column will be about how I found and fixed a bug in MTG Forge. For some reason Akroma, Angel of Fury can be blocked by non-flyers which truly puzzles me. The original, Akroma, Angel of Wrath, and other flyers have no problems and work correctly. But why is that red Akroma troublesome, who knows? Even as I am writing this I don’t know the answer and now I will go and hunt for bugs.

The first step in any bug hunt is to recreate the board position so that the error is reproducible. If the bug isn’t reproducible, how do you know if it is really fixed? Ok, the board position is setup in Run.java and it proves the computer does indeed block the red Akroma with Enormous Baloth but doesn’t block the regular white Akroma.

Second step, try to check the most brain dead, easiest thing first (i.e. The computer is not working because it is not plugged in). So I checked the card definition of Akroma in the file “cards.txt” I’m looking for any obvious mistakes like misspelling the keyword flying, not capitalize flying, etc… (Keywords like flying must be capitalized in MTG Forge.) WOW, the definition of Akroma looks like below.

Akroma, Angel of Fury
5 R R R
Legendary Creature Angel
no text
6/6
Flying, Trample

As you may or may not notice, keywords should go on separate lines, so it should look like this.

Akroma, Angel of Fury
5 R R R
Legendary Creature Angel
no text
6/6
Flying
Trample

Last step (hopefully), check to see if the computer does not block Akroma. YEAH!!! It is working like it should. It took 6 minutes total, which is a short amount of time to fix one small bug, but most bugs take at least 30 minutes. Hopefully this gives you an indication of the number of hours that I have put into this project.

I thought the problem might be in the file CombatUtil canBlock method, which says whether card A can block card B. This code has gotten a little messy considering keywords like fear, flying, abilities like “this creature cannot block” (Daggerclaw Imp) and unblockable (Phantom Warrior). As well as the cards like Cloud Sprite which can only block other creatures with flying and Skirk Shaman that has some sort of red fear and can only be blocked by artifact or red creatures. Thankfully I didn’t have to change any Java code and the listing for the canBlock method is shown below for interest value.

Well thanks for coming along with me on my little safari. Initially I didn’t know how long it would take. I’ll probably have more adventures in bug hunting. And remember a bug that isn’t fixed is a feature. Tromp the Domains incorrectly counts the number of lands and not land types. Remember that Tromp the Domains is a feature, ok :)

What “features” have you found in MTG Forge?


//taken from CombatUtil
//can “blocker” block attacker?
public static boolean canBlock(Card attacker, Card blocker)
{
if(blocker.getName().equals("Cloud Sprite") && !attacker.getKeyword().contains("Flying"))
return false;

if(attacker.getKeyword().contains("Unblockable"))
return false;

if(blocker.getKeyword().contains("This creature cannot block"))
return false;

if(attacker.getKeyword().contains("Flying"))
{
return blocker.getKeyword().contains("Flying")
blocker.getKeyword().contains("This creature can block as though it had flying.");
}

if(attacker.getKeyword().contains("Fear"))
{
return blocker.getType().contains("Artifact")
CardUtil.getColors(blocker).contains(Constant.Color.Black)
CardUtil.getColors(blocker).contains(Constant.Color.Colorless);
}

if(attacker.getName().equals("Skirk Shaman"))
{
return blocker.getType().contains("Artifact")
CardUtil.getColors(blocker).contains(Constant.Color.Red);
}

return true;
}

Thursday, August 2, 2007

Who Uses MTG Forge?

This question pops occasionally pops into my head and I’m not entirely sure what the answer is. So who does use my program? Players who are cheap? (That would be me!!) Players who don’t use Magic Online (Me again, lol.) Players who use land destruction and other mean decks. (Sometimes I do.) Players who are afraid of losing to another human player since their ego is so small. (Definitely me.)

Primarily, I would guess that the people who download and play MTG Forge are the casual crowd who just want to have an interesting, fun game of Magic. Yes winning is important, but having fun is more important. I enjoy winning and losing in an awesome, blow-up-the-world fashion. I love the games where the computer surprises me or I win with 1 life left. My goal is not to just win or lose, it is to get there in the most spectacular, remarkable way.

I also play MTG Forge to use great rares like Akroma, Angel of Wrath and Kokusho, the Evening Star, cards that I could/would never buy in real life. Playing with Korlash, Heir to Blackblade also lets me get a feel for cards that people are currently talking about. I also enjoy the crazy sealed deck and drafting games. Which card is better to draft, Mox Jet or Kokusho? Lol I don’t know, they are both awesome cards. I play Magic, and MTG Forge, in order to experience something. Why do you play Magic?