我正在使用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。输出显示&#34;设置cardValue&#34;,然后实例变量编号都是正确的,但其他两个是错误的。 playerTotal读取发行此卡之前的总数,并且playerCard3Value读取0.为了给你一些上下文,这个函数不是在发出第一张或第二张牌时调用,而是在第三张和第二张牌发出时调用。前两个不在函数中处理。有谁知道为什么会这样?
答案 0 :(得分: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);
}