#Enum, struct or const?

11 messages · Page 1 of 1 (latest)

wooden abyss
#

Hello! I'm trying to write a rust version of a Python library I've written for brute forcing solutions to a game mode in Teamfight Tactics, a League of Legends game. The Python solution to my question is here.

In the game, champions have 1-3 traits, like Warrior or Scholar, and if you put two Warriors out on the map, the Warriors 2 trait will activate. The problem I'm trying to solve involves figuring out which champions to place on the map in order to activate 7 different traits, preferably with as few champions as possible.

To represent my champions I have in Python written an enum that has values Traits, cost. For instance, for the champion called Ahri:

@dataclass
class ChampionTraits:
    traits: set[Trait]
    cost: int

class Champion(ChampionTraits, Enum):
    Ahri = {Trait.Arcana, Trait.Scholar}, 2
    # (59 more below)

In rust, I tried creating the following struct and enum:

struct Champion {
    traits: Vec<Trait>,
    cost: u8,
}

enum ChampionEnum {
    Ahri = Champion {
        traits: vec![Trait.Arcana, Trait.Scholar],
        cost: 2,
    },
}

but I'm realizing that enums don't work this way. A key part here is that there are (only) 60 well defined champions, and I won't be needing to create custom ones. So, a const AHRI might make sense, but I thought I'd check here first, since I'm still very new to rust.

Does anyone have a suggestion for a good data structure to use?

odd isle
#

if your program has no need to ask "is this champion Ahri or not?" then you probably don't need the enum and can just deal with &Champions

#

I would try this

struct Champion {
    name: &'static str,
    traits: &'static [Trait],
    cost: u8,
}
const CHAMPIONS: &[Champion] = &[
    Champion {
        name: "Ahri",
        traits: &[Trait::Arcana, Trait::Scholar],
        cost: 2,
    },
    ...
];
#

you might need more, but this is the minimal code, and so a good place to start trying to solve the problem

wraith valve
#

If you do need to get champions by name, you could store them in a Hashmap<String, Champion>

wooden abyss
#

Good points, and thank you very much for these! I do need to get them by name, for instance «I have Ahri and Zoe out already, what are the solutions that include those two?»

The enum solution in python was nice because I could autocomplete champions as part of the enum (e.g. type Champions.Ah<tab>.

odd isle
#

in that case you might have an enum and a match, which is how you associate data with enum variants

#
struct Info { ... }
#[derive(Clone, Copy, Debug)]
enum Champion {
    Ahri,
    ...
}
impl Champion {
    fn info(self) -> &'static Info {
        match self {
            Champion::Ahri => &Info { ... },
            ...
        }
    }
}
#

or you can have a bunch of methods that return different properties without the Info struct existing at all, but then you have to write more matches

#

but you can have methods to do self.info().traits etc. for you

wooden abyss
#

Ah, that Info struct was what I was missing! I was originally thinking I would have to do matches within both a cost method and a traits method. But using a Info object to encapsulate these makes tons of sense. Thanks!