【发布时间】:2013-03-12 21:23:28
【问题描述】:
在我的纸牌匹配游戏中(遵循斯坦福课程),我需要创建一个UISwitch,它将匹配两张卡之间的游戏模式更改为三张匹配,现在我已经有了一个看起来像这样的匹配方法:
-(int)match:(NSArray *)cardToMatch {
int score = 0;
if (cardToMatch.count == 1) {
PlayingCards *aCard = [cardToMatch lastObject];
if ([aCard.suit isEqualToString: self.suit]) {
score = 1;
} else if (aCard.rank == self.rank) {
score = 4;
}
}
return score;
}
它已经是一个数组,但我只是在两张卡之间进行检查。我怎样才能改进这种方法来检查三个,或者创建一个单独的?
这也是检查翻牌的方法:
-(Card *) cardAtIndex:(NSUInteger)index {
return (index < self.cards.count) ? self.cards[index] : nil;
}
#define FLIP_COST 1
#define MISMATCH_PENALTY 2
#define BONUS 4
-(void) flipCardAtIndex:(NSUInteger)index {
Card *card = [self cardAtIndex:index];
if (!card.isUnplayable) {
if (!card.isFaceUp) {
for (Card *otherCard in self.cards) {
if (otherCard.isFaceUp && !otherCard.isUnplayable) {
int matchScore = [card match:@[otherCard]];
if (matchScore) {
otherCard.unplayble = YES;
card.unplayble = YES;
self.notification = [NSString stringWithFormat:@"%@ & %@ match!", card.contents, otherCard.contents];
self.score += matchScore * BONUS;
} else {
otherCard.faceUp = NO;
self.score -= MISMATCH_PENALTY;
self.notification = [NSString stringWithFormat:@"%@ did not matched to %@", card.contents, otherCard.contents];
}
break;
}
}
self.score -= FLIP_COST;
}
card.faceUp = !card.isFaceUp;
}
}
谢谢。
【问题讨论】:
-
这并不能回答您的问题,但可能会让您思考:为什么要使用字符串比较来匹配西装?比较字符串非常“昂贵”,因此您可能希望使用
enum来表示花色,因为比较它们(整数)是微不足道的。 -
这是个好主意 :) 谢谢。你也许对我的问题也有一些解决方案..?即使我同意您使用枚举的建议,我如何比较 3 个对象..?让我发疯@trojanfoe
-
这是我第一次发布东西但没有人回应..奇怪@trojanfoe
-
在伪代码中,这只是
obj1 == obj2 && obj2 == obj3。 -
是的,但是当我尝试做类似'obj1[0] == obj2[1]'之类的事情时,我也做不到
obj1[0].suit@trojanfoe
标签: ios objective-c cocoa-touch cocoa nsarray