好的,我需要设计一种方法来跟踪每个项目的数量。 大约有 26 个项目。我还需要一种方法来查明是否存在特定的项目组合。
例如, 这是纸牌游戏的引擎。每张卡都有不同的类型,每张卡都可以附加卡。 需要有一定的卡牌组合才能让玩家在游戏中做某些事情。 为了简化这个程序,我想做一些类似的事情
if (meetsCrit(2, water, 4, ground))
{
do this()
}
else
{
displayerror()
}
编辑:已解决!
我使用了下面几篇文章中描述的技术组合。 特别提到:
乔恩双向飞碟, 里纳特·阿卜杜林, 弗兰克,
无论如何,这就是我所做的 我创建了一个名为 pair 的类,其中存储了我要查找的类型以及该类型的数量。 然后我使用 Predicate Delegate 来查找所有该类型并计算有多少, 然后我将它与我正在搜索的数字进行比较,并分别返回 true 或 false。
这是它的代码
public bool meetsCrit(params Pair[] specs)
{
foreach (Pair i in specs)
{
if (!(attached.FindAll(delegate(Card c) { return c.type == i.type; }).Count >= i.value))
{
return false;
}
}
return true;
}
最佳答案
使用params :
The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.
方法声明中params关键字后不允许有额外的参数,方法声明中只允许有一个params关键字...
关于c# - 如何在C#中传递任意数量的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/489317/