Graphical Set: solution of assignment 4 of CS193P (2013/2014)

Graphical Set is a card game based on Set with some improvements:

  • cards are presented using generic views and they have diamonds, squiggles and ovals drawn using Core Graphics,
  • there are many animations created using UIView and UIKit Dynamics APIs,
  • iPhone 4.x and iPhone 5. are both supported either in portrait and landscape.

From an implementation point of view, I think the most important thing to mention is the method updateUI within CardGameVC

- (void)updateUI
{
    NSUInteger visibleCardsCount = 0; // number of visible cards

    for (int index = 0; index < self.game.numberOfCardsCurrentlyInPlay; index++) {
        Card *card = [self.game cardAtIndex:index];
        UIView *cardView = [self cardViewAtIndex:index];
        if ([self.gridView.subviews containsObject:cardView]) {
            [self updateCardView:cardView atIndex:visibleCardsCount usingCard:card];
            visibleCardsCount = [self updateVisibleCardsCount:visibleCardsCount usingCard:card];
        } else if (!card.isMatched) {
            cardView = [self createCardViewAtIndex:visibleCardsCount usingCard:card];
            [cardView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCardView:)]];
            [self.cardViews insertObject:cardView atIndex:index];
            [self.gridView addSubview:cardView];
            visibleCardsCount++;
        }
    }
    self.scoreLabel.text = [NSString stringWithFormat:@"Score: %ld", (long)self.game.score];
}

As you can see, every time the UI gets updated I either update or create cards. I use visibleCardsCount in order to position cards in the grid, since matchismo and set have a slightly different behavior when matches or sets are found. In the first case, cards have to stay while in the second they have to be removed. You are completely free to change cellAspectRatio within SetPlayingCardGameVC because SetPlayingCardView adapts to size changing.

cellAspectRatio=0.5 / 2 in landscape and portrait

cellAspectRatio=0.5 / 2 in landscape and portrait

Download cs193p assignment 4 solution

Any question? Please, leave a comment below! 🙂

Leave a Reply

Required fields are marked *.