cs193p – Assignment #3 Extra Task #5

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

Knowing how to find Sets in the remaining cards also would allow you to let the user cheat. Have a button that will show them a Set (if available). It’s up to you how you want to show it, but maybe some little indicator (a star or something) on each of the 3 cards?

Start by adding a new button in storyboard:

cs193p - assignment #3 extra task #5
cs193p – assignment #3 extra task #5


Create a new action for this button, which fills a new property with possible matching cards:

@property (strong, nonatomic) NSArray *cheatCards; // of Card
...
- (IBAction)cheatButtonPressed {
    self.cheatCards = [self.game matchingCards];
    [self updateUI];
}

Both – the updateUI method and the method which draws new cells – call a new helper method to mark the matching cards in the first section. A second helper method is called to remove this mark again. Note that clearing function is called even for the third section, because it uses the same cell type and thus could dequeue a cell from the first section – another way would be to use a new cell type …

- (void)updateUI
{
    for (UICollectionViewCell *cell in [self.cardCollectionView visibleCells]) {
        [self clearCell:cell];
        ....
        if (indexPath.section == 2) {
            ....
        } else {
            ....
            [self updateCell:cell ifCheatCard:[self.game cardAtIndex:indexPath.item]];
        }
    }
    ...
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        ...
        [self clearCell:cell];
        ...
    }
    ...
    [self clearCell:cell];
    ...
    [self updateCell:cell ifCheatCard:card];
    return cell;
}

If a card should be marked in “cheat mode” place a star image in the right top corner of the card:

- (void)updateCell:(UICollectionViewCell *)cell ifCheatCard:(Card *)card
{
    if ([self.cheatCards containsObject:card]) {
        UIImage *image = [UIImage imageNamed:@"star.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        CGFloat min = cell.bounds.size.width;
        if (cell.bounds.size.height < min) min = cell.bounds.size.height;
        imageView.frame = CGRectMake(min / 10, min / 10, min / 5, min / 5);
        [cell addSubview:imageView];        
    }
}

Which gets removed again in the clearing function:

- (void)clearCell:(UICollectionViewCell*) cell
{
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) [view removeFromSuperview];
    }
}

The marks are removed when a new match has been found:

- (IBAction)flipCard:(UITapGestureRecognizer *)gesture
{
            ...
            if ([matchedIndexPaths count]) {
                self.cheatCards = nil;
                ...
            }
        ....
}

… or when a new set is dealt:

- (IBAction)dealButtonPressed:(UIButton *)sender {
    ...
    self.cheatCards = nil;
    ...
}

… because of the additional button you might want to adjust also the code of the last-flips label to cope with the available space.

The complete code is available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.