Thursday, May 29, 2008

Programming Cards With Events

The hardest Magic cards are the ones they say, “Do X when Z happens.” I was thinking that each card could register an event with the rules engine. Gruul War Plow says, “Creatures you control have trample.” The event code could look something like this.

engine.addEvent(CreatureComesIntoPlay)
{
Add trample to that creature
}

Events could also implement replacement effects. Leyline of the Void says “If a card would be put into an opponent's graveyard, remove it from the game instead.”

engine.addEvent(ReplaceOpponentGraveyard)
{
Remove card from the game
}

Events are limited type of observers. The event code only wants to know if a specific action took place. While this idea is primitive, it seems like a good way to split up the division between the card code and the rules engine.

The event code would be part of the overall code for that card. It is a good idea to keep the code for each card as separate and self-contained as possible in order to avoid hard-to-find errors and long “if” statements. Many details still have to be hammered out like what arguments does each event need?

Wednesday, May 28, 2008

Programming Notes

These are a few notes I have made about MTG Forge version 2. Much like the “behind the scenes” feature of a DVD, these notes are brief and disjointed. To me they outline a large part of version 2 but to anyone else they are just vague ideas.

I probably need a new name too. MTG Forge is ok, but I might go with “Card Forge” or “Magic Forge”. I’m open to suggestions. I personally love the name “Deckbot” but I don’t want to rip-off someone else’s name.

Download Word Notes

Tuesday, May 27, 2008

Minor Update

I accidentally disabled a minor AI feature that effectively lets the AI mulligan. So here is a minor release that probably improves the AI probably 5 percent. And as always, to save your decks make a copy of the file "all-decks2"

Windows Installer
Zip - Java jar file for Windows and all other platforms

Magic Numbers

It is a good practice not to hardcode numbers into your code, like “if x < 24”. In this case, “24” is a magic number and should be replaced with a variable name like “page_length.” While magic numbers are generally considered bad, programmers still use them from time to time.

I am slowly working on MTG Forge version 2.0 and I was trying to make the cards stay in their zone, i.e. making sure you can’t move a card from your hand into play and thus confusing yourself. Version 2.0 is still very alpha and only lets you move a few cards around the screen.

Right now I divide up the screen into three areas, your hand, your in-play area, and the computer’s in-play area. Logically you shouldn’t be able to move a card from your hand to your in-play area.

I am using Python and PyGame. PyGame is a graphical library which will let me draw the cards on the screen much like Shandalar. In PyGame every sprite has a rectangle object that has properties that can be changed. PyGame uses this rectangle to know the sprite’s location, its x y coordinates, and its size. To move a sprite you just changes the rectangle’s x and y, sprite.rect.center = [200,200].

The code below uses magic numbers and keeps the card sprite from moving out of its zone. Only the y coordinate is checked since the zones extend all the way across the screen. It’s been awhile since I’ve used simple x y geometry but thankfully nothing has been too hard.

#rect is short for rectangle
def keepCardInZone(self, card, originalX, originalY):
if originalY < 250:
if card.rect.bottom > 250:
card.rect.bottom = 250

elif originalY < 550:
if card.rect.top < 250:
card.rect.top = 250

if card.rect.bottom > 550:
card.rect.bottom = 550 + 1

else:
if card.rect.top < 550:
card.rect.top = 550

Friday, May 23, 2008

Landwalk

I haven't played MTG Forge in awhile, about a month. I guess it is similar to card designers who work on Magic all day, but may feel a little burnt out if they had to play Magic. I hope landwalk is working out, it could feel unfair at times. I played a few games but I wasn't familiar with all the new cards, so that made it harder.

Harder isn't necessarily bad but I didn't want to think too hard. Since Magic Online doesn't support Shadowmoor yet, I wish I could add more cards from the set but they are too complicated.

Wednesday, May 21, 2008

New Version - Details

Zip Version – Java jar for all operating systems, also includes the Windows executable in case you don’t want to run the installer

-The Java source code is included
-In order to keep your decks, save the file “all-decks2”)
-I forgot to make the Windows installer. I'll do it soon.

Thanks (again) to Rob Cashwalker for the 250 new creatures. They were added by the miracle of “cards.txt.” This file has a simple format but it allows new creatures to be easily defined. The creatures are simple and don’t have any activated abilities. Cards.txt supports about 10 keywords, like flying, vigilance, haste, and mana abilities. (Technically a mana ability is an activated ability, but you understand what I’m saying.) Currently cards.txt doesn’t support instants/sorceries yet. The file just holds the text that the spell displays, but none of the functionality. CardFactory makes all of the cards and adds their effects.

With this influx of creatures, I had to balance a few things out. The “generate deck” option for constructed will have at least 10 non-creature spells while sealed will at least 6. (The land counts for the generated decks are 26 for constructed and 18 for sealed, the lands are evenly divided between the two colors.)

I also balanced out the code that generated the booster packs for sealed and draft. They have a 60/40 percent ratio of creatures and non-creatures. I don’t know if this ratio is similar to real life booster packs. The colors for each booster pack were also smoothed out. Every time a card is chosen for a booster pack, a subset of 5 cards is generated, one for each color. Then one card is chosen from the subset. This ensures that each color has a fair chance.

The colors distribution should be very consistent compared to real life. To read how the booster packs are created, look at the Java object ReadBoosterPack. It uses the files “common.txt”, “uncommon.txt”, and “rare.txt” to generate a pack.

This is an example of cards.txt. The “no text” line is used for spells and creatures that have static abilities like Castle Raptors (as long as Castle Raptors is untapped, it gets +0/+2). The static ability is then implemented by GameActionUtil.executeCardStateEffects(). Birds of Paradise looks weird but each “keyword” goes on a separate line.

Alpha Myr
2
Artifact Creature Myr
no text
2/1

Rorix Bladewing
3 R R R
Legendary Creature Dragon
no text
6/5
Flying
Haste

Mox Emerald
0
Artifact
no text
tap: add G

Birds of Paradise
G
Creature Bird
no text
0/1
Flying
tap: add W
tap: add B
tap: add U
tap: add R
tap: add G

Monday, May 19, 2008

New Version

250 new creatures thanks to Rob Cashwalker and the magic of “cards.txt”. MTG Forge now has a total of 714 cards. Creatures now have landwalk!!! As a side note, creatures may be missing minor abilities like Dandan (sacrifice if you don’t own any islands) or Aether Membrane (bounce all blockers). I’ll do a full write up Wednesday.

Download Zip – works on all Java platforms, Windows, Mac, Linux, etc…
(To save your decks, keep a copy of the file “all-decks2”)

Debugging

If you are not a programmer by profession or interest (and most people aren’t) you probably don’t really know that debugging means. It is similar to correcting the grammar of someone else’s English report. Debugging tends to be dull, boring, frustrating and occasionally rewarding. Even though programmers would rather spend their time writing brand new, sparkling code, they end up spending the majority of it debugging.

Usually you debug a program because you want to fix a bug, “de-bug” it. The story goes that in one of the first computers a moth was found jamming a switch. So the first debugging session removed an actual bug.

In order to fix a bug you must be able to reproduce it. If you can’t see the error, you can’t fix it. This can be very frustrating. Users, often called “losers” by programmers, can’t really describe the problem they had, but they want it fixed. Sometimes a programmer will tell the user the problem is fixed even though it isn’t, because the probability of the same problem occurring is miniscule.

I do debug MTG Forge from time to time. Usually I want to be adding cards from the latest set, but I try to fix any important bugs. I just fixed an error when you use the “resizable game area” option and the computer blocks with multiple creatures. A separate window should popup but instead there was an error message. Thankfully it showed me the exact line that I needed to fix.

I made an assumption that the game area would always be this one specific class, GuiDisplay2, but since I added another class, GuiDisplay3, the assumption was incorrect. Debugging corrects faulty or outdated assumptions as well as errors in logic. Thankfully this error was easy enough to reproduce and fix, and only took about 15 minutes of time. May all errors be this easy ;-)

Wednesday, May 14, 2008

MOL

MOL stands for “Magic Online” and it isn’t doing too well. Version 3 is fresh out of the gates but is lagging way behind. Version 2.5 looked fine to me but I guess it had server issues and maybe couldn’t handle the newer card mechanics. Version 3 seems very stable, but worse at everything else.

The user interface, which is critically important, is getting many complaints. The number one criticism seems to be that the cards are hard to read. While this is a very specific complaint, it might be very hard to fix. And by “very hard” I mean it could take weeks to fix or it may be unfixable. A bad user interface can break an application, that is the reason many videogames look so good but play so badly. The number one complaint I’ve received about MTG Forge has been about the interface.

Designing the “next version” of a piece of software is difficult. For each great new feature that you want to add, it comes with added complexity and complexity is very, very bad. Complicated code takes about twice as long to write. It takes more time to write and more effort to debug. Personally I was amazed that version 2.5 supported multiplayer games since so many cards had to be updated or changed for group play. Version 3 probably should have kept everything the same and just improved the server stability.

Version 3 is the first version that Wizards has developed internally. While most people may think this is a minor point, it is actually very important. Developing your own internal version of Magic Online is like asking a car fix-it place to design a new car engine from scratch.

Version 2.5 was developed externally by the now defunct Leaping Lizards (their logo was very annoying) and Wizards just modified the existing code. Wizards has gone from just making cards and games to developing software. There will be some growing pains as Wizards becomes a better software company. And while version 3 won’t win any awards, Magic Online will continue to improve.

p.s.
There is an old programmer’s joke that goes like this. “You can have software that is easy to use, cheap, or reliable. Pick any 2 out of 3.” Easy to use, cheap code won’t be reliable. Easy to use, reliable code will be very expensive. And cheap, reliable code will be hard to use. MTG Forge falls into this last category :)

p.p.s.
This is probably the funniest article about software development that you are going to read.
Scouse of Cards - The Trouble With Version 3
http://www.starcitygames.com/php/news/article/15781.html

Monday, May 12, 2008

2 Headed Dragon

In most fantasy games there dragons and in a few there are two headed dragons. A two headed dragon isn’t going to go very far unless both heads work together, much like a three legged race at the park. My point is that MTG Forge has two conflicting goals. One, to program as many cards as possible. Two, to have a decent AI opponent.

These two goals conflict with each other. The AI wants simple cards that are always positive. The AI can easily use a card like Giant Growth versus a situational card like Wrath of God. But as more and more cards are programmed, some of them are going to be cards that depend heavily on the particular situation. Current MTG Forge doesn’t evaluate the game situation, it just blindly plays games. So this is better than just goldfish (playing against no opponent) it doesn’t always simulate the back-and-forth action of a real game.

In a way MTG Forge has a third goal, to program the AI for each card using the least amount of code and time. Often programming the AI for a card takes as long as the card itself. Let’s use Shock for example. When should the computer play Shock?

Currently MTG Forge will use Shock if your life is under 7 (an arbitrary number) or if you have a 2/2 flyer or better. MTG Forge will never double Shock a 4/4. Each card evaluation is independent of the other cards in the computer’s hand. Having different types of AI for red-burn or green-stompy would help give the computer more personality and skill. Just having the computer play instants during combat would be an improvement.

Thursday, May 8, 2008

The Problem with AI

Artificial Intelligence (AI) is an infinitely, complicated subject. Much like the hole that Alice in Wonderland fell down, “How deep do you want to go?” AI requires a specific area in which to function. Relating to videogames, an AI that simulates driving a car is different that an AI that plays tennis. In order to program a decent AI you have to understand the problem (trying to drive a car or play tennis) and be able to program that logic into the computer.

There are many problems when trying to adapt AI routines to MTG Forge. One, MTG Forge’s architecture does not support different AI algorithms. I can’t simply apply min-max and get a better/different AI. (The initial problem I had when trying to apply min-max and other “look ahead” algorithms is that I had an impossible task for copying all of the game data. In theory copying everything is simple, but in practice it is very hard.)

Min-max is an algorithm for two player games that allows the computer to maximize his turn while minimizing his opponent’s. Min-max should be able to answer questions like, “Which creatures should the computer attack with?” but programming it can be very complicated.

Two, even if MTG Forge’s architecture supported different pluggable AI’s they would probably be very complicated. Obviously there could be some “helper” functions so that the new AI wouldn’t have to write everything from scratch. Maybe trying to develop an AI interface for just combat would be a good first step. Creature combat is simple enough to understand and yet complicated enough to be tough. Just answering the question, “When should the computer race?” is hard. (Racing is when you attack each other back and forth.)

I love the idea of AI even though I only know a tiny bit about it. I do know about genetic algorithms and a few other AI algorithms like random hill climbing. I’ve tried to apply min-max to combat, but it is pretty complicated. I like the idea of giving the computer simple plans like “Destroy that big creature” or “Attack and then bounce my creature.” I think even these simple plans would give the AI more depth and personality.

p.s.
As a side note, not all videogames have sophisticated AI routines. Mario’s enemies tend to be basically brain-dead.

Monday, May 5, 2008

Misc.

Two things. Feel free to start your own open source project with my Java source code. I suggest SourceForge or Google. And my only suggestion is not to use "Magic: The Gathering" in the description, but instead just use "trading card game". Zip file with the full program and source code, download it here.

Secondly, when the computer blocks with multiple blockers a window should popup but doesn't. I should fix this soon. I think a previous version won't have this problem, download it here. Thanks for the error reports.

Visual Gatherer

Wizard's card, Gatherer, has been very popular, so they had to add those annoying advertisements. Create and search cards visual instead of having to click to see the picture.

Click on the tab "VS Creator" to see a whole set of cards at a time and click on "VS Manager" search by color, type, etc..

http://mtg.keryas.com

Marvel TCG – Play for Free!!!

The Marvel Trading Card Game was sold separately that let you play against the computer but it was meant to teach people about the game so they would buy boosters and play online. The bad news is that the online part of the game doesn’t seem to be catching on. The good news is that they are releasing all the cards, so players can build any deck that they want to. Presumably the online part of the game will go down in a few months, but until it does, you can play against other people for free!!!

Main Webpage for Marvel TCG

Download the Windows Client

PDF Rules

The Marvel TCG has some of the older Marvel cards from the VS TCG.
The total number of cards is 852 and includes the following sets:
Marvel Origins (released April 2004)
Web of Spider-Man (released September 2004)
Marvel Knights (released February 2005)
The Avengers (released August 2005)
28 cards from the Fantastic Four starter set.