-1

I'm pretty new at c# coding and I'm having trouble displaying my cards shuffled side by side, and I'm not sure if I coded the cards and deck correctly in the first place. Anything is helpful including tips!

My Enums

public enum SuitEnum
    {
        Hearts,
        Diamonds,
        Clubs,
        Spades
    }

    public enum ValueEnum
    {
        Ace = 1,
        Two = 2,
        Three = 3,
        Four = 4,
        Five = 5,
        Six = 6,
        Seven = 7,
        Eight = 8,
        Nine = 9,
        Ten = 10,
        Jack = 10,
        Queen = 10,
        King = 10
    }

The Card class

public class Card
    {       
        SuitEnum Suit { get; set; }
        ValueEnum Value { get; set; }

        public Card(SuitEnum cardSuit, ValueEnum cardValue)
        {
            Suit = cardSuit;
            Value = cardValue;
        }
    }

And finally my Deck class

public class Deck
    {
        public List<Card> Cards { get; set; }

        public Deck()
        {
            Cards = new List<Card>();
            foreach (SuitEnum suit in Enum.GetValues(typeof(SuitEnum)))
            {
                foreach (ValueEnum value in Enum.GetValues(typeof(ValueEnum)))
                {
                    Cards.Add(new Card(suit, value));
                }
            }                      
        }

So now that I have my cards and deck coded how would I display all 52 cards in my main program code shuffled each time? Would I have to make another method in a class? Or perhaps just make a loop in my main code? Let me know what you think.

  • *and I'm not sure if I coded the cards and deck correctly in the first place*. Then write unit tests to validate the correct behavior of your application. – Daniel Mann Mar 18 '19 at 14:03
  • Cards have a rank *and* a value with the value depends on what the game is. Poker and Euchre use different values. The Card calss ought to override `ToString()` – Ňɏssa Pøngjǣrdenlarp Mar 18 '19 at 14:09
  • 1
    I can point out be careful with [duplicate enum values](https://stackoverflow.com/questions/8043027/non-unique-enum-values). You can have duplicates but this impacts equality and conversion. – Zer0 Mar 18 '19 at 14:10
  • 2
    Welcome to StackOverflow. Questions like this are too broad to be answered, as it seems that you simply don’t know how to design, organize and create a running program rather than having a problem in your actual code you’re having an hard time with. Please take a tour to the help center to see how to ask good questions. Good luck! – Davide Vitali Mar 18 '19 at 14:12
  • There is a ton of different way for exemple if we drop the enum and build simple classe we can easly generate a deck like: `(from suit in suits from value in "23456789JQKA" select new Card(suit, value)).ToArray();` 3 lines, simple scalar product in LinQ. Clear and readable. – xdtTransform Mar 18 '19 at 14:48
  • Card/Deck /shuffle is one of the most common excercice. you will be surprise by the number of questions about it on StackOverflow and CodeReview, ranging from broken code to complete solution with PNG and GUI – xdtTransform Mar 18 '19 at 14:52
  • Here is a Metric ton of solution : https://codereview.stackexchange.com/questions/tagged/c%23+playing-cards. – xdtTransform Mar 18 '19 at 14:55
  • And mandatory : https://blog.codinghorror.com/shuffling/ https://blog.codinghorror.com/the-danger-of-naivete/ – xdtTransform Mar 18 '19 at 14:58

1 Answers1

1

I don't know your application but because you are new in StackOverflow, so I changed some code for your card shuffle need. I removed the enum duplication and also add a display method in Card class. I generating a random number from the given range with checking of repetition. I hope it will work for you.

public enum SuitEnum
{
    Hearts,
    Diamonds,
    Clubs,
    Spades
}

public enum ValueEnum
{
    Ace = 1,
    Two = 2,
    Three = 3,
    Four = 4,
    Five = 5,
    Six = 6,
    Seven = 7,
    Eight = 8,
    Nine = 9,
    Ten = 10,
    Jack = 11,
    Queen = 12,
    King = 13
}

public class Card
{       
    SuitEnum Suit { get; set; }
    ValueEnum Value { get; set; }

    public Card(SuitEnum cardSuit, ValueEnum cardValue)
    {
        Suit = cardSuit;
        Value = cardValue;
    }

    public string Display()
    {
        return Suit + "" + Value.ToString();
    }
}

    public class Deck
    {
        public List<Card> Cards { get; set; }

        public Deck()
        {
            Random rand = new Random();
            Cards = new List<Card>();
            List<int> possibleSuit = Enumerable.Range(1, 4).ToList();
            List<int> listNumberSuite = new List<int>();
            for (int i = 0; i < 4; i++)
            {
                int index = rand.Next(0, possibleSuit.Count);
                listNumberSuite.Add(possibleSuit[index]);

                SuitEnum suit = (SuitEnum)possibleSuit[index];
                possibleSuit.RemoveAt(index);
                List<int> possibleDeck = Enumerable.Range(1, 13).ToList();
                List<int> listNumberDeck = new List<int>();

                for (int j = 0; j < 13; j++)
                {
                    int indexDeck = rand.Next(0, possibleDeck.Count);
                    listNumberDeck.Add(possibleDeck[indexDeck]);

                    ValueEnum deck = (ValueEnum)possibleDeck[indexDeck];
                    Cards.Add(new Card(suit, deck));

                    possibleDeck.RemoveAt(indexDeck);
                }
            }
            foreach (Card c in Cards)
            {
                Console.WriteLine(c.Display());
            }
        }
    }