我可以在对象的 New() 构造函数中声明委托吗?还是带初始化参数?
Can I declare a delegate in an Object's New() constructor? Or with initialization parameters?
我试图在 C# 中声明一个与我的数据内联的委托,但它似乎无法编译。我希望有人可以帮助我(重新)构建我的想法或我的代码。
我的目标是在在线游戏中创建不同的玩家类型。每种玩家类型都有他们采用的策略。通过调用内联异步函数来采用此策略,该函数可能调用 HTTP REST 资源作为 IQueryable 评估的一部分。
问题:
- 为每个
AIPlayer
类型配置 EvaluateTurn()
委托中所需代码的可维护方法是什么?
将添加更多播放器类型,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;
}
委托是一种类型,您需要声明该类型的属性:
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; }
}
我试图在 C# 中声明一个与我的数据内联的委托,但它似乎无法编译。我希望有人可以帮助我(重新)构建我的想法或我的代码。
我的目标是在在线游戏中创建不同的玩家类型。每种玩家类型都有他们采用的策略。通过调用内联异步函数来采用此策略,该函数可能调用 HTTP REST 资源作为 IQueryable 评估的一部分。
问题:
- 为每个
AIPlayer
类型配置EvaluateTurn()
委托中所需代码的可维护方法是什么?
将添加更多播放器类型,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;
}
委托是一种类型,您需要声明该类型的属性:
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; }
}