Solution to Assignment 3: Set Card Matching Game with History

Assignment 3 (Set Card Matching Game with History) from “Developing iOS7 Apps for iPhone and iPad (Fall 2013) by Stanford University” took a bit longer than usual to complete. The major highlights of the assignment are:

  • Set card matching in addition to the playing card game implemented in the last assignment
  • Status history access via segueing through Navigation Bar button

With an object oriented approach, it was easy to keep the logic in the CardMatchingGame model the same for both Set Card and Playing Card. Here is a list of changes since the previous assignment:

  • Allowing 2 or 3 card for Playing card game
  • Not disabling match-mode slider during gameplay, as it doesn’t make sense to click Redeal card button twice for a new match mode
    • Previous behavior:
      • Click Re-deal button
      • Choose match-mode
      • Click Re-deal button
    • Current behavior:
      • Choose match-mode
      • Click Re-deal button
  • Status moved out of the model into the ViewController
  • Removed cheat mode for debugging

Download Solution to Assignment 3: Set Card Matching Game with History

2 thoughts on “Solution to Assignment 3: Set Card Matching Game with History

    1. Thanks for your comment. The content of the set card is actually just a string (NSAttributedString) and not an image. There are 4 properties are (in SetCard.h) that get visually translated by the view controller in to a string:

      @property (nonatomic) MatchismoSetColor color;
      @property (nonatomic) MatchismoSetSymbol symbol;
      @property (nonatomic) NSUInteger number;
      @property (nonatomic) MatchismoSetShading shading;
      

      The following methods in view controller (SetCardGameViewController.m) then actually does the visual translation of the properties:

      - (NSAttributedString *)titleForCard:(Card *)card
      {
          NSAttributedString *contents = [[NSAttributedString alloc] initWithString:[self symbolForSetCardContent:card symbolSeparator:@"\n"]
                                                                                       attributes:@{NSStrokeColorAttributeName: [self colorForSetCardContent:card],
                                                                                                    NSStrokeWidthAttributeName: [self shadingSrokeWidthForSetCardContent:card],
                                                                                                    NSForegroundColorAttributeName: [self shadingForegroundColorForSetCardContent:card]}];
          return contents;
      }
      
      - (NSString *)symbolForSetCardContent:(Card *)card symbolSeparator:(NSString *)symbolSeparator
      {
          SetCard *setCard = (SetCard *)card;
          
          NSString *symbol;
          switch (setCard.symbol) {
              case MatchismoSetSymbolCircle:
                  symbol = @"●";
                  break;
              case MatchismoSetSymbolSquare:
                  symbol = @"■";
                  break;
              case MatchismoSetSymbolTriangle:
                  symbol = @"▲";
                  break;
          }
          
          NSMutableArray *symbols = [[NSMutableArray alloc] init];
          for (int i = 0; i<=setCard.number; i++) {
              [symbols addObject:symbol];
          }
          NSString *symbolForSetCard = [symbols componentsJoinedByString:symbolSeparator];
          return symbolForSetCard;
      }
      - (NSNumber *)shadingSrokeWidthForSetCardContent:(Card *)card
      {
          NSNumber *setCardShading;
          SetCard *setCard = (SetCard *)card;
          
          switch (setCard.shading) {
              case MatchismoSetShadingOpen:
                  setCardShading = @10;
                  break;
              case MatchismoSetShadingSolid:
                  setCardShading = @-10;
                  break;
              case MatchismoSetShadingShaded:
                  setCardShading = @-10;
                  break;
          }
          return setCardShading;
      }
      - (UIColor *)shadingForegroundColorForSetCardContent:(Card *)card
      {
          UIColor *setCardShadingForegroundColor;
          SetCard *setCard = (SetCard *)card;
          
          if (setCard.shading == MatchismoSetShadingShaded) {
              switch (setCard.color) {
                  case MatchismoSetColorRed:
                      setCardShadingForegroundColor = [[UIColor alloc] initWithRed:255 green:0 blue:0 alpha:0.33];
                      break;
                  case MatchismoSetColorGreen:
                      setCardShadingForegroundColor = [[UIColor alloc] initWithRed:0 green:255 blue:0 alpha:0.33];
                      break;
                  case MatchismoSetColorPurple:
                      setCardShadingForegroundColor = [[UIColor alloc] initWithRed:0.4 green:0 blue:0.4 alpha:0.33];
                      break;
              }
          } else {
              setCardShadingForegroundColor = [self colorForSetCardContent:card];
          }
          return setCardShadingForegroundColor;
      }
      - (UIColor *)colorForSetCardContent:(Card *)card
      {
          UIColor *setCardContentColor;
          SetCard *setCard = (SetCard *)card;
          
          switch (setCard.color) {
              case MatchismoSetColorRed:
                  setCardContentColor = [UIColor redColor];
                  break;
              case MatchismoSetColorGreen:
                  setCardContentColor = [UIColor greenColor];
                  break;
              case MatchismoSetColorPurple:
                  setCardContentColor = [UIColor purpleColor];
                  break;
          }
          return setCardContentColor;
      }
      

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s