Stanford Ios 7 Assignment 2

Attached is the solution to the Assignment 2: Matchismo 2 (3-Card Matching Game) from the course “Developing iOS7 Apps for iPhone and iPad (Fall 2013)” by Stanford University (available in iTunes). The attached solution is not limited to a 2-card or 3-card matching game. The same method can be used for n-cards.

The solution addresses all the required tasks in the Assignment. The scoring mechanism implemented is a very simplified approach based on what was already shown in the lectures. While the iOS related tasks were easy to implement, the 3-card matching logic took some thought. There are obviously many approaches to matching a 3-card game. The solution attached is based on the CardMatchingGame model.

The model already stores the chosen and matched states of each card. While it may be tempting to create separate procedures to loop through the number of cards being played and matched, this is not required as the model already holds that information. All we need to do is simply to ask it.

The implementation also adds 2 features not required in the assignment. It presents the user with an alert (UIAlertView) when re-dealing the cards. This was easy to implement via UIAlertViewDelegate. A quick jump to the documentation shows how to do this. The other feature that was added was via the method – (void)shouldDisableGame. This disables the last remaining cards from being clicked if they cannot be matched.

A quick video demonstrating the solution in action.

Please download the attachment for full details – it contains the Xcode 5 project for Matchismo. Just unzip the file matchismo.zip and open Matchismo.xcodeproj

Download Solution to Assignment 2: Matchismo 2 (3-Card Matching Game)

The core matching logic is pasted below with inline code comments:

- (void)chooseCardAtIndex:(NSUInteger)index { Card *card = [self cardAtIndex:index]; if (!card.isMatched) { if (card.isChosen) { card.chosen = NO; self.status = @""; } else { // match against other cards // First we store the cards that are chosen and not matched in currentChosenCards NSMutableArray *currentChosenCards = [[NSMutableArray alloc] init]; NSMutableString *statusCurrentChosenCards = [[NSMutableString alloc] init]; for (Card *otherCard in self.cards) { if (otherCard.isChosen && !otherCard.isMatched) { [currentChosenCards addObject:otherCard]; [statusCurrentChosenCards appendFormat:@"%@ ", otherCard.contents]; } } if ([currentChosenCards count]) { self.status = [[NSString stringWithFormat:@"Chose %@ to match with: ", card.contents] stringByAppendingString:statusCurrentChosenCards]; } else { self.status = [NSString stringWithFormat:@"Chose %@", card.contents]; } // The model is already tracking how many cards are currently chosen and not matched // So all we need to do is match that count with the number of cards we are playing with // We do a -1 because currentChosenCards doesn't include the card that was just clicked if ([currentChosenCards count] == self.numberOfCardsToPlayWith-1) { int matchScore = [card match:currentChosenCards]; if (matchScore) { self.score += matchScore * MATCH_BONUS; for (Card *otherCard in currentChosenCards) { otherCard.matched = YES; } card.matched = YES; self.status = [[NSString stringWithFormat:@"Scored: %d. Match found for: %@ ", matchScore * MATCH_BONUS, card.contents] stringByAppendingString:statusCurrentChosenCards]; } else { self.score -= MISMATCH_PENALTY; for (Card *otherCard in currentChosenCards) { otherCard.chosen = NO; } self.status = [[NSString stringWithFormat:@"Penalty: %d. No match found for: %@ ", MISMATCH_PENALTY, card.contents] stringByAppendingString:statusCurrentChosenCards]; } } self.score -= COST_TO_CHOOSE; card.chosen = YES; } } // This is not part of the assignment. It's just a nice way to end the game. // If the last remaning cards do not match, the game should freeze those cards // from being clicked and end the game. [self shouldDisableGame]; }

Like this:

LikeLoading...

Related

В нескольких милях от этого места человек в очках в железной оправе сидел на заднем сиденье фиата, мчавшегося по проселочной дороге.

- Клуб Колдун, - повторил он, напомнив таксисту место назначения. Водитель кивнул, с любопытством разглядывая пассажира в зеркало заднего вида. - Колдун, - пробурчал он себе под нос.

0 Replies to “Stanford Ios 7 Assignment 2”

Lascia un Commento

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *