cs193p – Assignment #3 Task #5

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

When a Set match is successfully chosen, the cards should be removed from the game (not just blanked out or grayed out, but actually removed from the user-interface and the remaining cards rearranged to use up the space that was freed up in the user- interface).

The card-matching-game model needs a new property to show how many cards are currently available, and a new property to remove a specific card:

//  CardMatchingGame.h
@property (nonatomic) int numberOfCards;

- (void)removeCardAtIndex:(NSUInteger)index;

//  CardMatchingGame.m
- (int)numberOfCards
    return [self.cards count];

- (void)removeCardAtIndex:(NSUInteger)index
    [self.cards removeObjectAtIndex:index];

The generic game view controller gets a new property which tells if an unplayable card should be removed:

@property (nonatomic) BOOL removeUnplayableCards;

After a card has been flipped and the property above has been set loop over all cards of the collection view. If they are unplayable remove them first from the data source (the game) then from the view.

- (IBAction)flipCard:(UITapGestureRecognizer *)gesture
        if (self.removeUnplayableCards) {
            for (int i = 0; i < self.game.numberOfCards; i++) {
                Card *card = [self.game cardAtIndex:i];
                if (card.isUnplayable) {
                    [self.game removeCardAtIndex:i];
                    [self.cardCollectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:i inSection:0]]];

The previous implementation of the data-source protocol for the collection view had a constant number. This has to be changed to the actual card count:

- (NSInteger)collectionView:(UICollectionView *)collectionView
    return self.game.numberOfCards;

Finally set activate the removal of the unplayable cards in the set-game view controller:

- (BOOL)removeUnplayableCards
    return YES;

The complete code is available on github.


Leave a Reply

Your email address will not be published.