我的C#中的BlackJack游戏有这个枚举。我的游戏正常运行,除了我如何显示2而不是2?
public enum Face
{
A, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten,
J, Q, K
}
答案 0 :(得分:5)
按如下所示定义枚举:
var allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Credentials', 'true');
res.header('Access-Control-Allow-Headers', 'Content-Type,x-phone-token,Location');
next();
}
然后您可以使用强制转换 public enum Face
{
A=1,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
J,
Q,
K
}
来访问整数值,这将给您(int)Face.J
,请参考此工作Example
请注意::您可以为枚举分配整数值,如果未分配,则它将假定第一个值,因为11
0
将添加到后续项中。这意味着,如果您在未分配的情况下使用代码+1
,则将获得的值为(int)Face.J
而不是10
答案 1 :(得分:2)
创建绑定字典:
public enum Face
{
A, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, J, Q, K
}
public Dictionary<Face, string> cardValues = new Dictionary<Face, string> {
[Face.A] = "A",
[Face.Two] = "2",
[Face.Three] = "3",
...
}
public string card = cardValues[Face.Two]; // "2"
我个人只是将卡值存储为整数,并使用数组而不是字典和枚举。
答案 2 :(得分:0)
我通过分配不同类中的值解决了二十一点游戏。
sum
如果先前的卡的总值小于11,则Ace值应为11,否则为1
class Card
{
//CREATE OUR RANK ENUM
public enum SUIT
{
HEARTS,
DIAMONDS,
SPADES,
CLUBS
}
public enum RANK
{
TWO , THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE,
TEN, JACK, QUEEN, KING, ACE
}
public SUIT MySuit { get; set; }
public RANK MyRank { get; set; }
}
在表格中,我将字典字符串值用于要显示的卡的文件名,并获取相应的值
class Hand
{
private List<Card> cards;
public Hand()
{
cards = new List<Card>();
}
public void Clear()
{
cards.Clear();
}
public void Add(Card card)
{
cards.Add(card);
}
public void PopulateDeck()
{
foreach (Card.SUIT s in Enum.GetValues(typeof(Card.SUIT)))
{
foreach (Card.RANK r in Enum.GetValues(typeof(Card.RANK)))
{
cards.Add(new Card { MySuit = s, MyRank = r });
}
}
Shuffle();
}
public void Shuffle()
{
Random x = new Random();
var shuffledcards = cards.OrderBy(a => x.Next()).ToList();
foreach (Card c in shuffledcards)
{
cards.Add(c);
}
}
public Boolean Give(Card card, Hand otherHand)
{
if (!cards.Contains(card))
{
return false;
}
else
{
cards.Remove(card);
otherHand.Add(card);
return true;
}
}//end method
public void Deal(Hand hand, int perHand)
{
//deal to a single hand
for (int i = 0; i < perHand; i++)
{
if(cards.Count < 1)
{
MessageBox.Show("No more cards to deal","",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else {
this.Give(cards.ElementAt(0), hand);
}
}
} //end method
public Dictionary<String,int> GetCardRankSuit()
{
string cardSuit = "";
string cardRank = "";
string cardRanksuit = "";
int rankValue = 0;
int totalPoints = 0;
Dictionary<String, int> rankList = new Dictionary<String, int>();
//clears the collection of previous cards from previous games
rankList.Clear();
/*getting total points(any Aces by default will be 1)
loop through our cards*/
foreach (Card c in cards)
{
switch (c.MySuit)
{
case Card.SUIT.CLUBS:
cardSuit = "Clubs";
break;
case Card.SUIT.DIAMONDS:
cardSuit = "Diamonds";
break;
case Card.SUIT.HEARTS:
cardSuit = "Hearts";
break;
case Card.SUIT.SPADES:
cardSuit = "Spades";
break;
} //end switch
switch (c.MyRank)
{
case Card.RANK.TWO:
rankValue = 2;
cardRank = "2 of ";
break;
case Card.RANK.THREE:
rankValue = 3;
cardRank = "3 of ";
break;
case Card.RANK.FOUR:
rankValue = 4;
cardRank = "4 of ";
break;
case Card.RANK.FIVE:
rankValue = 5;
cardRank = "5 of ";
break;
case Card.RANK.SIX:
rankValue = 6;
cardRank = "6 of ";
break;
case Card.RANK.SEVEN:
rankValue = 7;
cardRank = "7 of ";
break;
case Card.RANK.EIGHT:
rankValue = 8;
cardRank = "8 of ";
break;
case Card.RANK.NINE:
rankValue = 9;
cardRank = "9 of ";
break;
case Card.RANK.TEN:
rankValue = 10;
cardRank = "10 of ";
break;
case Card.RANK.JACK:
rankValue = 10;
cardRank = "Jack of ";
break;
case Card.RANK.QUEEN:
rankValue = 10;
cardRank = "Queen of ";
break;
case Card.RANK.KING:
rankValue = 10;
cardRank = "King of ";
break;
case Card.RANK.ACE:
if (totalPoints < 11)
{
rankValue = 11;
cardRank = "Ace of ";
}
else
{
rankValue = 1;
cardRank = "Ace of ";
}
break;
}
totalPoints = totalPoints + rankValue;
cardRanksuit = cardRank + cardSuit;
rankList.Add(cardRanksuit, rankValue);
}
return rankList;
} // end method
}//end class
然后当我发给我在字典中循环的纸牌时,玩家便是类手的对象
private void CreateControlsPlayer(String pictureName, int rankValueCard, Panel panel)
{
string folder = "Cards/";
string fileExtension = ".png";
//creates dynamically picturebox
var newpictureBox = new PictureBox();
newpictureBox.Width = 70;
newpictureBox.Height = 130;
newpictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
newpictureBox.Image = Image.FromFile(folder + pictureName + fileExtension);
newpictureBox.Location = new Point(xPlayer, 60);
newpictureBox.Visible = true;
newpictureBox.Name = "picBox" + "_" + xPlayer;
xPlayer = xPlayer + 30;
panel.Controls.Add(newpictureBox);
}
不确定它是否有效,只是开始重新进行编程并创建纸牌游戏作为练习