cs193p – Assignment #3 Task #3

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

For Set, 12 cards should be initially dealt and Set cards should always show what’s on them even if they are technically “face down”. For the Playing Card game deal 22 cards, all face down.

Start by removing the card buttons from the set view controller in storyboard and remove the parts in the set-view-controller class which access the former card buttons – at the same time you can move lots of the public interface of the generic game-view-controller class back to be private … which is mostly cosmetic …

And because there are no card buttons any more set the starting number of cards to 12:

- (NSUInteger)startingCardCount
{
    return 12;
}

.. and change the code for the playing card view controller to 22:

- (NSUInteger)startingCardCount
{
    return 22;
}

In story board drag out a collection view and place it where the just deleted buttons have been. Control drag from the collection view to the controller class to add the controller as data source. Control drag in the other direction (from the controller to the view) to connect the outlet cardCollectionView.

Set the reuse identifier of the collection-view cell to “PlayingCard”. Create a new class SetCardCollectionViewCell derived from UICollectionViewCell and use it for the cell in storyboard.

Create a new subclass SetCardView of UIView. Drag out a view into the collection-view cell and change its class to the newly created one.

Create an outlet from the view in the cell class. Change the background color to clear and remove the check mark from opaque.

In the new view class create properties to hold the cards properties:

@property (strong, nonatomic) NSString *color;
@property (strong, nonatomic) NSString *symbol;
@property (strong, nonatomic) NSString *shading;
@property (nonatomic) NSUInteger number;
@property (nonatomic) BOOL faceUp;

Create a preliminary set card by writing the text of the properties:

- (void)drawRect:(CGRect)rect
{
    UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                           cornerRadius:12.0];
    [roundedRect addClip];
    if (self.faceUp) {
        [[UIColor colorWithWhite:0.9 alpha:1.0] setFill];
    } else {
        [[UIColor whiteColor] setFill];        
    }
    UIRectFill(self.bounds);
    [[UIColor colorWithWhite:0.8 alpha:1.0] setStroke];
    [roundedRect stroke];    
    [[UIColor blackColor] setFill];
    CGPoint point;
    point.x = 5;
    point.y = 2;
    [self.color drawAtPoint:point withFont:[UIFont systemFontOfSize:10]];
    point.y += 10;
    [self.symbol drawAtPoint:point withFont:[UIFont systemFontOfSize:10]];
    point.y += 10;
    [self.shading drawAtPoint:point withFont:[UIFont systemFontOfSize:10]];
    point.y += 10;
    [[NSString stringWithFormat:@"%d", self.number] drawAtPoint:point 
                                                       withFont:[UIFont systemFontOfSize:10]];
}

Finally adjust the cell updating method in the set-card view controller:

- (void)updateCell:(UICollectionViewCell *)cell usingCard:(Card *)card
{
    if ([cell isKindOfClass:[SetCardCollectionViewCell class]]) {
        SetCardView *setCardView = ((SetCardCollectionViewCell *)cell).setCardView;
        if ([card isKindOfClass:[SetCard class]]) {
            SetCard *setCard = (SetCard *)card;
            setCardView.color = setCard.color;
            setCardView.symbol = setCard.symbol;
            setCardView.shading = setCard.shading;
            setCardView.number = setCard.number;
            setCardView.faceUp = setCard.isFaceUp;
            setCardView.alpha = setCard.isUnplayable ? 0.3 : 1.0;
        }
    }
}

The complete code is available on github.

FacebooktwitterredditpinterestlinkedintumblrmailFacebooktwitterredditpinterestlinkedintumblrmail

Leave a Reply

Your email address will not be published.