onstwedder.com

All that you have is your soul

2 notes &

Cocoa coding: Blackjack tutorial - part 7

Looking at what I have now: a couple of classes for the Hand, Deck and Card. Then there is the BlackJackView and the BlackJackController and of course the tutorial21AppDelegate.

So let’s pretend I know what the Model-View-Controller (MVC) model is, and since i see a View and a Controller, the only thing missing is the Model. So let’s create a BlackJackModel file. If nothing else, it will look really smart.

Our model will have to know all about Blackjack. The model needs to be aware there are two hands: a playerhand and a tablehand. Also it has to be aware of the deck (of cards). And It has be smart enough to setup a game.

So, add a new class to the files, and choose it’s template to be “Objective-C class”. Be sure to make it a subclass of NSObject.

#import <Cocoa/Cocoa.h>
@class Deck;
@class Hand;


@interface BlackJackModel : NSObject {
    Deck *deck;
    Hand *playerHand;
    Hand *tableHand;
}

@property (assign) Deck *deck;
@property (retain) Hand *playerHand;
@property (retain) Hand *tableHand;

-(void) setupGame;

@end

The implementation of this class will be fairly simple for now. During the init, let’s add a deck, and two empty hands. In the SetupGame I’ll think of something to actually setup a game eventually, but let’s just shuffle the deck for now.

#import "BlackJackModel.h"
#import "Deck.h"
#import "Hand.h"
#import "Card.h"

@implementation BlackJackModel
@synthesize deck, tableHand, playerHand;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        self.deck = [[Deck alloc] init];
        self.tableHand = [[Hand alloc] init];
        self.playerHand =[[Hand alloc] init];
    }
    
    return self;
}

-(void) setupGame{
	[self.deck shuffle];
}


- (void)dealloc
{
    [super dealloc];
}

@end

Now the BlackJackController will need to know that there is a BlackJackModel - So I’ll add a property there. Also the BlackJackController will have to act when someone clicks the startButton in it the BlackJackView. So I’ll add an IBAction for that. Of course in the end the other buttons are also needed, but for now. Just the start will do.

In the startbutton action of the BlackJackController I’ll initialize the blackJackModel.

-(IBAction) startGame:(id)sender{
    blackJackModel = [[BlackJackModel alloc] init]; 
}

First let’s add the startbutton’s action to the BlackJackControllers’ IBAction we just made. Open up the .xib file and click on the start button. There open up the connection inspector. And connect the Sent Actions|selector to the file’s owner|startGame.

You can add a breakpoint on the blackJackModel’s init and see if it get’s there when you run the application.

So that seems to work. Let’s also call setupGame from the startGame method. However the setupGame only shuffles the deck. Let’s also deal the first 2 cards for the player and the first 2 cards for the table. I’ll deal them the following: player (open), table (open), player (open) and table (closed).

So now I have a slight problem: I want to set the open status of a card when I deal it into a hand. That’s why I’ve added a new method to Hand.

-(void) addCard:(Card *)card open:(BOOL)aBool;

And the implementation of this is:

-(void) addCard:(Card *)card open:(BOOL)aBool
{
    card.open = aBool;
    [self addCard:card];
}

Now it kinda looks bad to do it this way, but sue me. ( That’s a joke: don’t sue me. Instead leave a comment how to improve this, without making a new subclass)

With this new method, I can now do the following in the setupGame method in the blackJackModel.

-(void) setupGame{
	[self.deck shuffle];
    
    [self.playerHand addCard:[self.deck drawCard] open:YES];
    [self.tableHand addCard:[self.deck drawCard] open:YES];
    [self.playerHand addCard:[self.deck drawCard] open:YES];
    [self.tableHand addCard:[self.deck drawCard] open:NO];
    
}

Now that this is working, it feels I am almost there. Though you might panic and wonder why you’re still not seeing any cards.

I think that’s going to be added next (tomorrow).

Previous chapter: Blackjack tutorial - part 6.
Next chapter: BlackJack tutorial - part 8.

Filed under objective-c blackjack osx Cocoa Tutorial Xcode

  1. quantummao posted this