0

我正在使用 Xcode 4.6 版。在我正在为 iPhone 和 iPad 制作的应用程序中,我制作了一个名为 dealCard 的函数。这里是:

- (void)dealCard : (UIImageView *)cardImage : (int)card : (int)suit : (BOOL)aceBool : (int) cardValue : (int)total
{
suit = arc4random() % 4;
card = arc4random() % 13;
if (suit == 0)
{
    //set images

if (suit == 1)
{
   //set images
}

if (suit == 2)
{
    //set images
}

if (suit == 3)
{
    //set images
}
if (card < 10 && card != 0)
{
    NSLog(@"Setting cardValue");
    cardValue = card+1;
}
else if (card != 0)
{
    NSLog(@"Setting cardValue");
    cardValue = 10;
}
else
{
    NSLog(@"Setting cardValue");
    aceBool = YES;
    if (total + 11 <= 21)
    {
        cardValue = 11;
    }
    else
    {
        cardValue = 1;
    }

}
total = total + cardValue;
NSLog(@"Total = %d cardValue = %d",total,cardValue);
NSLog(@"playerTotal = %d playerCard3Value = %d",playerTotal,playerCard3Value);
}

在我放的页面下方:

[self dealCard: playerCard3Image : playerCard3 : playerSuit3 : playerCard3ace : playerCard3Value : playerTotal];

我的问题出在cardValue 中。输出显示“Setting cardValue”,然后实例变量编号都正确,但其他两个错误。playerTotal 读取这张牌发出前的总数,而 playerCard3Value 读取 0。为了给你一些上下文,这个函数不是在发出第一张或第二张牌时调用,而是在第三张及以后的时候调用。前两个不在函数中处理。有谁知道为什么会这样?

4

1 回答 1

1

面向对象编程是一件美好的事情。快速编码,向您展示它是如何工作的......

//
//  PlayingCard.h
//

#import <Foundation/Foundation.h>

@interface PlayingCard : NSObject

- (id)initWithSuit:(NSInteger)suit value:(NSInteger)value;
- (BOOL)isAce;
- (BOOL)isFace;

@end


//
//  PlayingCard.m
//

#import "PlayingCard.h"

@interface PlayingCard ()
@property(nonatomic, assign) NSInteger suit;
@property(nonatomic, assign) NSInteger value;

@end

@implementation PlayingCard

- (id)initWithSuit:(NSInteger)suit value:(NSInteger)value {
    self = [self init];
    if (self) {
        _suit = suit;
        _value = value;
    }
    return self;
}

- (BOOL)isAce {
    return self.value == 0;
}

- (BOOL)isFace {
    return self.value > 10;
}

- (NSString *)suitName {
    switch (self.suit) {
        case 0:
            return @"spades";
            break;
        case 1:
            return @"diamonds";
            break;
        case 2:
            return @"clubs";
            break;
        case 3:
            return @"hearts";
            break;

        default:
            return @"";
            break;
    }
}

// so it prints nicely.  you should consider making the suits enums
- (NSString *)description {
    return [NSString stringWithFormat:@"%@: suit:%@, value:%d", [super description], [self suitName], self.value];
}

@end


//
//  CardDeck.h
//

#import <Foundation/Foundation.h>

@class PlayingCard;

@interface CardDeck : NSObject

+ (id)cardDeck;
- (void)shuffle;
- (NSUInteger)count;
- (PlayingCard *)dealOne;

@end

//
//  CardDeck.m
//

#import "CardDeck.h"
#import "PlayingCard.h"

@interface CardDeck ()
@property(nonatomic,strong) NSMutableArray *cards;
@end

@implementation CardDeck

+ (id)cardDeck {

    CardDeck *deck = [[self alloc] init];
    for (NSInteger suit = 0; suit < 4; suit++) {
        for (NSInteger value = 0; value < 13; value++) {
            PlayingCard *card = [[PlayingCard alloc] initWithSuit:suit value:value];
            [deck.cards addObject:card];
        }
    }
    return deck;
}

- (id)init {
    self = [super init];
    if (self) {
        _cards = [NSMutableArray array];
    }
    return self;
}


- (void)shuffle {
    NSMutableArray *shuffledCards = [NSMutableArray array];
    for (int i=0; i<52; i++) {
        int randomIndex = arc4random_uniform(52-i);
        [shuffledCards addObject:[self.cards objectAtIndex:randomIndex]];
        [self.cards removeObjectAtIndex:randomIndex];
    }
    self.cards = shuffledCards;
}

- (NSUInteger)count {
    return self.cards.count;
}


- (PlayingCard *)dealOne {

    PlayingCard *card = [self.cards lastObject];
    [self.cards removeLastObject];
    return card;
}

@end

像这样称呼它...

CardDeck *deck = [CardDeck cardDeck];
// try it with and without shuffle
[deck shuffle];
while (deck.count) {
    PlayingCard *card = [deck dealOne];
    NSLog(@"%@", card);
}
于 2013-03-07T01:02:53.080 回答