onstwedder.com

All that you have is your soul

7 notes &

Cocoa coding: Blackjack tutorial - part 3

So here’s the 3rd part of the tutorial - and boy does it takes more time than planned.

Basically what I have from the first three parts (including part 0, that is) of the tutorial is now a Deck with playing cards, that can be initialized and print itself to the NSLog to prove it is somewhat working.

Hence we’ll now add the implementation of the shuffle and the drawCard methods. 

The drawCard will draw the card from the deck. It is not going to draw a card on the screen. If you’re waiting for graphics in this tutorial, there will be some. But not  just yet. 

The shuffle might not look like it’s an interesting part of the tutorial, but what would you know, it actually is. It turns out there are quite a few shuffling algorithms around and some of them were just wrong. Now for our blackjack it might not actually be that important how correct the shuffling is done, but there are plenty of stories on the internet, that showed that the randomizing of/shuffling cards in quite a few online casinos were not that random. They fixed it now (probably). But if you’re cousin created an online casino a couple of weeks ago, you should check it for the randomizing and the shuffle algorithm.

Read about that (the real online casino, not the one of your cousin) here: internet gaming flaw and the error in some shuffling 

So after reading those you will now know, you’ll need the Fisher–Yates shuffle.

And here is the implementation of shuffle in Deck.m:

-(void)shuffle{	
		NSUInteger count = [cards count];
		for (NSUInteger i = 0; i < count; ++i) {
			// Select a random element between i and end of array to swap with.
			int nElements = count - i;
			int n = (arc4random() % nElements) + i;
			[cards exchangeObjectAtIndex:i withObjectAtIndex:n];
		}
}

The arc4random() is a pseudo-random generator that does it’s own seeding (That’s nerd talk when you talk about random numbers, in case you’re puzzled).

The other stuff in shuffle is the actual Fisher-Yates shuffle.

Then there is the drawCard method. After you have shuffled the deck, you’ll need to draw a card from the deck. You’ll find the code you’ll need after reading about NSMutableArray in the Class Reference. (In Xcode under the menu Help / Developer Documentation). Actually you have to look at NSArray, cause it inherits the needed method from there.

In the documentation it says under a special header “Querying an Array”, what options you have to get data from the array. Getting a card can be done by either objectAtIndex: or lastObject. 

So looking at the options the objectAtIndex: will raise an error if you look for an object at an index where there is nothing in the array. lastObject will return nil if there is no object anymore. 

Once you realize, there is no real reason why you would prefer to pick cards from the top of the array over picking cards from the bottom of the array. You too will settle for lastObject.

The code for drawing a card will then be:

-(Card *) drawCard{
	if ([cards count] > 0)
	{
		Card *tCard = [cards lastObject];
		[cards removeLastObject];
		
		return tCard;
	}
	return nil;
}

The if statement is there to not call removeLastObject if there are no objects.

By modifying the code in tutorial21AppDelegate.m (that was right under the //insert code line ) to this:

	// Insert code here to initialize your application 
	Deck* deck = [[Deck alloc] init];
	[deck shuffle];
	NSLog(@"%@",[deck description]);
	Card* tcard = [deck drawCard];
	NSLog(@"%@",[tcard description]);

The program will log how the shuffled Deck looks, and what card was drawn. If you now build and run again, you can see it in your Debug Console.

If you add an extra line

NSLog(@"%@",[deck description]); 

after the NSLog that prints the card info, you can see when you build and run that the last card from the deck is now removed from the deck.

So drawing a card works. And shuffling works.

And that’s the end of part 3.

*This is part of a objective-C, cocoa tutorial, that will result in a working blackjack for a os x*.

Previous chapter: blackjack tutorial - part 2
Next chapter: blackjack tutorial - part 4

Filed under objective-c blackjack coding programming mac osx Cocoa Tutorial Xcode

  1. quantummao posted this