Presentation is loading. Please wait.

Presentation is loading. Please wait.

Black Jack in Objective-C

Similar presentations


Presentation on theme: "Black Jack in Objective-C"— Presentation transcript:

1 Black Jack in Objective-C
CS 355

2 Cards 52 cards in a standard poker deck Each card has a suit
heart  spade  diamond  club  and a rank A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K

3 Card Interface (Card.h)
#import <Foundation/Foundation.h> enum {HEART, SPADE, CLUB, DIAMOND}; enum {ACE=1, JACK=11, QUEEN=12, KING=13}; @interface Card : NSObject { @private int suit; // HEART, SPADE, DIAMOND, CLUB int rank; // ACE, 2, 3, …, 10, JACK, QUEEN, KING } -(id)init; -(id)initWithSuit:(int)s AndRank:(int)r; -(int) suit; -(int) rank; @end

4 Card Implementation (Card.m)
#import "Card.h" @implementation Card -(id)init {return [self initWithSuit: SPADE AndRank: ACE];} -(id)initWithSuit:(int)s AndRank:(int)r { if ((self = [super init]) != nil) { suit = s; rank = r; } return self; -(int)suit {return suit;} -(int)rank {return rank;} @end

5 Designated Initializer
-(id)initWithSuit:(int)s AndRank:(int)r { if ((self = [super init]) != nil) { suit = s; rank = r; } return self; Most specialized (has most parameters) Called by other initializers Proper chaining of initializers: First call parent class’s designated initializer, then initialize new instance variables

6 Accessors Note that instance variables are private.
-(int)suit {return suit;} -(int)rank {return rank;} Note that instance variables are private. A card’s suit and rank are retrieved via getter methods Usually have the same name as instance variable. Note that Card’s are immutable since we do not provide any setter methods.

7 Creating a new card id card = [[Card alloc] init]; We first send an alloc message to the Card class object which creates a new instance. Then we send an init message to this newly created instance. It is important to capture the return value of the init method call, since it may return a different object then was created via alloc. The newly created card has a reference count of 1.

8 Black Jack Shoe In blackjack, a shoe is a device that can hold one to eight decks of cards from which the dealer can draw one card at time. When the shoe becomes sufficiently low, it is restocked and reshuffled.

9 Shoe.h #import <Foundation/Foundation.h>
@interface Shoe : NSObject { @private int numDecks; id cards; // mutable array } -(id)initWithDeckCount:(int)n; -(id)init; -(void)dealloc; -(void)shuffle; -(id)deal; -(int)cardsLeft; -(void)reload; @end

10 Shoe initialization (id)initWithDeckCount:(int)n {
if ((self = [super init]) != nil) { numDecks = n; cards = [[NSMutableArray alloc] initWithCapacity: n*52]; [self reload]; } return self;

11 Shoe destruction Release cards array.
(void)dealloc { [cards release]; [super dealloc]; } Release cards array. Array releases each card Ends with call to superclass’s dealloc method.

12 Dealing cards from Shoe
-(id)deal { if ([cards count] <= 0) return nil id card = [[cards lastObject] retain]; [cards removeLastObject]; return [card autorelease]; } Note that we retain the last card before we release it! We autorelease the card to safely perform the hand-off back to the caller!

13 Shuffling the Shoe -(void)shuffle { const int n = [cards count];
for (int i = 0; i < n; i++) { int j = (int) (drand48()*n); if (i != j) [cards exchangeObjectAtIndex: i withObjectAtIndex: j]; }

14 Hand A hand is the set of cards held by a player.
A hand is valued by the sum of its cards: Aces worth 1 or 11 Face cards (Jack, Queen, King) are worth 10. A blackjack is a hand containing exactly two cards worth 21.

15 Hand.h #import <Foundation/Foundation.h> #import "card.h"
@interface Hand : NSObject { @private id cards; } -(id)init; -(void)dealloc; -(void)empty; -(void)insertCard:(Card *)card; -(unsigned)numCards; -(unsigned)softCount; -(unsigned)hardCount; -(unsigned)count; -(BOOL)busted; -(BOOL)blackjack; -(NSEnumerator *)cardEnumerator; @end

16 Hand init and dealloc -(id)init { self = [super init];
if (self != nil) { cards = [[NSMutableArray alloc] initWithCapacity: 10]; } return self; -(void)dealloc { [cards release]; [super dealloc];

17 Inserting cards into and emptying a hand
-(void)insertCard:(Card *)card { [cards addObject: card]; } -(void)empty { [cards removeAllObjects]; -(unsigned)numCards { return [cards count];

18 Soft Count Aces = 1 -(unsigned)softCount {
NSEnumerator *e = [cards objectEnumerator]; id card; unsigned c = 0; while ((card = [e nextObject]) != nil) { int r = [card rank]; c += (r <= 10) ? r : 10; } return c;

19 Hard Count Aces = 11 -(unsigned)hardCount {
NSEnumerator *e = [cards objectEnumerator]; id card; unsigned c = 0; while ((card = [e nextObject]) != nil) { int r = [card rank]; if (r == ACE) c += 11; else if (r <= 10) c += r; else // face card c += 10; } return c;

20 Count closest to 21 without busting
-(unsigned)count { NSEnumerator *e = [cards objectEnumerator]; id card; unsigned c = 0; unsigned numAces = 0; while ((card = [e nextObject]) != nil) { int r = [card rank]; if (r == ACE) { c += 11; numAces++; } else c += (r <= 10) ? r : 10; } while (c > 21 && numAces > 0) { c -= 10; numAces--; return c;

21 Busted or Blackjack? -(BOOL)busted { return [self softCount] > 21;
} -(BOOL)blackjack { return [cards count] == 2 && [self hardCount] == 21;

22 Plays by a fixed set of rules
Player Hierarchy NSObject Player abstract base class Dealer Plays by a fixed set of rules Human Plays using human I/O Cyborg Plays using AI

23 Player Base Class typedef enum {HIT, STAY} Action;
@interface Player : NSObject { @protected int bank; id hand; } -(id)initWithBank:(int)amount; -(void)dealloc; -(int)betAmount; -(int)bank; -(void)bankCredit:(int)amount; -(void)bankDebit:(int)amount; -(Action)action; -(void)insertCard:(Card *)card; -(int)score; -(BOOL)busted; -(BOOL)blackjack; -(void)empty; -(NSEnumerator *) cardEnumerator; @end

24 Player init and dealloc
-(id)initWithBank:(int)amount { self = [super init]; if (self != nil) { bank = amount; hand = [[Hand alloc] init]; } return self; -(void)dealloc { [hand release]; [super dealloc];

25 Default player strategy
-(int)betAmount {return bank;} // bet it all! -(Action)action { // mimic dealer return ([hand hardCount] < 17) ? HIT : STAY; }

26 Other default behavior
-(int)bank {return bank;} -(void)bankCredit:(int)amount {bank += amount;} -(void)bankDebit:(int)amount {bank -= amount;} -(void)insertCard:(Card *)card { [hand insertCard: card]; } -(int)score {return [hand count];} -(BOOL)busted {return [hand busted];} -(BOOL)blackjack {return [hand blackjack];} -(void)reset {[hand empty];} -(NSEnumerator *) cardEnumerator { return [hand cardEnumerator];

27 The Dealer @interface Dealer : Player { @protected id shoe; }
-(id)initWithDeckCount:(int)num; -(id)deal:(id)player; -(int)cardsLeft; -(void)reload; -(void)shuffle; @end

28 Dealer init and dealloc
-(id)initWithDeckCount:(int)num { self = [super initWithBank: 0]; // has no bank if (self != nil) { shoe = [[Shoe alloc] initWithDeckCount: num]; } return self; -(void)dealloc { [shoe release]; [super dealloc];

29 Dealer methods -(id)deal:(id)player { id card = [shoe draw]; if (player != nil) // player == nil => burn card [player insertCard: card]; return card; } -(int)cardsLeft {return [shoe cardsLeft];} -(void)restock {[shoe restock];} -(void)shuffle {[shoe shuffle];} // inherits action strategy (and other behavior) from Player

30 Table Dealer Is a player Has shoe 1 to 7 Players

31 Table.m Dealer and 1 to 7 Players
@interface Table : NSObject { @private id dealer; id players; // mutable array } -(id)initWithDeckCount:(int)num; -(void)dealloc; -(int)numPlayers; -(void)addPlayer:(id)player; -(void)removePlayer:(id)player; -(id)dealer; -(NSEnumerator *)playerEnumerator; @end

32 Cyborg.h An Example Player AI
@interface Cyborg : Player { // no new instance vars } -(int)betAmount; -(Action)action; @end

33 Cyborg method overrides
-(int)betAmount { int amount = (int) (50*drand48()); if (amount > bank) amount = bank; return amount; } -(Action)action { return ([hand softCount] < 18) ? HIT : STAY;

34 Human.h Human Player @interface Human : Player {
// no new instance vars } -(int)betAmount; // get from user -(Action)action; // get from user @end


Download ppt "Black Jack in Objective-C"

Similar presentations


Ads by Google