簡體   English   中英

我可以在對象的 New() 構造函數中聲明一個委托嗎? 還是帶初始化參數?

[英]Can I declare a delegate in an Object's New() constructor? Or with initialization parameters?

我試圖在 C# 中聲明一個與我的數據內聯的委托,但這似乎無法編譯。 我希望有人可以幫助我(重新)構建我的想法或代碼。

我的目標是在在線游戲中創建不同的玩家類型。 每種玩家類型都有他們采用的策略。 此策略通過調用內聯異步 function 來使用,該資源可能會調用 HTTP REST 資源作為 IQueryable 評估的一部分。

問題:

  • 為每個AIPlayer類型配置EvaluateTurn()委托中所需的代碼的可維護方式是什么?

將添加更多播放器類型,並將IQueryable延遲執行 Linq 查詢 CosmosDB、AzureTable、SQL、內存數組等。

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;
}

委托是一種類型,您需要聲明該類型的屬性:

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);
}

然后:

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; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM