<?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>iPhone Programming Tutorials &#187; iphone game</title>
	<atom:link href="/tag/iphone-game/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Tue, 19 Nov 2013 19:34:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.1.1</generator>
	<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><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Advanced]]></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[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  ...]]></description>
				<content:encoded><![CDATA[<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"><table><tr><td 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></td></tr></table></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"><table><tr><td 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></td></tr></table></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"><table><tr><td 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></td></tr></table></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"><table><tr><td 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></td></tr></table></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"><table><tr><td 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></td></tr></table></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/ICBBlackJack1.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/ICBBlackJack1.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>40</slash:comments>
		</item>
		<item>
		<title>iPhone Coding Tutorial &#8211; Creating an Online Leaderboard For Your Games</title>
		<link>http://icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/</link>
		<comments>http://icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 16:14:50 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone game]]></category>
		<category><![CDATA[leaderboard]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1426</guid>
		<description><![CDATA[As you may have seen, there are quite a few services out there offering free leaderboards.  These are great and all, but sometimes you want to have complete control over your data.  I have put together a complete tutorial detailing step by step how you can create your own online leaderboard.  This will also give you a very simple introduction to interfacing with web services.]]></description>
				<content:encoded><![CDATA[<p>As you may have seen, there are quite a few services out there offering free leaderboards.  These are great and all, but sometimes you want to have complete control over your data.  I have put together a complete tutorial detailing step by step how you can create your own online leaderboard for use in your iPhone games.  For those of you who don&#8217;t know, a leaderboard is essentially a high scores list.  This tutorial will also give you a very simple introduction to interfacing with web services.</p>
<p>The first part of this tutorial will discuss how to build the server.  We will be doing this using PHP and MYSQL.  I will also assume you have some working knowledge of PHP and MYSQL (if not, use the Google machine to get up to speed).</p>
<p>Since this tutorial is so long, I have decided to split it up into pages.  You will notice the page number at the bottom.  Please use them to navigate between parts of this post.  I feel that I have to make this explicit as I will undoubtably get some dude in the comments saying &#8220;Where is the rest of the tutorial&#8221;.</p>
<p>Skill level: <font color="red">Advanced</font></p>
<h2>Creating The Database</h2>
<p>When creating a leaderboard, you will need some way of storing the data.  We will be using MYSQL for our storage.  You can also be lame and simply use flat files.  This would add quite a bit of code in the long run as you would have to write all of the sorting and pagination functionality yourself. Don&#8217;t do it.</p>
<p>One thing to note about my php server files.  I know they could be cleaned up a little and optimized (you could create a config.php that contains all the db interface code), but the goal of this tutorial is not to show you how to code killer PHP.  It&#8217;s to show you how to create code that you can connect your iPhone apps to.</p>
<p>I like to create one file that does a complete flash of the database.  That way, when I&#8217;m testing or switch from staging to production, it is a very simple process.  So, with that being said, here is my code for create_db.php.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
        <span style="color: #666666; font-style: italic;">// create_db.php</span>
	<span style="color: #009933; font-style: italic;">/** MySQL database name */</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_NAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009933; font-style: italic;">/** MySQL database username */</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_USER'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009933; font-style: italic;">/** MySQL database password */</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_PASSWORD'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009933; font-style: italic;">/** MySQL hostname */</span>
	<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DB_HOST'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_ENV</span><span style="color: #009900;">&#123;</span>DATABASE_SERVER<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$table</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;highscores&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Initialization</span>
	<span style="color: #000088;">$conn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span>DB_HOST<span style="color: #339933;">,</span>DB_USER<span style="color: #339933;">,</span>DB_PASSWORD<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span>DB_NAME<span style="color: #339933;">,</span> <span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Error checking</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Could not connect '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Drop existing table if exists</span>
	<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DROP TABLE <span style="color: #006699; font-weight: bold;">$table</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$retval</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;CREATE TABLE <span style="color: #006699; font-weight: bold;">$table</span>(
		id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
		udid VARCHAR(45),
		name VARCHAR(25),
		score FLOAT(10,2),
		date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
	)&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$retval</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Database created...&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Could not create database &quot;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>I&#8217;m not going to go into too much detail about this code, however I will give a high level description of what&#8217;s going on.  One thing to note about this code is it assumes you already have the database created.</p>
<p>First, we are defining variables that contain our database information.  Make sure you replace my empty strings with your database info. After we connect to the database, we drop the table if it already exists.  This is useful if you want to wipe out your test data.  Finally, we create the scores table.  Navigate to this file in your browser to run it and create the scores table.  Pretty easy right?</p>
<p><font color="red">You will want to make sure to remove this file from your server after running it to avoid people resetting your database.</font></p>
<p>Now that our database table has been created, it&#8217;s time to implement the web service code to publish new scores to our leaderboard.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/10/29/iphone-coding-tutorial-creating-an-online-leaderboard-for-your-games/feed/</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
		<item>
		<title>Look Familiar?</title>
		<link>http://icodeblog.com/2009/07/08/look-familiar/</link>
		<comments>http://icodeblog.com/2009/07/08/look-familiar/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 17:53:53 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[iphone game]]></category>
		<category><![CDATA[moron]]></category>
		<category><![CDATA[thief]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1076</guid>
		<description><![CDATA[Ok, so I was perusing the App Store the other day to stumble upon this gem of an application. See the screenshot below.
<a href="/wp-content/uploads/2009/07/screenshot_015.png"></a>
If you have been on iCodeBlog before, you might recognize this &#8220;game&#8221; from a tutorial that I wrote about writing an iPhone game (<a href="/2009/01/15/iphone-game-programming-tutorial-part-1/">link</a><a></a>).  One thing to note here is the company name is BlaBlaIncTech (not iCodeBlog) meaning I did not submit this nor did I give permission to do so.  I find it  ...]]></description>
				<content:encoded><![CDATA[<p>Ok, so I was perusing the App Store the other day to stumble upon this gem of an application. See the screenshot below.</p>
<p><a href="/wp-content/uploads/2009/07/screenshot_015.png"><img class="aligncenter size-full wp-image-1077" title="screenshot_01" src="/wp-content/uploads/2009/07/screenshot_015.png" alt="screenshot_01" width="600" /></a></p>
<p>If you have been on iCodeBlog before, you might recognize this &#8220;game&#8221; from a tutorial that I wrote about writing an iPhone game (<a href="/2009/01/15/iphone-game-programming-tutorial-part-1/">link</a><a></a>).  One thing to note here is the company name is BlaBlaIncTech (not iCodeBlog) meaning I did not submit this nor did I give permission to do so.  I find it interesting that someone has the guts to take a tutorial on iCodeBlog and submit it to the app store <strong>unmodified</strong>.</p>
<p>Not only was this VERY basic code meant to teach simple concepts, the graphics are absolutely atrocious as they were hacked together very quickly in Photoshop.  I actually find this quite hilarious that this guys expects me not to find out.</p>
<p>I wanted to highlight some of the great features noted by the &#8220;developer&#8221; of this application (Seen in the feature section of iTunes).</p>
<ul>
<li>&#8220;It&#8217;s basic game design makes it a fun game for someone that doesn&#8217;t play alot of games&#8221; &#8211; WOW, emphasis on the BASIC.  This game is fun for no one as it sucks! It was meant as a teaching tool moron!</li>
<li>&#8220;Great Quality&#8221; &#8211; Are we looking at the same game?</li>
<li>&#8220;Easy (So easy your grandma can play)&#8221; &#8211; If you have the same taste in games as your grandma, you probably don&#8217;t own an iPhone.</li>
<li>&#8220;<strong>Almost no crashes</strong>&#8221; &#8211; hahahaha this is the best one. The keyword here is &#8220;Almost&#8221;.  Well, that does it, I&#8217;m sold!</li>
</ul>
<p>So, if you want to scoop up this rad game, the link is <a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=317420248&amp;mt=8">here</a> . I almost will pay him the $.99, just to make fun of him in the comments section.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/07/08/look-familiar/feed/</wfw:commentRss>
		<slash:comments>100</slash:comments>
		</item>
	</channel>
</rss>
