Monday, May 30, 2011

The Magic Stack


I've read countless times that the most confusing part of Magic for beginners to understand is the stack.  When I personally think of a "stack", I picture the little stack of plates at buffet restaurants.  The top is the only place that you can add or remove plates.  Stacks are a common computer programming concept.

When programming Forge I had to decide what type of object would go on the stack?  What information should the stack item store?  I had already created a class called SpellAbility, which represents each spell or ability using the command pattern, which is an abstract execute() method. 

In Forge I put the SpellAbility object on the stack and getSourceCard() which would return the card object that created the spell or ability.  Generally this works very well with Shocks and Giant Growths stacking correctly.  (If I had to do Forge again, I would cheat a little and put Card objects on the stack.  Card.getStackObject() would return a SpellAbility object.  Moving only Card objects around makes some of the programming much easier.)

Obviously as cards become more complicated the stack must become more complicated.  Currently the developer team is redesigning the stack and wrapping SpellAbility with an object called StackItem.  This makes sense because SpellAbility was designed to be added to Card objects and wasn't designed to go on the stack.  By changing Forge's design it will allow more cards to be added.  And hey, everybody loves the smell of fresh cardboard in the morning.

Thanks for reading,
mtgrares

p.s. 
The stack gets very complicated very quickly when you include obscure cases which generate multiple effects which individually go on the stack.  First the active player chooses the order of effects for his cards then the non-active player chooses. 

405.3. If an effect puts two or more objects on the stack at the same time, those controlled by the active player are put on lowest, followed by each other player’s objects in APNAP order (see rule 101.4). If a player controls more than one of these objects, that player chooses their relative order on the stack.

APNAP stands for Active Player, Nonactive Player Order - which means that the active player goes first, then the non-active player.  The active player is "the player whose turn it is" or simply, the player who untapped and drew a card.

1 comment:

Frothman said...

WHY NO BIRTHING POD!?