我已经学习了CS193P的第一堂课和第二堂课,并完成了第一个作业,该作业要求我让Matchismo以随机顺序翻阅整个扑克牌,一次显示每张纸牌。这是我的CardGameViewController.m代码

#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♣︎”,因为提示告诉我应该显示卡背面来显示它。我成功构建了应用程序,似乎工作很好。

但是,作业的提示告诉我:


  一个好的解决方案将考虑如果卡组中的每张卡都已显示并且用户仍在继续翻转会发生什么情况。做一些简单而明智的事情(例如,您不必以任何方式修改Model中的类)。


但是我的解决方案将允许用户无限翻转卡片,因此我尝试通过添加一个if()来达到目标​​:

- (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以查看其他人的解决方案,似乎这些提示被忽略了。您可以和我分享您的想法吗?

任何建议将不胜感激!

最佳答案

您只需要用一个if语句将其包装

if (card) {
       //flip the card
}


记住cardDeck.h的对象,并且我们已经将Deck设置为在卡用完时返回nil。因此,一旦卡牌用完了,它将停止翻转

另外,最重要的是,没有神奇的数字!即使编写了解释它的命令,也不能仅仅将53扔掉。这只是不好的编程习惯

- (IBAction)touchCardButton:(UIButton *)sender {

   // No magic number! --> if (self.flipCount < 53){

   if (!sender.currentTitle) {
      Card *card = [self.fullDeck drawRandomCard];

      if (card) {  // <----- this is what I'm talking about
          [sender setBackgroundImage:[UIImage imageNamed:@"cardfront"]
                            forState:UIControlStateNormal];
          [sender setTitle:card.contents
                  forState:UIControlStateNormal];
      }
   } else {
      [sender setBackgroundImage:[UIImage imageNamed:@"cardback"]
                      forState:UIControlStateNormal];
      [sender setTitle:nil forState:UIControlStateNormal];
  }
      self.flipCount++;
}

关于ios - 有关CS193P分配1的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25580439/

10-11 20:26