我可以在对象的New()构造函数中声明一个委托吗?还是用初始化参数?

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

我试图在C#中用我的数据内联声明一个delegate,但似乎无法编译。我希望有人能帮我(重新)架构一下我的思路或代码。

我的目标是在一个在线游戏中创建不同的玩家类型。 每个玩家类型都有他们所采用的策略。这个策略是通过调用一个内联async函数来实现的,这个函数可能会调用HTTP REST资源作为IQueryable评估的一部分。

问题:什么是可维护的方式?

  • 什么是一种可维护的方式来配置IQueryable中所需要的代码?EvaluateTurn() 每一个人的代表 AIPlayer 类型?

将会增加更多的玩家类型,并? IQueryable 以成为针对CosmosDB、AzureTable、SQL、内存数组等的延迟执行Linq查询。

public class AIPlayer
{
    public string PlayerPersonaName { get; set; }
    public string Description { get; set; }

    public delegate int EvaluateTurn(IQueryable<GameplayRound> playHistory);
}


public List<AIPlayer> CreateDefaultPersonas()
{
    var ret = new List<AIPlayer>();

    ret.Add(new AIPlayer()
    {
        PlayerPersonaName = "CopyCat" ,
        Description = "Hello! I start with Cooperate, and afterwards, I just copy whatever you did in the last round. Meow",
    });

    ret.Add(new AIPlayer() {
        PlayerPersonaName = "Grudger" ,
         Description = "Listen, pardner. I'll start cooperatin', and keep cooperatin', but if y'all ever cheat me, I'LL CHEAT YOU BACK 'TIL THE END OF TARNATION."
    });

    ret.Add(new AIPlayer() {
        PlayerPersonaName = "AlwaysCheat",
        Description = "the strong shall eat the weak"
    });

   ret.Add(new AIPlayer() {
       PlayerPersonaName = "AlwaysCooperate",
       Description = "Let's be best friends! <3"
   });

   ret.Add(new AIPlayer() {
       PlayerPersonaName = "Detective",
       Description = "First: I analyze you. I start: Cooperate, Cheat, Cooperate, Cooperate. If you cheat back, I'll act like Copycat. If you never cheat back, I'll act like Always Cheat, to exploit you. Elementary, my dear Watson."
    });

    ret.Add(new AIPlayer()
    {
        PlayerPersonaName = "CopyKitten",
        Description = "Hello! I'm like Copycat, except I Cheat back only after you Cheat me twice in a row. After all, the first one could be a mistake! Purrrrr",
    });

    ret.Add(new AIPlayer()
    {
        PlayerPersonaName = "Simpleton",
        Description = "hi i try start cooperate. if you cooperate back, i do same thing as last move, even if it mistake. if you cheat back, i do opposite thing as last move, even if it mistake.",
    });

    ret.Add(new AIPlayer()
    {
        PlayerPersonaName = "Random",
        Description = "Monkey robot! Ninja pizza tacos! lol i'm so random (Just plays Cheat or Cooperate randomly with a 50 / 50 chance)",
    });
   return ret;
}
c# delegates
1个回答
1
投票

delegate是一个类型,你需要声明一个该类型的属性。

public class AIPlayer
{
    public string PlayerPersonaName { get; set; }
    public string Description { get; set; }
    public EvaluateTurn Evaluation { get; set; }

    public delegate int EvaluateTurn(IQueryable<GameplayRound> playHistory);
}

Then:

new AIPlayer()
{
    PlayerPersonaName = "CopyCat" ,
    Description = "Hello! I start with Cooperate, and afterwards, I just copy whatever you did in the last round. Meow",
    Evaluation = playHistory => 1
};

或者你可以直接使用 Func 为简洁起见。

public class AIPlayer
{
    public string PlayerPersonaName { get; set; }
    public string Description { get; set; }
    public Func<IQueryable<GameplayRound>, int> Evaluation { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.