Wednesday, August 27, 2008

Stack – Cards versus Spells

Sometimes thinking about a Magic card really confuses me. Generally a card is a spell right? And if that card is a creature, that spell generates a creature. The confusing part is that the card represents the spell for the creature and the creature itself. This isn’t confusing if you have physical card, but it does confuse me, a Magic programmer. And cards don’t go on the stack, only spells do.

The stack is the only zone that doesn’t hold cards. Technically the stack only holds spells and abilities. MTG Forge version 1 tries to stay true to the rules, no Card objects can be added to the stack, only SpellAbilities. (All spells and abilities are represented by a SpellAbility object. This object implements the functionality of the card. Using Wrath of God as an example, its SpellAbility would destroy all creatures.)

Since Magic is all about moving cards, it is difficult when you play a card to move it from your hand to the stack, since the stack doesn’t hold cards. I decided that I would let the stack hold cards and each Card object would hold a SpellAbility object. All I am really doing is swapping Card and SpellAbility. Currently only SpellAbility objects are added to the stack but and they hold a reference to the card that they represent.

Allowing the stack to hold cards makes moving cards much, much easier. The stack is a zone but you had to treat it as a special exception since it didn’t hold cards. Now I can write a simple moveCard() function that will work all the time.

moveCard(Card card, String zoneTo, String zoneFrom)

Incantus, another Magic program, adheres strictly to the rules and each card has characterstistics. Below is taken from Magic’s comprehensive rules.

201.2. An object’s characteristics are name, mana cost, color, type, subtype, supertype, expansion symbol, rules text, abilities, power, and toughness.


Ian said...

The stack does hold cards. The first thing you do while announcing a spell is to move the card from your hand onto the stack. The stack also holds things that aren't cards, but the cards really are on the stack.

Gando the Wandering Fool said...

Ian thats not quite right. In a real game it may seem that you are actually placing cards on a stack in the order of priority but in fact you are only stacking the spell's abilities. Take for instance a card that has an "or" (modal) ability. Do this or do that. The choice is what is stacked..the card itself waits in limbo for resolution. Once resolved it moves to the appropriate zone (Hand, graveyard, library, play, removed.)

Gando the Wandering Fool said...

MTGForge...I think it is the mechanics that are obvious to the player that need to be strictly enforced. If you can accomplish something behind the scenes by treating something in a way that doesnt seem to be in the rules it doesnt matter at all. Just like drawing a menu with commands doesnt actually have to draw a picture of a menu it just needs to light up the right pixels (and maybe color them) so too your solution of moving cards to a stack makes sense. Though you might come up with some funky bugs as a result with some cards that have buyback etc. Make sure you track the card's eventual zone properly.

Incantus said...

Hey Gando,

Actually, you are incorrect. Cards do move onto the stack. For example, some cards have static abilities that are only relevant when they are on the stack (split second). Since abilities can't have abilities, the card itself must be present in that zone.

Here's the relevant comp rules:

217.6a - When a spell is played, the physical card is put on the stack.
When an ability is played, it goes on top of the stack without any card associated with it (See Rule 409.1, "Playing Spells and Activated Abilities.") [CompRules 2003/07/01]

Ian said...

I guess I should always quote the comp rules when stating something about how Magic works. Thanks, incantus.

Gando the Wandering Fool said...

My bad. I stand corrected. And Ian no need to quote the comp. The fact of the matter is I haven't played tournament leve mtg in nearly 9 years. (Not counting online drafts.)

Forge said...

These thoughts about "what is a card" occur to me when I'm programming because I have a Card class but in the real physical game of Magic a cardboard card represents many different things. Thankfully it is easy to understand but the computer is very literal so I need to understand all aspects of what a card actually is.