Reputation: 21
I'm trying to add points into count by verifying the string value but since .equal and .contains only works with string, i have no idea how to count points. Am i supposed to make my own .equal method for type variable card? The requirement is that the number must have a spades/heart/diamond/club value.
//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());
Upvotes: 0
Views: 260
Reputation: 37060
One way to do it would be to store the card Name
property as an enum
, where the position of each name correlates to the card's value (at least for the numbered cards). For example:
// 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
}
This makes calculating the score for a specific name pretty easy. If the value is 1
then return 11
(assuming Aces are 11), if it's greater than 9
, then return 10
(assuming face cards are 10), otherwise return the value:
public int Value
{
get
{
var value = (int) Name;
return value == 1 ? 11 : value > 9 ? 10 : value;
}
}
And now, if we have a List<card> hand
, we can get the sum of the cards in the hand by doing something like:
int total = hand.Sum(card => card.Value);
For completeness, here's a little sample of a Card
class, a Deck
class (which is a fancy wrapper around a List<Card>
), and an example usage of them to show how you can get the sum of a player's hand:
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;
}
}
Output
Upvotes: 1