简体   繁体   中英

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

I'm trying to declare a delegate inline with my data in C# and that doesn't seem to compile. I'm hoping someone can help me (re)structure either my thoughts or my code.

My goal is to create different player types in an online game. Each player type has a strategy they employ. This strategy is employed by calling an inline async function that might call a HTTP REST resource as part of the IQueryable evaluation.

Question:

  • What is a maintainable way to configure the code required within the EvaluateTurn() delegate for each of the AIPlayer types?

More player types will be added, and the IQueryable to be a deferred execution Linq Query against CosmosDB, AzureTable, SQL, in-memory array etc.

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

A delegate is a type, you need to declare a property of that type:

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

Or you could just use Func for brevity:

public class AIPlayer
{
    public string PlayerPersonaName { get; set; }
    public string Description { get; set; }
    public Func<IQueryable<GameplayRound>, int> Evaluation { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM