For those who are unfamiliar with Magic (as it's often called informally), casting costs are values that represent the "mana" needed to cast a "spell". Mana comes from different sources, and has certain colors assigned to each type of mana. For example, a hypothetical "blue" spell may require 2 blue mana, and two mana of any color, whereas another spell may require 1 green mana, 2 red mana, and 3 mana of any color. There is a finite number of mana colors (red, blue, green, black, white, and colorless (which is agnostic of mana color, in other words, colorless casting costs can be paid for by any color of mana). So, supposing we had a spell that costs 2 green, 1 blue, and 3 colorless, it could only be paid for with 2 green, 1 blue, and 3 of any combination of colors. What would be some effective ways to represent this in a program? I'm currently writing this in Java, but generic advice would also be nice. If you have any questions, comment, and I will clarify my question if need be.
The natural way to represent the mana cost of an MtG spell is as a mapping from mana types to (non-negative) integers; in Java terms, a Map<ManaType, int>, where ManaType would most reasonably be an enum.
Of course, there are other ways you could represent mana costs, too, e.g. as vectors of integers. However, the nice thing about the map representation, besides its naturalness, is that it's easily extensible, if and when you find yourself needing to represent, say, Phyrexian mana or snow mana or that silly pink mana from Unhinged.
In many other languages, like JavaScript, such a map could also be conveniently initialized using a literal syntax like:
manaCost: { colorless: 1, green: 2 }
Alas, Java still has no collection literals, but at least you can use double brace initialization to get something vaguely similar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With