【发布时间】:2020-09-03 11:11:37
【问题描述】:
我正在创建一个由 6 张卡片组成的游戏,我想组合这些卡片,但问题是我一直在创建该功能。以下是我制作的代码
public class Cards
{
public int number{ get; set; }
public string symbol{ get; set; }
public Cards(int number, string symbol)
{
number= Number;
symbol = Symbol;
}
}
//populate list
List<Cards> CardList = new List<Cards>();
void InitCards()
{
CardList.Add(new Cards(1, "dogman"));
CardList.Add(new Cards(2, "dogman"));
CardList.Add(new Cards(1, "catman"));
CardList.Add(new Cards(2, "catman"));
CardList.Add(new Cards(1, "birdman"));
CardList.Add(new Cards(2, "birdman"));
}
void MakeCombination()
{
foreach (var card in CardList)
{
//make combination
}
}
我的预期输出是用一组两张卡获得所有可能的组合 下面的示例预期输出
1 dogman, 2 dogman
1 dogman, 1 catman
1 catman, 2 birdman
【问题讨论】:
-
预期的结果是什么?你说的组合是什么意思?也许相关:stackoverflow.com/questions/30081908
-
为什么你的输出中只有 3 个组合?例如,为什么没有 1dogman、2catman 或根本没有 dogman/birdman?
-
与你的问题没有太大关系,但最好打电话给你的卡类
Card。并且属性应该是 Pascal 大小写(以大写字符开头) -
同意汉诺;在 C# 中使类名单数。如果一个类代表一个集合,那么附加单词“Collection”比复数名称更有意义。作为 Card 的数组/列表的原因实际上应该称为 Cards(作为属性)->
List<Card> Cards。如果你打电话给你的班级卡片,你应该写List<Cards> Cardss或List<Cards> Cardses。List<Card>得到一个复数名称,因为它是某个其他类的属性。当一个类的唯一目的是成为其他东西的集合时,称之为-Collection。可以确定的细微差别 -
基本相同的问题as here。您只使用对象而不是整数。我只是使用 hashcode as a single value for your object 类