Wednesday, May 27, 2009

Unusual Needs

As a guy who programs Magic, unofficially of course, I want to do things that are unusual. For example, I want to sort all of the cards in MTG Forge by putting the newest cards first. And by newest I mean putting the "Alara Reborn" cards first and then the "Conflux" cards second and so on. I really enjoy playing with the new cards and even though MTG Forge may only have a few cards from a given set, they are still fun to use.

OK, back to my task of putting all of the cards in MTG Forge in print order. My first idea was to put all of the card names for each set into separate files. While this would have worked, I found a better solution. When I was playing around with Nate's Deckbuilder I found out that it could produce a file with all of the card names sorted by the set that it was in. While this isn't perfect because I have to reorder the file by putting Alpha first and Beta second, it does get me closer to my solution.

Now all I have to do is to read the "master file" and then sort the cards according to their relative location, if card A is after card B, I would swap them. Nate's Deckbuilder isn't really designed for what I'm using it for but it is versatile enough to still be helpful. It didn't have an option for saving only the card names but it lets you tinker with how it saves decks so I eventually came up with a working solution.

Sorting cards isn't a monumental task but it is certainly hard enough. Every computer program is an accrual of man-hours, it is easier to modify a 100 line program than a 1,000 line program. MTG Forge has 21,000 lines of code not including CardFactory and 37,000 lines of code with it. CardFactory creates all of the cards but it has a lot of redundant code.

Monday, May 25, 2009

Other Magic Programs

Today I don’t really have anything to write about so I’m going to talk about a few of the various Magic computer programming projects that I know about. (I have created my own zip file for some of the program downloads in order to let you quickly play these programs but they might have had additional updates.)

--These programs lets you play Magic against the AI.

Wagic – is designed for to be played on a Playstation Portable (PSP) but also works on Windows. It has around 1,600 cards. Download

Magma – has a very graphical user interface (maybe too many). The homepage is in Italian I think. Download

Deckbot – is text based but lets you play AI versus AI to test out the strength of a deck. Download

--These programs lets you play Magic against another person on the Internet but they enforce the rules like Magic Online.

Firemox – uses XML for the cards and has been an ongoing project for many years.

Incantus – has a nice user interface and probably has the most cards of any project, 4000 cards and counting

--There are other programs that don’t enforce the rules but let you play against other people. So these programs only function like a virtual tabletop.

Magic Workstation – probably the most well-known.

Apprentice – an oldie but goodie that not too many people seem to use.

OCTGN – this is a newer program that also has a blog

--Other Magic related programs are:

Magic Set Editor – which lets you create your own professional looking cards.

Nate’s Deckbuilder and Rulesviewer – lets you easily create decks for constructed, vintage, or of any other type. It also generates nice text spoilers. Download It uses 7z compression which you can get here.

Shandalar updates – Yes, that old Microprose program is still being updated. Download Part 1 (1.5 MB), Part 2 (79 MB)

Many of these programs are hosted in Slightly Magic's forums.

This article has the most links of anything I’ve ever written, lol. And in case you didn’t see the memo, you can play MTG Forge online here.

Friday, May 22, 2009

High Quality Card Scans

Magic has thousands of cards, about 10,500 according to Gatherer, and that number doesn’t include promo cards, alternate art, or tokens. There is a forum that is dedicated to scanning each Magic card ever printed, which is an enormous undertaking.

Each card is scanned in very high quality that lets you really appreciate the artwork (for all of your Vorthos out there). You can download scans of each set and submit scans that they don’t have. Collectively they call themselves the CCGHQ Team (formerly the MWSHQ Team). This is also the same forum that kindly hosts MTG Forge’s forum. The site Slightly Magic also host forums for other Magic projects like Wagic which lets you play Magic on your PSP or on Windows against the computer.

MTG Forge Forum

Thursday, May 21, 2009

Balanced Booster Pack

In the quest mode you win new cards but balancing the cards between the 5 colors, artifacts, artifact creatures, and lands is very tricky. My idea, which I’ve coded, consists of creating a smaller, balanced "bag" and then choosing randomly from it. The idea is to reduce randomness while still being random.

The colors are usually evenly distributed while allowing some unpredictability. Artifacts, lands and multicolored cards appear less often than any color. If you total the artifacts, lands and multicolored cards you will roughly get a number that is usually close to the number of white cards (or any other one color).

This got me thinking about physical booster packs, how are they created? The cards don’t seem to be randomly mixed together because you don’t seem to get duplicate cards (something that did happen when MTG Forge created purely random packs). The creation of real booster packs seems to be related with how the cards are laid out in sheets. Which also leads to the question of what algorithm does Magic Online use to generate packs?

Someone has finally done it and converted MTG Forge into an applet. The link is The "play the game" option worked for me while the "play with images preloaded" didn’t. You can only play with preconstructed decks, the Deck Editor and Quest mode aren't implemented. Anyways, have fun :)

Monday, May 18, 2009

Version 2 Goals

MTG Forge version 2 is just gigantic so I have been trying to figure out what is the most important feature that version 2 will have that version 1 doesn’t? The two main contenders are more cards and a better AI. Hopefully version 2 will have both more cards and a better AI but sometimes you have to make difficult choices.

Having more cards is a fairly simple. Cards could be programmed using text instead of Java. The targeting and resolve code could be separated into separate blocks of code and used over and over again. (Currently much of the card code is copy and pasted.)

Implementing a better AI could be at odds with having more cards. Improving the AI might hinder other parts of the program since the AI influences everything. Having a better AI might mean having fewer cards. (And when I say “fewer cards” I mean having the current number of cards which is about 1,600 instead of 5,000.)

The AI is really interesting so I’m going to work on it first. I’m going to get a few cards working and see how much better I can make the AI. I know the AI could really improve the basic creature combat but I’m not sure how well the AI will handle combat in a full-blown game of Magic with activated abilities and instants.

I’m hoping that the AI will be very competitive and will help you improve as a Magic player. (Let’s also hope that I can find a good Java implementation of the alpha-beta algorithm.)

I tried using Google for about 15 minutes to find a good Java alpha-beta algorithm but I didn't have any luck. If you have any ideas please let me know, thanks.

I think I found a good implementation by downloading the source code from a Java algorithm book. I would like to find an optimized version though.

Friday, May 15, 2009

High Quality Scans - Alara Reborn

Magic has wonderful artwork and you can download large, high quality scans of Alara Reborn here (25 MB). The card picture to the left is actually reduced and you can see the card at full size here.

Wednesday, May 13, 2009

High Level Programming

When you program, you should always try to program at the highest level. "This level provides the abstractive power to work with a problem on its own terms. Your code at this level should be somewhat readable by someone who's not a computer-science whiz. At this level your code depends more on the tools you've built than on the capabilities of the language you're using." [1]

Basically your goal is layer computer-science data structures until you can use language similar to the problem domain. If your problem domain is Magic: The Gathering hopefully you have methods like startGame() and drawCard(player). These methods are "high level" because they can be understood by a Magic player who isn't a computer programmer.

Thankfully MTG Forge has a number of high level methods and objects. In the beginning I didn't have drawCard() and I would just duplicate the code. Needless to say this isn't the best solution, so I created drawCard() out of convenience.

Not all methods and object can be defined by the Magic glossary. Paying the mana cost involves a number of "artificial steps" that the computer has to do which are trivial in a real world game such as checking to see if the mana is necessary as well as untapping lands when the user cancels and doesn't pay for the spell. Combat also involves many artificial steps that the computer needs in order to make combat work correctly such as assigning damage and triggering combat abilities like Hypnotic Specter.

In closing, write high level code because it will save you time and frustration in the long run.

[1] - Code Complete by Steve McConnell. This is a great programming book that is one of my major influences. This book is better than all of the books that I had to read for my computer degree, bachelor's in information science. I suggest that you try to find it at your local library.

The book has both a first and second edition. I still have the first edition but the second edition has probably 10% new and updated material. If you are cheap buy a used copy of the first edition.

Monday, May 11, 2009

New Version

MTG Forge now has 1606 cards, which is about 100 more cards than the previous version. All the credit goes to Dennis and Chris from the forums. It is nice to sit back and let someone else do all of the hard work, lol. MTG Forge now has a ton of new creatures that have protection.

MTG Forge now also supports deathtouch and changeling. Deathtouch makes attacking and blocking more complicated while changeling helps Shapeshifters fit into any tribal deck. I had Avian Changeling in my quest deck and I kept fetching it with abilities that got Mercenaries. MTG Forge also has the brand new card Jenara, Asura of War which is a killer. (I should know because the computer has beaten me with her.)

Questing is improved. (MTG Forge has a quest mode where you start out with a few cards and then you win more cards.) The AI quest decks are divided up into easy, medium, and hard. When you play on the easy mode, you face 3 easy decks, 3 medium decks, and 4 hard decks. In the medium, hard, and very hard modes you win new cards after you win or lose 2 matches, this should make these modes a little bit easier.

You may also want to download the token art which is the real artwork from Wizards. And if you are having trouble downloading the card pictures, I have a link below. And for those who want to know, this is just the 05/03 version that was posted on the forums. (Yes the forums get the new version before anyone else.)


MTG Forge 05-06 (2 MB)

Token Art (1.5 MB)

Readme (which is included in the main download)

Card Pictures (41 MB)

List of changes:

- Merged rares' quest changes code (if I understand correctly, AI opponents encountered are divided between easy, medium and hard), and included Chris' latest quest files.
- Fixed lands with tap abilities being able to use mana to pay for those abilities (Mikokoro, Center of the Sea, Kher Keep, Pendelhaven, etc.).
- Added different images for basic lands (change them to your favorites in card-pictures.txt).
- Fixed some problem with fetching Rebels/Mercenaries if cancel is pressed.
- Fixed Hush Cycling.
- Fixed Cover of Darkness.
- Fixed Guardian of Cloverdell AI lifegain?
- Lightning Helix cast by the AI should not cause crashes anymore.
- Fixed Stalker Hag.
- Added "Changeling" keyword (only works on creatures for now though I think?), and fixed existing cards that were supposed to have Changeling, thank you Chris for pointing them out.
- Added "Deathtouch" keyword (only tested this for a bit).
- Added "Protection from creatures" keyword (haven't tested this a lot either).
- Fixed Demonic Tutor (can only cast it during your own turn now).
- Fixed display of -1/-1 counters (it would show a negative number).
- Fixed Lockjaw Snapper.
- Fixed some issues with the AI targeting side of Oblivion Ring.
- AI Guiltfeeder should attack now.
- Fixed crash with Aether Vial for AI... however, turns out the thing is still broken, for now, don't put this card in any AI decks.
- Fixed Zur the Enchanter, will only be able to get enchantments with manacost 3 or less.
- Vampiric creatures should not receive +1/+1 counters as they're hitting the graveyard anymore.
- Fixed a crash with Faceless Butcher when its target is null, and the AI shouldn't play it when there's no targetable creatures in play...

New cards:

A lot (60+ if I'm not mistaken) more Protection from {color} cards (thanks Chris!)
Terminate, Unmake
Maelstrom Pulse
Jenara, Asura of War
Fable of Wolf and Owl
Sighted-Caste Sorcerer
Vithian Renegades
Steely Resolve
Cloudchaser Eagle, Aven Cloudchaser, Monk Realist
Commander Eesha, Beloved Chaplain
Wirewood Lodge
Bunch of deathtouch cards
Bunch of changeling cards

Wednesday, May 6, 2009

Name Mutator

The MTG Forge zip file contains the curiously named file "name-mutator.txt". MTG Forge can use this file to rename all of the cards. I added this functionality after a cease-and-desist letter was send to, my previous hosting website. I figured if my program used different card names it would still be legal, I have no idea if this is really true.

Renaming all of the cards has certain drawbacks which makes it unappealing. First, the user has to read the card text for every card. Magic has been around for a long time and people enjoy using their favorite card. If their favorite card is renamed, the user won't know if MTG Forge has it. Second, if MTG Forge showed the originally named card picture it would still be confusing.

I do have to say that creating new card names for the 441 cards in name-mutator.txt was quite challenging. Let me share with you some of the names that I am most proud of. Zombify became Rise Again and Wrath of God turned into Peaceful Solution. I worked the hardest on the lands because I wanted them to make sense.

Mountain : Rock
Forest : Grass
Swamp : Bog
Plains : White Sand
Island : Underwater

Since Mountains make R mana, Rock is a decent substitute. I tried to make each renamed land match the mana color that is makes. Thinking of a synonym for Plains was very hard, so that explains White Sand.

Basically the name mutator file could be used but it makes the game less interesting.

Monday, May 4, 2009

Stuck in the Middle

Have you ever started a computer programming project with high hopes only to become lost and disillusioned? Usually this happens after you've written a few thousand lines of code and have put a fair amount of effort into the project. Somewhere you realize that things aren't working how they should and the code becomes all tangled in your mind. Currently I'm "stuck in the middle" while working on MTG Forge 2.0.

This is my third attempt at MTG Forge 2.0 and I have a fair amount of things working, like the user interface, but the devil is always in the details. All of the millions of details have to be worked out and somewhere in the middle things tend to get worse before they get better. (You have to observe which parts need to be fixed, which is why things seem to get temporarily worse.)

The key to programming (and life) is to only worry about the major details and handle the small details one at a time as needed. When I program I don't plan much. I like the idea of UML and other diagrams, but to me the code is what is really important. Each object solves one big problem while each method solves a smaller sub-problem.

Sometimes the problem with "2.0 software" is that it tries to add too many features. Initially this was my problem but even when you cut features, the overall problem is still hard. Magic is a tough, but interesting, nut to crack and this explains why many Magic projects are started but never finished. MTG Forge is usable because so many features were cut, like phase stops. I knew I couldn't do everything right the first time, so I didn't try. Hopefully MTG Forge 2.0 will solve many of the deficiencies of 1.0 but it certainly won't be perfect. (Perfect software doesn't exist.)

I've only had a sinking feeling after I've written a 5,000 or so lines of code. I feel sorry for programmers who spend any portion of their life on code that is never used or thrown away. (I would love to get my hands on the Magic Online 2.0 code which isn't being used any more. It would be interesting just to see the documentation.)

Friday, May 1, 2009

What is MTG Forge?

MTG Forge lets you play the card game Magic: The Gathering against a computer opponent using all of the rules. This is very different than playing solitare Magic with Apprentice or Magic Workstation because the AI will attack and play spells. Currently there are over 1,600 cards ranging from old power cards like Ancestral Recall and Lightning Bolt to newer cards like planeswalkers. The user interface is basic, but it does let you download all of the cards pictures and view them while playing the game.

You can also play sealed deck, draft, or the new quest mode. In draft mode the AI drafts for the 7 other opponents and builds the decks from the cards that it chooses. In the quest mode you start out with a few cards and the more games that you play the more that you win. As you get more cards you can update and change your deck.

Download - MTG Forge uses Java and works on Windows, Mac, Linux, etc...

Tutorial (85 MB) - If you don't know how to play Magic: The Gathering download this windows demo.

MTG Forge requires Java, if your computer doesn't have Java you can get it from here,