如何在 Rust 中根据用户输入动态创建枚举

问题描述 投票:0回答:1

我正在制作一款游戏,允许你选择要对抗的玩家数量;我们使用玩家数量来创建一个玩家轮次枚举,然后我们会继续进行。

enum MetaGamePhase{
    ChoosePlayerCount,
    ConfirmLibrary,
    ChooseFirstTurn,
    InitialDraw,
    MulliganChoice,
    ActiveGame,
    EndGame,
    DeclareWinner,
    None
}

// need to set the contents of this based on the players choosing the player count
enum Turn{
    Player1,
    Player2,
    Player3,
}

enum Action {
    ProgressPlayerTurn
}

fn reducer(mut state: GameState, action: Action) -> GameState {
    match action{
        Action::ProgressPlayerTurn => GameState{
            turn: {
                use Turn::*;
// will also need to set this dynamically based on the enum
                match state.turn {
                    Player1 => Player2,
                    Player2 => Player3,
                    Player3 => Player1,
                }
            },
            ..state
        }
    }
}

struct GameState{
    turn: Turn,
    meta_game_phase: MetaGamePhase
}

我该如何在 Rust 中做到这一点?

rust dynamic enums
1个回答
0
投票

答案是你不能这样做,你应该使用不同的语言功能。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.