cs193p – Assignment #3 Extra Task #6

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

Make this a two player game. There’s no need to go overboard here. Think of a simple UI and a straightforward implementation that make sense.

Create a new property to hold the separate scores of the players and another one to know the current player:

@property (strong, nonatomic) NSMutableArray *playerScores; // of NSNumber
@property (nonatomic) int currentPlayer;
...
- (NSMutableArray *)playerScores
{
    if (!_playerScores) {
        _playerScores = [NSMutableArray arrayWithObjects:@0, @0, nil];
    }
    return _playerScores;
}


The score for the current player derives from the game score minus the score of the other player. When the score has decreased, the current player looses the turn …

- (void)calculateMultiPlayerScore
{
    int otherPlayer = (self.currentPlayer + 1) % 2;
    int newScore = self.game.score - [self.playerScores[otherPlayer] intValue];
    if (newScore < [self.playerScores[self.currentPlayer] intValue]) {
        self.playerScores[self.currentPlayer] = @(newScore);
        self.currentPlayer = otherPlayer;
    } else {
        self.playerScores[self.currentPlayer] = @(newScore);
    }    
}

It is calculated when a card has been flipped, or when cards have been added:

- (IBAction)flipCard:(UITapGestureRecognizer *)gesture
{
        ...
        [self calculateMultiPlayerScore];
        ...
}

- (IBAction)addCardsButtonPressed:(UIButton *)sender {    
    if ([[self.game matchingCards] count]) {
        ...
        [self calculateMultiPlayerScore];
        ...
    }
}

The scores are displayed in the current score label, where the score of the current player is highlighted using attributed strings:

- (void)updateUI
{
    ...
    NSString *text = [NSString stringWithFormat:@"P1: %@  P2: %@",
                      self.playerScores[0], self.playerScores[1]];
    NSMutableAttributedString *label = [[NSMutableAttributedString alloc] initWithString:text];
    NSRange range = ]];
    if (range.location != NSNotFound) {
        [label addAttributes:@{ NSStrokeWidthAttributeName : @-3,
                                NSForegroundColorAttributeName : [UIColor blueColor] }
                       range:range];
    }
    self.scoreLabel.attributedText = label;
    self.resultOfLastFlipLabel.alpha = 1;
    self.resultOfLastFlipLabel.text = self.game.descriptionOfLastFlip;    
    [self updateSliderRange];
}

… and the scores need to be reset when a new deck is dealt:

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

Finally you might need to adjust the size of the label and/or its content-hugging setting.

Because we should not go overboard … the game concept is a little bit flawed. There could be better ways to determine who is the current player … Also the high scores are still saved for the “complete” game and not for the individual player … but we did not want to go overboard 😉

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 *