我无法显示手中已抽出的牌。我可以看到文本形式的卡片,"All Output"
但它们没有直观地显示。牌名的一个例子是"club04"
, "spades06"
,"heart13"
等。11-13 代表 Jacks to Kings。
这是一部分ViewController.m
:
-(void) showHand:(Hand *)hand {
int ypos = 40;
for (int i = 0; i < [hand countCards]; i++) {
Card *card = [hand getCardAtIndex:i];
// UIImage *cardImage = [UIImage imageNamed:@"heart08.png"];
UIImage *cardImage = [UIImage imageNamed:[card filename]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:cardImage];
CGRect arect = CGRectMake((i * 40) + 20, ypos, 71, 96);
imageView.frame = arect;
[self.view addSubview:imageView];
}
}
@end
这是Hand.h
#import <Foundation/Foundation.h>
@class Card;
@interface Hand : NSObject {
}
@property NSMutableArray *cardsInHand;
-(void) addCard:(Card *)card;
-(NSInteger) getPipValue;
-(NSInteger) countCards;
-(Card *) getCardAtIndex:(NSInteger) index;
@end
这是Hand.m
#import "Hand.h"
#import "Card.h"
@implementation Hand
- (id) init {
if (self = [super init]){
self.cardsInHand = [[NSMutableArray alloc] initWithCapacity:6];
}
return (self);
}
- (NSInteger)countCards {
return ([self.cardsInHand count]);
}
- (void)addCard:(Card *)card {
[self.cardsInHand addObject:card];
}
- (NSInteger)getPipValue {
NSInteger aValue = 0;
for (Card *tCard in self.cardsInHand) {
aValue = aValue + [tCard pipValue];
}
return aValue;
}
- (Card *)getCardAtIndex:(NSInteger)index {
return ([self.cardsInHand objectAtIndex:index]);
}
- (NSString *)description {
return [NSString stringWithFormat:@"cards in hand : %@", self.cardsInHand];
}
@end
有趣的是,当我手动输入一张卡片时,我会看到卡片。我只看到同一张卡片的 6 次。
但"All Output"
显示我需要看到的。例子:
2013-04-21 19:18:41.025 blackjack[1395:c07] Dealer:cards in hand : (
"Club 5 (pipValue = 0)",
"Spade Jack (pipValue = 1)",
"Club 8 (pipValue = 0)",
"Spade Queen (pipValue = 1)",
"Diamond King (pipValue = 1)",
"Heart 7 (pipValue = 0)"
)
这是将牌放在手中和桌子上的部分:
-(id) init {
if ((self = [super init])){
_deck = [[Deck alloc] init];
_playerHand = [[Hand alloc] init];
_dealerHand = [[Hand alloc] init];
_table = [[Table alloc] init];
}
return (self);
}
-(void)setup
{
//deal cards
[_playerHand addCard:[_deck drawCard]];
[_dealerHand addCard:[_deck drawCard]];
[_playerHand addCard:[_deck drawCard]];
[_dealerHand addCard:[_deck drawCard]];
[_playerHand addCard:[_deck drawCard]];
[_dealerHand addCard:[_deck drawCard]];
[_playerHand addCard:[_deck drawCard]];
[_dealerHand addCard:[_deck drawCard]];
[_playerHand addCard:[_deck drawCard]];
[_dealerHand addCard:[_deck drawCard]];
[_playerHand addCard:[_deck drawCard]];
[_dealerHand addCard:[_deck drawCard]];
[_table addCard:[_deck drawCard]];
[_table addCard:[_deck drawCard]];
[_table addCard:[_deck drawCard]];
[_table addCard:[_deck drawCard]];
这是card.m
:
#import "Card.h"
@implementation Card
@synthesize suit = _suit, cardNumber=_cardNumber;
-(id) initWithCardNumber:(NSInteger)aCardNumber suit:(Suit)aSuit {
if (self = [super init])
{
_suit = aSuit;
_cardNumber = aCardNumber;
}
return self;
}
/*
** pipValue will return the pipValue of a card.
** 2 to 9 will return 0.
** an ace will always return 1
** Jack, Queen and King will always return 1.
** 2 of Clubs will return 1
** 10 of diamonds will return 2
*/
-(NSInteger) pipValue {
if (_suit==Diamonds && _cardNumber==10)
return (2);
else if (_cardNumber>=10)
return (1);
else if (_cardNumber==1)
return (1);
else if (_suit==Clubs && _cardNumber==2)
return (1);
else
return (0);
}
/*
** private method
** returns the suit as a String
*/
-(NSString *) suitAsString{
switch (_suit) {
case Hearts:
return @"Heart ";
break;
case Spades:
return @"Spade ";
break;
case Diamonds:
return @"Diamond ";
break;
case Clubs:
return @"Club ";
break;
default:
return nil;
break;
}
}
/*
** private method
** returns the cardNumber as a String
*/
-(NSString *) cardNumberAsString{
switch (_cardNumber) {
case 1:
return @"Ace";
break;
case 11:
return @"Jack";
break;
case 12:
return @"Queen";
break;
case 13:
return @"King";
break;
default:
return [NSString stringWithFormat:@"%d", _cardNumber];
break;
}
}
-(NSString *) filename{
return ([NSString stringWithFormat:@"%@%02d.png", [self suitAsString], [self cardNumber]]);
}
-(NSString *) description {
return [NSString stringWithFormat:@"%@ %@ (pipValue = %d)", [self suitAsString], [self cardNumberAsString], [self pipValue]];
}
@end
还有deck.m
:
#import "Deck.h"
#import "Card.h"
@implementation Deck
-(id) init {
if (self = [super init])
{
cards = [[NSMutableArray alloc] init];
for (int suit = 0; suit <= 3; suit++)
{
for (int cardNumber = 1; cardNumber <= 13; cardNumber++)
{
[cards addObject:[[Card alloc] initWithCardNumber:cardNumber suit:suit]];
}
}
[self shuffle];
}
return self;
}
-(Card *) drawCard {
if ([cards count]>0)
{
Card* tCard = [cards lastObject];
[cards removeLastObject];
return tCard;
}
return nil;
}
-(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];
}
}
- (NSString *)description{
return [NSString stringWithFormat:@"Deck : %@", cards];
}
@end