How can May represent magic?

For those unfamiliar with Magic (as it is often called informally), casting costs are values ​​that represent the "mana" needed to "spell". Mana comes from different sources and has certain colors assigned to each type of mana. For example, a hypothetical "blue" spell might require 2 blue mana and two mana of any color, while another spell might 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 a mana color agnostic, in other words, colorless casting costs can be paid for with any mana color) So let's say we had a spell that cost 2 green, 1 blue and 3 colorless, it could only be paid for 2 green,1 blue and 3 from any combination of colors. What would be effective ways to represent this in a program? I'm currently writing this in Java, but general guidelines would be nice too. If you have any questions, comments and I will clarify my question if necessary.

+3


source to share


3 answers


I would have a class called ManaCost that every map needs.

enum ManaColor {

     RED, GREEN, BLUE, BLACK, WHITE;

}

class ManaCost {

    private int colorlessCost;
    private HashMap<ManaColor,Integer> colorCosts;

    public int getConvertedManaCost(){
        int convertedManaCost = 0;
        for(Integer colorCost : colorCosts.values()){
            convertedManaCost = convertedManaCost + colorCost;
        }
        convertedManaCost = convertedManaCost + colorlessCost;
        return convertedManaCost;
    }

    public Integer getColorCost(ManaColor color){
        return this.colorCosts.get(color);
    }

}

class Card {
    private ManaCost manaCost;
    ...
}

      



Yes, I play Magic :) ConvertedManaCost = sum of all mana costs.

+3


source


The natural way to represent the mana cost of an MtG spell is by mapping from mana types to (non-negative) whole numbers; in Java terms Map<ManaType, int>

, where enumManaType

would be most reasonable .

Of course, there are other ways you could represent the mana cost, for example. as vectors of integers. The nice thing about representing the map, however, besides being natural, is that it's easily expandable if and when you need to represent, for example, Phyrexian mana or snow mana or that silly pink mana from Unhinged .

In many other languages ​​such as JavaScript, such a map can also be conveniently initialized using a literal, for example:



manaCost: { colorless: 1, green: 2 }

      

Alas, Java still doesn't have collection literals , but at least you can use double parenthesis initialization to get something vaguely similar.

+4


source


The casting cost representation can be a very simple class or structure. It's been a while since I wrote Java, so here's a C # example that should be translated to Java in a simple way (anyone can edit this answer to fix Java):

class SpellCost
{
    public int GreenCost { get; set; }
    public int RedCost { get; set }
    // etc
    public int AnyCost { get; set; }
}

      

The algorithm that subtracts the mana points for the spell will subtract from the player's current mana directly for each color (invalidate the casting if it lowers the mana for any of these colors to a negative value), then use some formula to choose which mana pool AnyCost subtracts (removes casting again if total remaining mana is less than AnyCost).

In Magic, the number of different mana pools is small. If you were dealing with a large and / or variable number of pools, you could instead use the HashMap<ManaType, int>

ManaType key with values ​​representing the cost for that type of mana.

When you cast a spell, you then iterate HashMap<ManaType, int>

, subtracting each mana cost from the player's mana pool, and nullifying the spell if any of the mana costs cannot be paid.

+2


source







All Articles