
Cards like Mana Flare and other cards that change the “core rules” of Magic are some of the hardest cards to program. I’m going to briefly talk about how to program these cards.
Let’s say that there is a method called “getMana(Card c) : String” that takes a card as an argument and returns a string representing the mana which that card generates. If getMana() was passed a Forest, “G” would be returned. (For this example we are presuming that a card can only generate one type of mana like basic lands not Birds of Paradise) The method getMana() is relatively simple so far.
One of the ways to program Mana Flare is to have getMana() check to see is Mana Flare on the board. While this approach isn’t elegant, it gets the job done.
String getMana(Card c)
{
String s = c.getMana()
if Mana Flare is in play
s = s + s
return s
}
This approach is cumbersome if there are many cards that modify getMana(), because getMana() could become very complicated. Even though I use this approach in MTG Forge I dislike it because it means that the code for Mana Flare isn’t in only one place, it would be best if all the code relating to a card is together.
The second way I know of implementing Mana Flare is by “stacking methods” or adding “modifiers”. Let me show a brief example.
abstract class ChangeGetMana
{
getMana(String mana) : String
}
class Mana
{
addModifier(ChangeGetMana c)
getMana(Card c) : String
}
I’ve created a “blank” class called ChangeGetMana which has one method, this class can be “added” to Mana and will be called in the getMana() method.
String getMana(Card c)
{
String s = c.getMana()
loop through all ChangeGetMana that were added
s = changeGetMana(s);
return s
}
In essence you are adding code that will be executed in the getMana() method. This approach is good because it automatically handles multiple Mana Flares in play.
Some languages like Python let you actually change another object’s method, so object A can change object’s B method. Python also lets you “stack” methods easier than languages like Java. Python is more flexible than languages based on C, like Java, because all types are checked at runtime versus compile time.
I’m sure there are better ways to implement Mana Flare but these are the only two that I know of. Feel free to share your ideas.