cs193p – Assignment #1 Task #3

Please note, this blog entry is from a previous course. You might want to check out the current one.

Add a text label somewhere which desribes the results of the last flip. Examples: “Matched J♥ & J♠ for 4 points” or “6♦ and J♣ don’t match! 2 point penalty!” and simply “Flipped up 8♦” if there is no match or mismatch.

The information needed to display is already inside the model. Analogue to the score create a new property to hold the text string, and set it in flipCardAtIndex:

//  CardMatchingGame.h
@property (readonly, nonatomic) NSString *descriptionOfLastFlip;

//  CardMatchingGame.m
@property (readwrite, nonatomic) NSString *descriptionOfLastFlip;

- (void)flipCardAtIndex:(NSUInteger)index
{
  ...
  if (card && !card.isUnplayable) {
    if (!card.isFaceUp) {
      self.descriptionOfLastFlip = 
        [NSString stringWithFormat:@"Flipped up %@", card.contents];
          ...
          if (matchScore) {
            ...
            self.descriptionOfLastFlip = 
              [NSString stringWithFormat:@"Matched %@ & %@ for %d points",
              card.contents, otherCard.contents,
              matchScore * MATCH_BONUS];
          } else {
            ...
            self.descriptionOfLastFlip = 
              [NSString stringWithFormat:@"%@ and %@ don’t match! %d point penalty!",
              card.contents, otherCard.contents,
              MISMATCH_PENALTY];
          }
     ...
}

Create a new label in storyboard, set its title to an empty string, connect it to a new outlet in the controller and set it in updateUI:

//  CardGameViewController.m
...
@property (weak, nonatomic) IBOutlet UILabel *resultOfLastFlipLabel;
...
- (void)updateUI
{
  ...
  self.resultOfLastFlipLabel.text = self.game.descriptionOfLastFlip;
}

The complete code is available on github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

7 thoughts on “cs193p – Assignment #1 Task #3”

  1. It’s written in Assignment 2 :

    “If you violated MVC in your solution to Required Task 3 of Assignment 1, then Required Task 8 in this assignment will be more difficult (that’s why you shouldn’t have violated MVC!) and you’ll probably want to go back and redo Required Task 3 of Assignment 1 with better MVC separation before doing Required Task 8 in this assignment.”

    Does that mean we were supposed to do this in the controller instead of the model ?

    1. It means that the model should only know what kind of cards it has and how they compare to each other, but it should not care about how the card actually looks like when on screen or what should happen when you click on it … which is part of the controller / view …

      1. So, I’m still confused.

        Does this mean that the flipCard method should or should not create the result string?

        In this code, the flipCard method in the model creates the result string. Is this wrong?

        1. … like in my comment before “it should not care about how the card actually looks like”. Please notice that the string created here is the descriptionOfLastFlip. Which you can use for debugging. If you use it to create the actual view, is another matter. In the view controller you could use the resulting score to detect the result, or parse the description, or you could even introduce new parameters in the model you can use for the view …
          … e.g. the description does not have any information that you want to display the hearts sign in the color red …

  2. I feel like adding a label to the interface with an empty title is sort of a bad practice. When you’re looking at the interface file without the label selected, it’s not immediately apparent that the label is present. So, for this exercise I added the label title “flip description”. I then set it to an empty string in a “viewDidLoad” method in my controller.

Leave a Reply

Your email address will not be published. Required fields are marked *