1

I'm currently trying to implement a world generator, that has (currently) two major options. World size and difficulty.

I implemented an enum like this:

public enum WorldType {
    TEST,EASY,MEDIUM,HARD;

    public int size(){
        return switch (this) {
            case TEST -> 30;
            case EASY -> 200;
            case MEDIUM -> 500;
            case HARD -> 1000;
        };
    }
}

Now, I want to change some things and I need to separate difficulty and size from each other. They also need to be freely combinable. I imagine something like: TEST.TINY, EASY.LARGE and so on.

The first thing I found while researching was this post: Enum within an enum

But this solution doesn't seem to be able to combine the two enums like I imagine.

Is there a way to achieve what I want without bloating up my code? I've seen solutions with interfaces, structs and classes that implement the feature I want and if at all possible I'd like to keep it simple.

Edit: Thanks for everyones feedback. I've since found this solution that works (although not in the way I imagined it above).

public enum WorldType {
TEST,EASY,MEDIUM,HARD;

public enum Size {
    TINY(30),SMALL(200),MEDIUM(500),LARGE(1000);
    private int value;

    Size(int value){
        this.value =value;
    }

    public int get(){
        return value;
    }
}

}

Wewius
  • 87
  • 7
  • 5
    It seems like you just need two enums and a class/record that contains both as fields? – Sweeper Nov 10 '22 at 08:50
  • 2
    Yeah, some sort of `class Combo { Size size; Difficulty difficulty; }` – shmosel Nov 10 '22 at 08:51
  • also just FYI, Enums can have fields/constructor, if it helps you :) – JCompetence Nov 10 '22 at 08:52
  • @JCompetence but why the hell would you want an `enum` instead of a `class`? you would need to enumerate each combination of `Size` and `Difficulty`. – Maurice Perry Nov 10 '22 at 08:56
  • @MauricePerry Not saying he needs to do it this way. I mentioned the fields, so he can use it instead of creating a method to return size. – JCompetence Nov 10 '22 at 08:58
  • @MauricePerry But it's not always a bad idea either. Sometimes you want combination constants, as in [this example](https://stackoverflow.com/questions/74273122/use-enummap-instead-of-ordinal-indexing). – shmosel Nov 10 '22 at 09:00
  • @shmosel yes, in this example, why not. But in this question, the `Size` and `Difficulty` are decoupled. You could have a field in `Size` for the value in pixels or something – Maurice Perry Nov 10 '22 at 09:35

0 Answers0