cs193p – Assignment #3 Task #9

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

Do something sensible when no more cards are in the deck and the user requests more.

There a various “sensible” solutions, lets just disable the button and gray it out, when there are no cards left in the deck:

- (IBAction)addCardsButtonPressed:(UIButton *)sender {
    ...
    if (self.game.deckIsEmpty) {
        sender.enabled = NO;
        sender.alpha = 0.5;
    }
}


To be able to enable the button again, create an outlet from storyboard:

@property (weak, nonatomic) IBOutlet UIButton *addCardsButton;

… and use it when the deal button has been pressed. At the same time let the collection view know that the cards have changed:

- (IBAction)dealButtonPressed:(UIButton *)sender {
    ...
    if (!self.game.deckIsEmpty) {
        self.addCardsButton.enabled = YES;
        self.addCardsButton.alpha = 1.0;
    }
    [self.cardCollectionView reloadData];
    ...
}

At the property to the game model (which we just used above):

@property (nonatomic) BOOL deckIsEmpty;

… and implement its getter to reflect the state of the deck:

- (BOOL)deckIsEmpty
{
    if (self.deck.numberOfCardsInDeck) return NO;
    return YES;
}

… which needs a slight adjustment of the deck model:

//  Deck.h
@property (nonatomic) int numberOfCardsInDeck;

//  Deck.m
- (int)numberOfCardsInDeck
{
    return [self.cards count];
}

The complete code is available on github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

Leave a Reply

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