-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- updated the code with the changed from Lecture 3 (Objective-C)
- improved the code to work without warning on 64bit as well as 32bit
- Loading branch information
Jeroen Wesbeek
committed
Nov 6, 2013
1 parent
3c7a3ed
commit f6f625c
Showing
7 changed files
with
323 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// CardMatchingGame.h | ||
// Matchismo | ||
// | ||
// Created by Jeroen Wesbeek on 05/11/13. | ||
// Copyright (c) 2013 MyCompany. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "Deck.h" | ||
|
||
@interface CardMatchingGame : NSObject | ||
|
||
// designated initializer | ||
- (instancetype)initWithCardCount:(NSUInteger)count | ||
usingDeck:(Deck *)deck; | ||
|
||
- (void)chooseCardAtIndex:(NSUInteger)index; | ||
- (Card *)cardAtIndex:(NSUInteger)index; | ||
|
||
@property (nonatomic, readonly) NSInteger score; // make score read only in public API | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// CardMatchingGame.m | ||
// Matchismo | ||
// | ||
// Created by Jeroen Wesbeek on 05/11/13. | ||
// Copyright (c) 2013 MyCompany. All rights reserved. | ||
// | ||
|
||
#import "CardMatchingGame.h" | ||
|
||
@interface CardMatchingGame() | ||
@property (nonatomic, readwrite) NSInteger score; // make score writable in out private API | ||
@property (nonatomic, strong) NSMutableArray *cards; // of Card | ||
@end | ||
|
||
@implementation CardMatchingGame | ||
|
||
- (NSMutableArray *)cards | ||
{ | ||
if (!_cards) _cards = [[NSMutableArray alloc] init]; | ||
return _cards; | ||
} | ||
|
||
- (instancetype)initWithCardCount:(NSUInteger)count | ||
usingDeck:(Deck *)deck | ||
{ | ||
self = [super init]; | ||
|
||
if (self) { | ||
for (NSUInteger i = 0; i < count; i++) { | ||
Card *card = [deck drawRandomCard]; | ||
|
||
if (card) { | ||
[self.cards addObject:card]; | ||
} else { | ||
self = nil; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return self; | ||
} | ||
|
||
//#define MISMATCH_PENALTY 2 // it's a matter of preference what to use, however: | ||
static const int MISMATCH_PENALTY = 2; // this is typed so it will show in the debugger | ||
static const int MATCH_BONUS = 4; | ||
static const int COST_TO_CHOOSE = 1; | ||
|
||
- (void)chooseCardAtIndex:(NSUInteger)index | ||
{ | ||
Card *card = [self cardAtIndex:index]; | ||
|
||
if (!card.isMatched) { | ||
if (card.isChosen) { | ||
card.chosen = NO; | ||
} else { | ||
// match against another card | ||
for (Card *otherCard in self.cards) { | ||
if (otherCard.isChosen && !otherCard.isMatched) { | ||
int matchScore = [card match:@[otherCard]]; | ||
|
||
if (matchScore) { | ||
// increase score | ||
self.score += (matchScore * MATCH_BONUS); | ||
|
||
// mark cards as matched | ||
card.matched = YES; | ||
otherCard.matched = YES; | ||
} else { | ||
// mismath penalty when cards do no match | ||
self.score -= MISMATCH_PENALTY; | ||
|
||
// flip othercard | ||
otherCard.chosen = NO; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
self.score -= COST_TO_CHOOSE; | ||
card.chosen = YES; | ||
} | ||
} | ||
} | ||
|
||
- (Card *)cardAtIndex:(NSUInteger)index | ||
{ | ||
return (index < [self.cards count]) ? self.cards[index] : nil; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters