有没有更好的方法使用枚举来做到这一点?

Is there a better way to do this using an enum?

到目前为止我的代码:

package com.company;


public class CardObject {

    public static final int LENGTH = 19; // aka amount of cards  KEEP THIS UPDATED!
    public static final int BEDROCK = 2;

    String name;
    String baseType;
    int attack;
    int health;
    String[] tags;
    String description;

    CardObject(int card) {
        transform(card);
    }

    public void transform(int card) { //  init("" new String[]{}); break; copypasta
        switch (card) { // I will need to make eventually that stuff is replaced (WOB = when on board, OF = on faint, SOT = start of turn, EOT = end of turn)
            case -1: break;
            case  0: init("Zombie",         "mob", 2, 6, new String[]{"undead"}, "WOB: can Wield tools and weapons"); break;
            case  1: init("Lava Bucket",  "spell", 0, 0, new String[]{}, "Give an enemy Burning 3 for 4 turns"); break;
            case  2: init("Bedrock",      "block", 1, 3, new String[]{}, "Immune. Unbuffable. WOB: make allies on either side Immune"); break;
            case  3: init("Creeper",        "mob", 6, 1, new String[]{}, "Thorns 6. Recoil."); break;
            case  4: init("Spider",         "mob", 2, 5, new String[]{}, "WOB: can Combine with Skeleton to Transform into Spider Jockey"); break;
            case  5: init("Skeleton",       "mob", 3, 6, new String[]{}, "OF: Gain Arrow"); break;
            case  6: init("Spider Jockey",  "mob", 2, 5, new String[]{}, "Double Attack. OF: Summon Skeleton"); break;
            case  7: init("Baby Zombie",    "mob", 3, 8, new String[]{}, "Fire Resistance."); break;
            case  8: init("The Sun",      "spell", 0, 0, new String[]{}, "Give all enemy undead mobs burning for 6 turns"); break;
            case  9: init("Lava Bucket",  "spell", 0, 0, new String[]{}, "Give an enemy burning 3 for 4 turns"); break;
            case 10: init("Water Bucket",  "item", 0, 6, new String[]{}, "SOT: remove burning from allies"); break;
            case 11: init("Dispenser",    "block", 0, 6, new String[]{}, "WOB: projectiles cost 1 or are free not sure"); break;
            case 12: init("Arrow",        "spell", 0, 0, new String[]{}, "deal 2 damage"); break;
            case 13: init("Air Block",    "block", 0, 1, new String[]{"unobtainable"}, ""); break;
            case 14: init("Air",          "spell", 0, 0, new String[]{}, "Summon Air for the enemy"); break;
            case 15: init("Bow",           "item", 0, 5, new String[]{}, "WOB: Arrows deal 2x damage. OB: gain 2 arrows"); break;
            case 16: init("Crossbow",      "item", 0, 5, new String[]{}, "WOB: Arrows deal 3x damage once per turn. OB: gain 2 arrows"); break;
            case 17: init("Dropper",      "block", 0, 6, new String[]{"powerable"}, "SOT: optionally play a random card in your hand at a discount"); break;
            case 18: init("Bamboo Shoot", "block", 0, 4, new String[]{}, "EOT: Summon Bamboo"); break;
            case 19: init("Bamboo",       "block", 0, 3, new String[]{"unobtainable"}, "EOT: for every bamboo shoot Summon a 0/2 Bamboo"); break;
            case 20:
            case 21:
            case 22:
            case 23:
            case 24:
            case 25:
            case 26:
        }
    }

    private void init(String n, String bt, int a, int h, String[] t, String d) {
        this.name = n;
        this.baseType = bt;
        this.attack = a;
        this.health = h;
        this.tags = t;
        this.description = d;
    }
}

我很确定我以后需要的是能够找到并根据他们的名字创建一个对象(例如“OF:召唤骷髅”之类的东西),并能够制作卡片对象。所以我想知道使用枚举是否会使这变得更简单,以及我将如何以这种方式使用枚举。另外,如果有人知道输入列表的更好方法(我现在使用的是:new String[]{"undead"}),也请回答我。

你确实可以让CardObject成为enum,然后你就可以避开巨人switch。主要的变化是使 init 成为构造函数。

为了写tags更方便,可以将t参数移到最后的位置,这样可以使它成为可变元数参数String... t,这样就可以很方便的传一个list标签数。

enum Card {

    // note the way that the "undead" tag is being passed
    ZOMBIE("Zombie", "mob", 2, 6, "WOB: can Wield tools and weapons", "undead"),
    LAVA_BUCKET("Lava Bucket",  "spell", 0, 0, "Give an enemy Burning 3 for 4 turns"),
    // other cards go here...
    ;

    // add getters for these fields...
    private final String name;
    private final String baseType; // consider using an enum for the baseType too
    private final int attack;
    private final int health;
    private final String[] tags;
    private final String description;

    Card(String n, String bt, int a, int h, String d, String... t) {
        this.name = n;
        this.baseType = bt;
        this.attack = a;
        this.health = h;
        this.tags = t;
        this.description = d;
    }
}

要创建 Card,只需使用其中一个名称,例如 Card.ZOMBIE。如果你有索引号,你可以做Card.values(someIndex)。这将生成与使用旧代码调用 new CardObject(someIndex) 相同的卡片。

不需要 LENGTH 常量,因为您可以通过 Card.values().length.

访问有多少张卡片