到目前为止,我的代码是:

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:召唤骷髅"),以及能够制作卡片对象.所以我想知道使用枚举是否会使这变得更简单,以及如何以这种方式使用枚举.另外,如果有人知道输入列表的更好方法(我现在使用的是:新字符串[]{"亡灵"}),也请回答我.

推荐答案

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

为了更方便地编写标记,可以将t参数移动到最终位置,使其成为可变的arity参数String... t,然后可以轻松地传递标记列表.

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访问有多少卡.

Java相关问答推荐

Java应用程序崩溃时试图读取联系人从电话

JsonPath在多个线程中返回错误的值

Listview—在Android Java中正确链接项目时出错

在for—each循环中的AnimationTimer中的if语句'

JVM会优化这个数学运算吗?

SpringBootreact 式Web应用程序的Spring Cloud Configer服务器中的资源控制器损坏

连接Quarkus中的两个异步操作

为什么我的在一个范围内寻找素数的程序不能像S所期望的那样工作

使用GridBagLayout正确渲染

如何在ApachePOI中将图像添加到工作表的页眉?

如何从错误通道回复网关,使其不会挂起

Kotlin Val是否提供了与Java最终版相同的可见性保证?

在Oracle JDBC连接中,连接失效和身份验证失效是什么意思?

对角线填充二维数组

本机方法(JNI)总是编译的吗?

JavaFX,GridPane:在GridPane的列中生成元素将保持所有列的宽度

为什么JavaFX MediaPlayer音频播放在Windows和Mac上运行良好,但在Linux(POPOS/Ubuntu)上却有问题?

如何在Spring Security中设置一个任何人都可以打开的主页?

验证没有';t work on Hibernate Entity';s字段

如何在 WebSphere Application Server 内的托管线程上运行 BatchEE 作业(job)?