Click here to Skip to main content
15,897,291 members
Articles / Programming Languages / SQL

rdsCards - A Card Deck Library using RDS

Rate me:
Please Sign up or sign in to vote.
4.43/5 (4 votes)
17 Jul 2012CPOL3 min read 26.7K   328   10  
Create an easy-to-use Card Deck / Deal / Shuffle library with RDS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using rds;

namespace rdsCards
{
	public enum Decksize
	{
		AceToTen20Cards = 5,
		AceToSeven32Cards = 8,
		Poker52Cards = 13,
		Rummy104Cards = 26,
		Canasta156Cards = 39
	}

	public enum Suit
	{
		None = 0,
		Spades = 1,
		Hearts = 2,
		Diamonds = 3,
		Clubs = 4
	}

	/// <summary>
	/// RDSTable to hold a deck of cards
	/// </summary>
	public class CardDeck
	{
		#region STATIC STRINGS AND CONSTRUCTORS
		internal static string stringsuits = " ♠♥♦♣";
		internal static string stringcards = "AKQJT98765432*";

		private RDSTable mdeck = new RDSTable();

		/// <summary>
		/// Initializes a new instance of the <see cref="CardDeck"/> class with no jokers.
		/// </summary>
		/// <param name="deck">The deck size to generate</param>
		public CardDeck(Decksize deck) : this(deck, 0) { }

		/// <summary>
		/// Initializes a new instance of the <see cref="CardDeck"/> class.
		/// </summary>
		/// <param name="deck">The deck size to generate</param>
		/// <param name="numjokers">The number of jokers in the deck.</param>
		public CardDeck(Decksize deck, int numjokers)
		{
			// We are using a trick here:
			// The enum cast to int gives us the number of chars of the stringcards string
			// we will use to fill the table.
			// For Rummy and Canasta we do this multiple times
			int ccount = (int)deck;
			int multiples = (ccount <= 13 ? 1 : ccount / 13);
			int maxcount = (ccount <= 13 ? ccount : 13);

			for (int multi = 0; multi < multiples; multi++)
			{
				for (int s = 1; s <= 4; s++)
				{
					for (int c = 0; c < maxcount; c++)
					{
						mdeck.AddEntry(new Card(stringcards[c].ToString(), (Suit)s), 1);
					}
				}
			}

			for (int i = 0; i < numjokers; i++)
				mdeck.AddEntry(new Card("*", Suit.None), 1);
		}
		#endregion

		#region SHUFFLE
		/// <summary>
		/// Shuffles this deck. "Shuffling" in rds terms just means to set all cards to "IsDealt=false"
		/// as RDS does the random work us.
		/// </summary>
		public void Shuffle()
		{
			foreach (Card c in mdeck.rdsContents)
				c.IsDealt = false;
		}
		#endregion

		#region DEAL
		/// <summary>
		/// Deals the specified number of cards from the deck.
		/// If there are not enough cards left in the deck, the returned number of cards may be lower than numcards.
		/// </summary>
		/// <param name="numcards">The numcards.</param>
		/// <returns></returns>
		public IEnumerable<Card> Deal(int numcards)
		{
			mdeck.rdsCount = numcards;
			return mdeck.rdsResult.Cast<Card>().ToList();
		}
		#endregion

		#region DECK
		/// <summary>
		/// Gets the entire deck as list of cards
		/// </summary>
		public IEnumerable<Card> Deck
		{
			get { return mdeck.rdsContents.Cast<Card>(); }
		}
		#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)
Austria Austria
Software Developer since the late 80's, grew up in the good old DOS-Era, switched to windows with Win95 and now doing .net since early 2002 (beta).
Long year c# experience in entertainment software, game programming, directX and XNA as well as SQLServer (DBA, Modelling, Optimizing, Replication, etc) and Oracle Databases in Enterprise environments. Started with Android development in 2014.

Developer of the gml-raptor platform (See my github profile below).

My Game Developer Profile at itch.io
My Repositories at github

Comments and Discussions