我试图通过验证字符串值将点数添加到计数中,但是由于.equal和.contains仅适用于字符串,因此我不知道如何计数点。我应该为类型可变卡制作自己的.equal方法吗?要求该数字必须具有黑桃/心/钻石/俱乐部价值。
//card class...
class Card
{
public string s;
public string f;
public Card (string sC, string fC)
{
s = sC;
f = fC;
}
public override string ToString()
{
return f + s;
}
}
//Deck class....
string[] cards = {"2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K",
"A"};
//spades, diamond , club, heart. not necessarily in the same order
string[] type = { "\x2660", "\x2666", "\x2665", "\x2663" };
deck = new Card[52];
random = new Random();
for (int i = 0; i < deck.Length; i++)
{
deck[i] = new Card(cards[i % 13], type[i / 13]);
}
//output is spades of 2
Console.WriteLine(Deck[0]);
void Shuffle()
{
for (int i = 0; i < deck.Length; top++)
{
int j = random.Next(52);
Card temp = playDeck[i];
playDeck[i] = playDeck[j];
playDeck[j] = temp;
}
}
Card passCards()
{
if (currentCard < deck.Length)
{
return deck[currentCard++];
}
else
{
return null;
}
}
// Method I'm trying to make
int count =0;
int countPoints()
{
for(int i = currentCard; i < deck.Length; i++)
{
if (deck[currentCard].Equals("1"))
{
count = count + 1;
}
}
return count
}
//main...
Console.WriteLine("playername" + ...13 cards.. + Total points : " + deck.countPoints());
答案 0 :(得分:1)
一种实现方法是将卡的Name
属性存储为enum
,其中每个名称的位置与卡的值相关(至少对于已编号的卡)。例如:
// Start the Ace with value 1, the rest are automatically one greater than the previous
public enum CardName
{
Ace = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King
}
这使得计算特定名称的分数变得非常容易。如果值是1
,则返回11
(假设A是11),如果值大于9
,则返回10
(假设面卡是10),否则返回值:
public int Value
{
get
{
var value = (int) Name;
return value == 1 ? 11 : value > 9 ? 10 : value;
}
}
现在,如果我们有List<card> hand
,我们可以通过执行以下操作来获得手牌的总和:
int total = hand.Sum(card => card.Value);
为完整起见,下面是Card
类,Deck
类(这是List<Card>
周围的精美包装)的小样,并举例说明了它们如何您可以获得手牌的总和:
public enum Suit { Hearts, Clubs, Diamonds, Spades}
public enum CardName
{
Ace = 1, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King
}
public class Card
{
public Suit Suit { get; }
public CardName Name { get; }
public int Value => (int) Name == 1 ? 11 : (int) Name > 9 ? 10 : (int) Name;
public Card(CardName name, Suit suit)
{
Name = name;
Suit = suit;
}
public override string ToString()
{
return $"{Name} of {Suit}";
}
}
public class Deck
{
private readonly List<Card> cards = new List<Card>();
private readonly Random random = new Random();
public int Count => cards.Count;
public static Deck GetStandardDeck(bool shuffled)
{
var deck = new Deck();
deck.ResetToFullDeck();
if (shuffled) deck.Shuffle();
return deck;
}
public void Add(Card card)
{
cards.Add(card);
}
public bool Contains(Card card)
{
return cards.Contains(card);
}
public bool Remove(Card card)
{
return cards.Remove(card);
}
public int Sum => cards.Sum(card => card.Value);
public Card DrawNext()
{
var card = cards.FirstOrDefault();
if (card != null) cards.RemoveAt(0);
return card;
}
public void Clear()
{
cards.Clear();
}
public void ResetToFullDeck()
{
cards.Clear();
// Populate our deck with 52 cards
foreach (Suit suit in Enum.GetValues(typeof(Suit)))
{
foreach (CardName name in Enum.GetValues(typeof(CardName)))
{
cards.Add(new Card(name, suit));
}
}
}
public void Shuffle()
{
var thisIndex = cards.Count;
while (thisIndex-- > 1)
{
var otherIndex = random.Next(thisIndex + 1);
if (thisIndex == otherIndex) continue;
var temp = cards[otherIndex];
cards[otherIndex] = cards[thisIndex];
cards[thisIndex] = temp;
}
}
}
class Program
{
private static void Main()
{
var deck = Deck.GetStandardDeck(true);
var playerHand = new Deck();
var computerHand = new Deck();
Console.WriteLine("Each of us will draw 5 cards. " +
"The one with the highest total wins.");
for (int i = 0; i < 5; i++)
{
GetKeyFromUser($"\nPress any key to start round {i + 1}");
var card = deck.DrawNext();
Console.WriteLine($"\nYou drew a {card}");
playerHand.Add(card);
card = deck.DrawNext();
Console.WriteLine($"I drew a {card}");
computerHand.Add(card);
}
while (playerHand.Sum == computerHand.Sum)
{
Console.WriteLine("\nOur hands have the same value! Draw another...");
var card = deck.DrawNext();
Console.WriteLine($"\nYou drew a {card}");
playerHand.Add(card);
card = deck.DrawNext();
Console.WriteLine($"I drew a {card}");
computerHand.Add(card);
}
Console.WriteLine($"\nYour total is: {playerHand.Sum}");
Console.WriteLine($"My total is: {computerHand.Sum}\n");
Console.WriteLine(playerHand.Sum > computerHand.Sum
? "Congratulations, you're the winner!"
: "I won this round, better luck next time!");
GetKeyFromUser("\nDone! Press any key to exit...");
}
private static ConsoleKeyInfo GetKeyFromUser(string prompt)
{
Console.Write(prompt);
var key = Console.ReadKey();
Console.WriteLine();
return key;
}
}
输出