Sign in to follow this  

Simple Card, Hand, and Deck Classes

This topic is 3592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here are some classes I wrote up to help with card, hand, and deck operations for simple card games. I make no claim that these are perfectly written classes, nor do I ensure that they are entirely efficient and correctly written. They're simply what I came up with. I'm posting these in the event that some beginner's want a basic idea of how to describe these three components to essentially all basic card games. Note: The deck class only supports random draw operations and no shuffle/sort operations. Note: These were created with console based games in mind. A simple card class:
// Card.cs
    public class Card
    {
        private int _number;        // number of the card
        private string _suit;       // suit of the card
        private string _special;    // special title such as King, Queen, etc. of the card

        // constructor takes a number and suit to generate a card
        // checks if the number is 1 (Ace), 11 (Jack), 12 (Queen), (13) King and assigns the special title
        public Card(int number, string suit)
        {
            _number = number + 1;
            _suit = suit;

            if (_number == 1)
                _special = "Ace";
            else if (_number == 11)
                _special = "Jack";
            else if (_number == 12)
                _special = "Queen";
            else if (_number == 13)
                _special = "King";
            else
                _special = "None";
        }

        // overriden ToString will display a proper title for a card
        // example: "Ace of Spades" will be output for 1 of Spades with special title Ace
        public override string ToString()
        {
            if (_special.Equals("None"))
                return (_number + " of " + _suit);
            else
                return (_special + " of " + _suit);
        }
    }




A simple hand class:
// Hand.cs
    public class Hand
    {
        private List<Card> _hand;

        public Hand()
        {
            _hand = new List<Card>();
        }

        // adds a card to the hand
        public void AddCard(Card card)
        {
            _hand.Add(card);
        }

        // displays the hand using the overridden ToString method within the Card class
        public void Display()
        {
            foreach (Card card in _hand)
                Console.WriteLine(card.ToString());
        }
    }




A simple deck class:
// Deck.cs
    // 13 Hearts, 13 Diamonds, 13 Clubs, 13 Spades (52 total cards)
    public class Deck
    {
        private List<Card> _deck;                                       // the deck itself
        private int _numCards = 52;                                     // the number of cards currently in the deck
        private enum _types { Hearts, Diamonds, Clubs, Spades };        // the types of suits
        private bool _disposed = false;                                 // used for proper disposal
        private Random _random;                                         // used to draw a random card from the deck

        // default constructor creates a new deck of size 52 and adds 13 of each suit to the deck
        public Deck()
        {
            _random = new Random();
            _deck = new List<Card>(_numCards);
            for (int i = 0; i <= 12; i++)
            {
                _deck.Add(new Card(i, _types.Hearts.ToString()));
                _deck.Add(new Card(i, _types.Diamonds.ToString()));
                _deck.Add(new Card(i, _types.Clubs.ToString()));
                _deck.Add(new Card(i, _types.Spades.ToString()));
            }
        }

        // displays the deck using the overridden ToString method within the Card class
        public void Display()
        {
            foreach(Card card in _deck)
                Console.WriteLine(card.ToString());
        }
        
        // draws a single card from the deck and returns it to the caller
        public Card Draw()
        {
            int index = _random.Next(1, Count());   // get a random index from which to draw between 1 and the number of cards remaining
            Card card = _deck[index];               // get the card from the deck at that index
            _deck.RemoveAt(index);                  // remove the card from the deck at that index
            return card;
        }

        public int Count()
        {
            return _deck.Count;
        }
    }




Share this post


Link to post
Share on other sites

This topic is 3592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this