我已經學會了CS193P的第一堂課和第二堂課,並完成了第一個任務,讓我讓Matchismo以隨機順序翻閱整副撲克牌,每次只顯示一張牌。這裏是我的CardGameViewController.m的代碼關於CS193P任務1的問題
#import "CardGameViewController.h"
#import "Deck.h"
#import "PlayingCardDeck.h"
@interface CardGameViewController()
@property (weak, nonatomic) IBOutlet UILabel *flipsLabel;
@property (nonatomic) int flipCount;
@property (strong, nonatomic) Deck *fullDeck;
@end
@implementation CardGameViewController
- (void)setFlipCount:(int)flipCount{
_flipCount = flipCount;
self.flipsLabel.text = [NSString stringWithFormat:@"Flips: %d",self.flipCount];
NSLog(@"flipCount changed to %d",self.flipCount);
}
- (Deck *)fullDeck{
if (!_fullDeck) _fullDeck =[[PlayingCardDeck alloc] init];
return _fullDeck;
}
- (IBAction)touchCardButton:(UIButton *)sender {
if (!sender.currentTitle) {
[sender setBackgroundImage:[UIImage imageNamed:@"cardfront"]
forState:UIControlStateNormal];
[sender setTitle:[[self.fullDeck drawRandomCard] contents]
forState:UIControlStateNormal];
} else {
[sender setBackgroundImage:[UIImage imageNamed:@"cardback"]
forState:UIControlStateNormal];
[sender setTitle:nil forState:UIControlStateNormal];
}
self.flipCount++;
}
@end
當然,在此之前,我已經刪除了「A♣︎」在第一次攀科技演示的按鈕,因爲提示告訴我,我應該讓拿出展示而不是卡的背面。我成功構建應用程序,看起來工作得很好。
然而,轉讓的提示告訴我:
一個很好的解決方案將給予一些思想,如果在甲板上的每卡已被證明與用戶仍保持翻轉會發生什麼。做一些簡單明智的事情(例如,你不應該以任何方式修改模型中的類)。
但我的解決方案將讓用戶翻轉卡infinitely.So我嘗試如果()這樣增加了達到目標:
- (IBAction)touchCardButton:(UIButton *)sender {
if (self.flipCount < 53){ /*There are 52 cards in a deck */
if (!sender.currentTitle) {
[sender setBackgroundImage:[UIImage imageNamed:@"cardfront"]
forState:UIControlStateNormal];
[sender setTitle:[[self.fullDeck drawRandomCard] contents]
forState:UIControlStateNormal];
} else {
[sender setBackgroundImage:[UIImage imageNamed:@"cardback"]
forState:UIControlStateNormal];
[sender setTitle:nil forState:UIControlStateNormal];
}
self.flipCount++;
}
}
然後當我運行應用程序,觸摸屏,當fliplabel達到52時,它不會翻轉。
這種解決方案是否幼稚和荒謬?我搜索了github以查看其他人的解決方案,似乎忽略了這些提示。可以與我分享您的想法嗎?
任何建議將不勝感激!