Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / Visual Basic

Playing Cards

Rate me:
Please Sign up or sign in to vote.
3.57/5 (4 votes)
30 Aug 2008CPOL2 min read 40.2K   1.3K   20  
A set of classes that represent a typical set of playing cards.
using System.Collections;
using System;

namespace PlayingCards
{
    [Serializable()]
    public class Deck : ICloneable
    {
        #region "Fields"
        private InnerDeck _deck;
        #endregion

        #region "Properties"
        public int Count
        {
            get { return _deck.Count; }
        }
        #endregion

        #region "Constructors"
        /// <summary>
        /// Create a single 52 Card deck.
        /// </summary>
        public Deck() { _deck = new InnerDeck(); }

        /// <summary>
        /// Create multiple 52 Card decks.
        /// </summary>
        /// <param name="decks">The number of decks to create.</param>
        /// <exception cref="System.ArgumentException">number of decks is less than 1.</exception>
        public Deck(int decks)
        {
            if (decks < 1) { throw new ArgumentException(decks + " requested to be created.  The number of decks must not be less than 1."); }
            _deck = new InnerDeck(decks);
        }
        #endregion

        #region "Methods"
        /// <summary>
        /// Creates a shallow copy of the PlayingCards.Deck.
        /// </summary>
        /// <returns>A PlayingCards.Deck object.</returns>
        public object Clone() { return _deck.Clone(); }

        /// <summary>
        /// Returns an enumerator for the entire PlayingCards.Deck.
        /// </summary>
        /// <returns>An System.Collections.IEnumerator for the entire PlayingCards.Deck.</returns>
        public IEnumerator GetEnumerator() { return _deck.GetEnumerator(); }

        /// <summary>
        /// Returns an enumerator for a range of elements in the PlayingCards.Card.
        /// </summary>
        /// <param name="index">The zero-based starting index of the PlayingCards.Card section that the enumerator should refer to.</param>
        /// <param name="count">The number of elements in the PlayingCards.Card section that the enumerator should refer to.</param>
        /// <returns>An System.Collections.IEnumerator for the specified range of elements in the PlayingCards.Deck.</returns>
        /// <exception cref="System.ArgumentException">index and count do not specify a valid range in the PlayingCards.Deck.</exception
        /// <exception cref="System.ArgumentOutOfRangeException">index is less than zero.-or- count is less than zero.</exception>
        public IEnumerator GetEnumerator(int index, int count) { return _deck.GetEnumerator(index, count); }

        /// <summary>
        /// Searches for the specified System.Object and returns the zero-based index of the first occurrence within the entire PlayingCards.Deck.
        /// </summary>
        /// <param name="value">The System.Object to locate in the PlayingCards.Deck. The value can be null.</param>
        /// <returns>The zero-based index of the first occurrence of value within the entire PlayingCards.Deck, if found; otherwise, -1.</returns>
        public int IndexOf(object value) { return _deck.IndexOf(value); }

        /// <summary>
        /// Searches for the specified System.Object and returns the zero-based index of the first occurrence within the range of elements in the PlayingCards.Dec that extends from the specified index to the last element.
        /// </summary>
        /// <param name="value">The System.Object to locate in the PlayingCards.Deck. The value can be null.</param>
        /// <param name="startIndex">The zero-based starting index of the search.</param>
        /// <returns>The zero-based index of the first occurrence of value within the entire PlayingCards.Deck, if found; otherwise, -1.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">startIndex is outside the range of valid indexes for the PlayingCards.Card.</exception>
        public int IndexOf(object value, int startIndex) { return _deck.IndexOf(value,startIndex); }

        /// <summary>
        /// Searches for the specified System.Object and returns the zero-based index of the first occurrence within the range of elements in the PlayingCards.Deck that starts at the specified index and contains the specified number of elements.
        /// </summary>
        /// <param name="value">The System.Object to locate in the System.Collections.ArrayList. The value can be null.</param>
        /// <param name="startIndex">The zero-based starting index of the search.</param>
        /// <param name="count">The number of elements in the section to search.</param>
        /// <returns>The zero-based index of the first occurrence of value within the entire PlayingCards.Deck, if found; otherwise, -1.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException"> startIndex is outside the range of valid indexes for the System.Collections.ArrayList.-or- count is less than zero.-or- startIndex and count do not specify a valid section in the System.Collections.ArrayList.</exception>
        public int IndexOf(object value, int startIndex, int count) { return _deck.IndexOf(value, startIndex, count); }

        /// <summary>
        /// Searches for the specified System.Object and returns the zero-based index of the last occurrence within the entire PlayingCards.Card.
        /// </summary>
        /// <param name="value">The System.Object to locate in the PlayingCards.Card. The value can be null.</param>
        /// <returns>The zero-based index of the last occurrence of value within the entire the PlayingCards.Card, if found; otherwise, -1</returns>
        public int LastIndexOf(object value) { return _deck.LastIndexOf(value); }

        /// <summary>
        /// Searches for the specified System.Object and returns the zero-based index of the last occurrence within the range of elements in the PlayingCards.Deck that extends from the first element to the specified index.
        /// </summary>
        /// <param name="value">The System.Object to locate in the PlayingCards.Card. The value can be null.</param>
        /// <param name="startIndex">The zero-based starting index of the backward search.</param>
        /// <returns>The zero-based index of the last occurrence of value within the entire the PlayingCards.Card, if found; otherwise, -1</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">startIndex is outside the range of valid indexes for the PlayingCards.Deck.</exception>
        public int LastIndexOf(object value, int startIndex) { return _deck.LastIndexOf(value, startIndex); }

        /// <summary>
        /// Searches for the specified System.Object and returns the zero-based index of the last occurrence within the range of elements in the PlayingCards.Deck that extends from the first element to the specified index.
        /// </summary>
        /// <param name="value">The System.Object to locate in the PlayingCards.Card. The value can be null.</param>
        /// <param name="startIndex">The System.Object to locate in the PlayingCards.Card. The value can be null.</param>
        /// <param name="count">The System.Object to locate in the PlayingCards.Card. The value can be null.</param>
        /// <returns>The zero-based index of the last occurrence of value within the entire the PlayingCards.Card, if found; otherwise, -1</returns>        
        /// <exception cref="System.ArgumentOutOfRangeException">startIndex is outside the range of valid indexes for the System.Collections.ArrayList.-or- count is less than zero.-or- startIndex and count do not specify a valid section in the PlayingCards.Deck.</exception>
        public int LastIndexOf(object value, int startIndex, int count) { return _deck.LastIndexOf(value, startIndex, count); }

        /// <summary>
        /// Reverses the order of the elements in the entire PlayingCards.Deck.
        /// </summary>
        /// <exception cref="System.NotSupportedException">The System.Collections.ArrayList is read-only.</exception>
        public void Reverse() { _deck.Reverse(); }

        /// <summary>
        /// Reverses the order of the elements in the specified range.
        /// </summary>
        /// <param name="index">The zero-based starting index of the range to reverse.</param>
        /// <param name="count">The number of elements in the range to reverse.</param>
        /// <exception cref="System.ArgumentOutOfRangeException">index is less than zero.-or- count is less than zero.</exception>
        /// <exception cref="System.NotSupportedException">The System.Collections.ArrayList is read-only.</exception>
        ///<exception cref="System.ArgumentException">index and count do not denote a valid range of elements in the System.Collections.ArrayList.</exception>
        public void Reverse(int index, int count) { _deck.Reverse(index, count); }

        /// <summary>
        /// Order the cards in a random order.
        /// </summary>
        public void Shuffle() { _deck.Shuffle(); }


        /// <summary>
        /// Sort the cards in the entire PlayingCards.Deck using the default System.IComparable implementation of each PlayingCards.Card.
        /// <exception cref="System.NotSupportedException">The PlayingCards.Card is read-only.</exception>
        /// </summary>
        public void Sort() { _deck.Sort(); }

        /// <summary>
        /// Sort the cards in the entire PlayingCards.Deck using the given System.IComparable implementation.
        /// </summary>
        /// <param name="comparer">The IComparer object to determine the sorting order.</param>
        /// <exception cref="System.NotSupportedException">The PlayingCards.Card is read-only.</exception>
        public void Sort(IComparer comparer) { _deck.Sort(comparer); }

        /// <summary>
        /// Sort the cards in the range of Cards starting at the index through the count using the given System.IComparer implementation.
        /// </summary>
        /// <param name="index">The zero-based starting index of the range to sort.</param>
        /// <param name="count">The length of the range to sort.</param>
        /// <param name="comparer">The System.Collections.IComparer implementation to use when comparing PlayingCards.Card.-or- null to use the System.IComparable implementation of each element.</param>
        /// <exception cref="System.ArgumentException">index and count do not specify a valid range in the System.Collections.ArrayList.</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">index is less than zero.-or- count is less than zero.</exception>
        /// <exception cref="System.NotSupportedException">The PlayingCards.DeckS is read-only.</exception>
        public void Sort(int index, int count, IComparer comparer) { _deck.Sort(index, count, comparer); }


        /// <summary>
        /// Returns a System.String that represents the current PlayingCards.Deck.
        /// </summary>
        /// <returns>A System.String</returns>
        public override String ToString() { return _deck.ToString(); }
        #endregion


        #region "Inner Deck class"
        /// <summary>
        /// A private class that inherits the CardArray class to expose its methods to the containing class.
        /// </summary>
        private class InnerDeck : CardArray
        {
            private const int _size = 52;

            public InnerDeck()
                : base(_size)
            {
                for (int i = 0; i < _size; i++) { base.Add(new Card(i)); }
            }

            public InnerDeck(int decks)
                : base(_size * decks)
            {
                for (int j = 0; j < decks; j++)
                {
                    for (int i = 0; i < _size; i++) { base.Add(new Card(i)); }
                }
            }
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Arroweye Solutions
United States United States
I am a .net software developer based out of Chicago.

Comments and Discussions