我要编写一个纸牌游戏,首先要做的是创建一个纸牌类和一个纸牌堆类。
Card类初始化一张空白卡片或一张已定义卡片…
public enum Suit { Clubs, Diamonds, Hearts, Spades }
public enum FaceValue {
Two, Three, Four, Five, Six, Seven, Eight, Nine,
Ten, Jack, Queen, King, Ace
}
public enum Color {
Red, Black
}
public class Card{
private FaceValue faceValue;
private Suit suit;
private Color color;
//construct a blank card
public Card() { }
// constuct a defined card
public Card(FaceValue FV, Suit S, Color C) {
faceValue = FV;
suit = S;
color = C;
}
然后,这是我的CardPile类
//create a empty CardPile
public CardPile() { }
public List<Card> pile;
public CardPile(bool newdeckquestionmark) {
pile = new List<Card>();
if (newdeckquestionmark) {
int index = 0;
int c;
foreach (Suit s in Enum.GetValues(typeof(Suit))) {
foreach (FaceValue fv in Enum.GetValues(typeof(FaceValue))) {
if ((index < 13) || (index > 38)) {
c = (int)Color.Red;
} else {
c = (int)Color.Black;
}
pile.Add(new Card()); // works
// pile.Add(new Card (fv, s, c)); // - FAILS.
index++;
}
}
}
}
}
}
我不知道我哪里出错了,是在card属性还是在我如何写堆。添加但我想要FOREACH FaceValue FOREACH Suit循环插入52张牌....
当我输入
pile.Add(new Card (fv, s, c));
我得到错误,
最佳重载方法匹配'Shared_Game_Class_Library. card . card (Shared_Game_Class_Library. card)。表面上,Shared_Game_Class_Library。Suit, Shared_Game_Class_Library.Color)'有一些无效的参数…
c
是int
,构造函数期望Color
:
public Card(FaceValue FV, Suit S, Color C)
您可以将c
转换为Color
,但是您应该将c
的声明类型更改为Color
。没有理由我可以看到使用int
我猜是因为你的c是int而不是Color试着把它转换成你的颜色。如果它是enum,那么为什么要将其转换为int?把你换成c;