<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iCodeBlog &#187; iphone card deck</title>
	<atom:link href="/tag/iphone-card-deck/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Wed, 22 Sep 2010 00:14:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>iPhone Game Programming Series: Blackjack &#8211; Part 1: The Deck</title>
		<link>http://icodeblog.com/2010/09/09/iphone-game-programming-series-blackjack-part-1-the-deck/</link>
		<comments>http://icodeblog.com/2010/09/09/iphone-game-programming-series-blackjack-part-1-the-deck/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 13:00:01 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[iPhone Game Programming]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[blackjack]]></category>
		<category><![CDATA[card deck]]></category>
		<category><![CDATA[iphone card deck]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone game]]></category>
		<category><![CDATA[iphone programming]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2299</guid>
		<description><![CDATA[<a href="/2010/09/09/iphone-game-programming-series-blackjack-part-1-the-deck/"><img align="left" hspace="5" width="150" height="150" src="/wp-content/uploads/2010/09/secret-of-blackjack1-150x150.jpg" class="alignleft tfe wp-post-image" alt="secret-of-blackjack" title="secret-of-blackjack" /></a>It has been quite some time since our last iPhone video game series and now we are ready to start a new one.  Given the success of our iTennis tutorial series, we will be following along the same line and [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ficodeblog.com%2F2010%2F09%2F09%2Fiphone-game-programming-series-blackjack-part-1-the-deck%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ficodeblog.com%2F2010%2F09%2F09%2Fiphone-game-programming-series-blackjack-part-1-the-deck%2F&amp;source=elctech&amp;style=normal&amp;hashtags=blackjack,card+deck,iphone+card+deck,iPhone+Coding,iphone+game,iphone+programming" height="61" width="50" /><br />
			</a>
		</div>
<p>It has been quite some time since our last <a href="/2009/01/15/iphone-game-programming-tutorial-part-1/">iPhone video game series</a> and now we are ready to start a new one.  Given the success of our iTennis tutorial series, we will be following along the same line and create a game without using OpenGL ES.  If you are interested in OpenGL ES programming, check out <a href="http://iphonedevelopment.blogspot.com/">Jeff Lamarche&#8217;s blog</a>, he&#8217;s super rad.  In this series we will be creating a simple Blackjack game with the following features/functionality:</p>
<ul>
<li>Deck of cards</li>
<li>Basic Blackjack rules/logic</li>
<li>Dealer</li>
<li>Basic Dealer AI</li>
<li>Player</li>
<li>Controls for hit/stand</li>
<li>Audio</li>
<li>User Interface</li>
</ul>
<p>In this first part of the series we are going to introduce the core of the game; the deck.  We will be building a very generic deck of cards that could be used in any type of card game.  The deck will be portable enough to just drag and drop into any project that requires a deck of cards.  So, let&#8217;s begin by creating a view based application and calling it ICBBlackjack.</p>
<h2>Creating A Card Object</h2>
<p>Before we can create the deck, we need to create a card.  Before we dig into the code, let me explain how a card object will work.  A card has 2 properties that we care about.  They are suit and value.  Suit is pretty obvious (Hearts, Diamonds, Spades, Clubs), but the value might not be what you&#8217;d expect.  For the cases of the numeric cards, the value is simply their value, however it changes a bit for the face cards and ace.</p>
<p>The Ace gets assigned a value of 1 (pretty obvious), while the face cards get values based on their ordering from the 10 card.  So a Jack = 11, Queen = 12, and King = 13.  Obviously this isn&#8217;t the case when playing a game like Blackjack, but we will let another class figure that out. For now, we will use the value as and identifier for the card.  Add a new class file to your project called <strong>Card.m</strong>.  Make sure you check the box to include the .h file. Open up Card.h and add the following code:</p>
<h3>Card.h</h3>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">typedef</span> <span style="color: #a61390;">enum</span> <span style="color: #002200;">&#123;</span>
	Hearts,
	Diamonds,
	Spades,
	Clubs
<span style="color: #002200;">&#125;</span> Suit;
&nbsp;
<span style="color: #6e371a;">#define Ace   1</span>
<span style="color: #6e371a;">#define Jack  11</span>
<span style="color: #6e371a;">#define Queen 12</span>
<span style="color: #6e371a;">#define King  13</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Card <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
	NSInteger value;
	Suit suit;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic<span style="color: #002200;">&#41;</span> NSInteger value;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic<span style="color: #002200;">&#41;</span> Suit suit;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithValue<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> aValue suit<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Suit<span style="color: #002200;">&#41;</span> aSuit;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Ok, so a few things here to point out.  The first thing we see at the top is an enum.  The enum allows us to declare our own type called Suit.  We could have just as easily used constants, but the enum makes things a little more clear.  This allows us to do things like <strong>myCard.suit = Clubs</strong>.</p>
<p>Now, we define the &#8220;Special&#8221; values which will be assigned to the Ace and face cards.  Finally, we declare our value and suit properties and declare and init method to build a card with these values set.  Now, it&#8217;s time to implement our Card class.  Open Card.m and add the following code.</p>
<h3>Card.m</h3>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Card.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Card<span style="color: #002200;">&#40;</span>Private<span style="color: #002200;">&#41;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> valueAsString;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> suitAsString;
&nbsp;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Card
&nbsp;
<span style="color: #a61390;">@synthesize</span> value,suit;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithValue<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> aValue suit<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>Suit<span style="color: #002200;">&#41;</span> aSuit <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		self.value <span style="color: #002200;">=</span> aValue;
		self.suit <span style="color: #002200;">=</span> aSuit;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> valueAsString <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span>self.value<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">case</span> Ace<span style="color: #002200;">:</span>
			<span style="color: #a61390;">return</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ace&quot;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">case</span> Jack<span style="color: #002200;">:</span>
			<span style="color: #a61390;">return</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Jack&quot;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">case</span> Queen<span style="color: #002200;">:</span>
			<span style="color: #a61390;">return</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Queen&quot;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">case</span> King<span style="color: #002200;">:</span>
			<span style="color: #a61390;">return</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;King&quot;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">default</span><span style="color: #002200;">:</span>
			<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%d&quot;</span>,self.value<span style="color: #002200;">&#93;</span>;
			<span style="color: #a61390;">break</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> suitAsString <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span>self.suit<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">case</span> Hearts<span style="color: #002200;">:</span>
			<span style="color: #a61390;">return</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hearts&quot;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">case</span> Diamonds<span style="color: #002200;">:</span>
			<span style="color: #a61390;">return</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Diamonds&quot;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">case</span> Spades<span style="color: #002200;">:</span>
			<span style="color: #a61390;">return</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Spades&quot;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">case</span> Clubs<span style="color: #002200;">:</span>
			<span style="color: #a61390;">return</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Clubs&quot;</span>;
			<span style="color: #a61390;">break</span>;
		<span style="color: #a61390;">default</span><span style="color: #002200;">:</span>
			<span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
			<span style="color: #a61390;">break</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> description <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ of %@&quot;</span>,
			<span style="color: #002200;">&#91;</span>self valueAsString<span style="color: #002200;">&#93;</span>,
			<span style="color: #002200;">&#91;</span>self suitAsString<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Wow, so this looks like a lot of overkill.  The truth is, you only need the init method that I created.  The description method allows us to overwrite the printing method for a card and gives us pretty output.  The other 2 methods are simply help methods for it (we have declared them as private at the top).  You can choose to omit them if you like, but make debugging a lot easier.  When you do something like NSLog(@&#8221;%@&#8221;,myCard), it will print &#8220;Ace of Spades&#8221;.  Great, now you have a Card object to work with.  Now we just need to create 52 of these puppies and we are on our way to hundreds of card games.</p>
<h2>Creating The Deck</h2>
<p>The deck of cards is fairly straight forward.  First off, we need an array of cards.  The card needs to have 3 methods implemented for correct functionality.  They are draw, shuffle, and cardsRemaining.  The reason the cardsRemaining method is needed is to prevent users from trying to draw cards from an empty deck (we will discuss this in a bit).  Add a new class file to your project called <strong>Deck.m </strong>and add the following code to Deck.h:</p>
<h3>Deck.h</h3>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Card.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Deck <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #a61390;">@private</span>
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>cards;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> shuffle;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>Card <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> draw;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> cardsRemaining;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>A few things I want to point out.  First, we import Card.h.  Normally we would import the models in the .m file if they are not needed in the header.  However, we need to declare the draw method which returns a Card object.  So, we include the import statement here.</p>
<p>The next thing I want to point out is we have marked the cards array as private.  We don&#8217;t want anyone mucking around in our deck without going through our methods.  The reason for this is to prevent synchronization issues where someone modifies the deck without notifying the class.  Kinda trivial, but good practice non the less.  Now for the implementation.  Add the following code to Deck.m.</p>
<h3>Deck.m</h3>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Deck.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Deck
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> init <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		cards <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> suit <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; suit <span style="color: #002200;">&amp;</span>lt;<span style="color: #002200;">=</span> <span style="color: #2400d9;">3</span>; suit<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> value <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>; value <span style="color: #002200;">&amp;</span>lt;<span style="color: #002200;">=</span> <span style="color: #2400d9;">13</span>; value<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
				Card <span style="color: #002200;">*</span>card <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Card alloc<span style="color: #002200;">&#93;</span> initWithValue<span style="color: #002200;">:</span>value suit<span style="color: #002200;">:</span>suit<span style="color: #002200;">&#93;</span>;
				<span style="color: #002200;">&#91;</span>cards addObject<span style="color: #002200;">:</span>card<span style="color: #002200;">&#93;</span>;
				<span style="color: #002200;">&#91;</span>card release<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#125;</span>
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*
 * Random sort used from this blog post
 * http://zaldzbugz.wordpress.com/2010/07/16/randomly-sort-nsarray/
 */</span>
<span style="color: #a61390;">int</span> randomSort<span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> obj1, <span style="color: #a61390;">id</span> obj2, <span style="color: #a61390;">void</span> <span style="color: #002200;">*</span>context <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// returns random number -1 0 1</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#40;</span>arc4random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>3 <span style="color: #002200;">-</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> shuffle <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; x <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">500</span>; x<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>cards sortUsingFunction<span style="color: #002200;">:</span>randomSort context<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span> 
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>Card <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> draw <span style="color: #002200;">&#123;</span>
 	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self cardsRemaining<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		Card <span style="color: #002200;">*</span>card <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>cards lastObject<span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>cards removeLastObject<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>card autorelease<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSException</span><span style="color: #002200;">*</span> myException <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSException</span>
		exceptionWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;OutOfCardsException&quot;</span>
		reason<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tried to draw a card from a deck with 0 cards.&quot;</span>
		userInfo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">@throw</span> myException;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span> cardsRemaining <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>cards count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> description <span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>desc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Deck with %d cards<span style="color: #2400d9;">\n</span>&quot;</span>,<span style="color: #002200;">&#91;</span>self cardsRemaining<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; x <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>self cardsRemaining<span style="color: #002200;">&#93;</span>; x<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		desc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>desc stringByAppendingFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@<span style="color: #2400d9;">\n</span>&quot;</span>,<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>cards objectAtIndex<span style="color: #002200;">:</span>x<span style="color: #002200;">&#93;</span> description<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">return</span> desc;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> dealloc <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>cards release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Ok, this file needs a little more explanation.  First, we see the init method has been implemented.  There are 2 for loops.  The outer loop is from 0 to 3 representing the suit of each card.  Basically, we want to create 13 cards for each of the 4 suits.  The inner loop is from 1 to 13 representing the card&#8217;s value.  We simply instantiate a Card object with the suit and value and add it to the cards array. Let&#8217;s chat about each of the methods.</p>
<p><strong>Shuffle</strong></p>
<p>The shuffle method simply sorts the array based on a random number.  If we do it once, the deck will barely be shuffled, which makes sense.  So, I have randomly sorted the array 500 times to ensure that it has been effectively shuffled.  Note: I stole the randomSort method from http://zaldzbugz.wordpress.com/2010/07/16/randomly-sort-nsarray/ based on a quick/lazy Google search, but it&#8217;s a pretty common way of accomplishing this task.</p>
<p><strong>Draw</strong></p>
<p>We first check to see if the deck is empty. If it&#8217;s not, we retain the last object, remove it from the array and return an auto release of it.  It will be up to the caller to retain the Card.  Here is something you don&#8217;t see everyday, we are throwing an exception if the user tries to draw from an empty deck.  Kind of a jerk move, but it will ensure that they check the cardsRemaining before performing a draw.  Again, not totally necessary, but good practice.</p>
<p><strong>cardsRemaining</strong></p>
<p>Returns the length of the cards array  I override the description method in this class to print out the deck in its entirety.  Again, this is very useful for debugging.  Finally, we rock the memory management and release our card deck array when our deck gets cleaned up.</p>
<h2>Sample Run/Output</h2>
<p>Here is a simple example of how to create a deck, shuffle, and draw.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">        Deck <span style="color: #002200;">*</span>d <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Deck alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>,d<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>d shuffle<span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>,d<span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Drew Card: %@&quot;</span>,<span style="color: #002200;">&#91;</span>d draw<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Drew Card: %@&quot;</span>,<span style="color: #002200;">&#91;</span>d draw<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Drew Card: %@&quot;</span>,<span style="color: #002200;">&#91;</span>d draw<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Drew Card: %@&quot;</span>,<span style="color: #002200;">&#91;</span>d draw<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>,d<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>d release<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The output is a little long, so I&#8217;ll just past some snippets of it.  You can run this for yourself to see the full output.</p>
<pre>Deck with 52 cards
Ace of Hearts
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Diamonds
2 of Diamonds
3 of Diamonds
...
(After shuffle)
Deck with 52 cards
King of Diamonds
6 of Hearts
5 of Spades
9 of Clubs
2 of Diamonds
8 of Clubs
7 of Hearts
Ace of Clubs
10 of Diamonds
Jack of Diamonds
8 of Spades
6 of Diamonds
Ace of Spades
3 of Spades
...
2010-09-07 14:56:37.953 ICBBlackJack[5465:207] Drew Card: Queen of Spades
2010-09-07 14:56:37.953 ICBBlackJack[5465:207] Drew Card: 9 of Spades
2010-09-07 14:56:37.954 ICBBlackJack[5465:207] Drew Card: 9 of Hearts
2010-09-07 14:56:37.955 ICBBlackJack[5465:207] Drew Card: Queen of Clubs

Deck with 48 cards
King of Diamonds
6 of Hearts
5 of Spades
9 of Clubs
2 of Diamonds
8 of Clubs
7 of Hearts
Ace of Clubs
10 of Diamonds
Jack of Diamonds
8 of Spades
6 of Diamonds
Ace of Spades</pre>
<p>Note that drawing a card pulls from the end of the array.  It doesn&#8217;t matter if you pull from the front or the back, just make sure it&#8217;s consistent.</p>
<h2>Conclusion</h2>
<p><a href="/wp-content/uploads/2010/09/ICBBlackJack.zip">Click Here To Download The Code For This Tutorial</a></p>
<p>And there you have it! A fully functional deck of cards.  Please be sure to join me next time when we will start implementing the dealer and some basic Blackjack logic.  <a href="/wp-content/uploads/2010/09/ICBBlackJack.zip">Click Here To Download The Code For This Tutorial</a> Feel free to post questions in the comments section or <a href="http://twitter.com/brandontreb">@reply them to me on Twitter</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/09/09/iphone-game-programming-series-blackjack-part-1-the-deck/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
