简体   繁体   中英

Need help creating and displaying my deck of cards

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.

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());
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM