-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatchismoSetCardViewController.m
More file actions
142 lines (126 loc) · 5.84 KB
/
MatchismoSetCardViewController.m
File metadata and controls
142 lines (126 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// MatchismoSetCardViewController.m
// Matchismo
//
// Created by Priyanjana Bengani on 18/12/2013.
// Copyright (c) 2013 anothercookiecrumbles. All rights reserved.
//
#import "MatchismoSetCardViewController.h"
#import "MatchismoSetCardDeck.h"
#import "MatchismoSetCard.h"
@interface MatchismoSetCardViewController ()
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@end
@implementation MatchismoSetCardViewController
- (void) awakeFromNib {
[super awakeFromNib];
self.gameMode = 1; // Set mandates a three-card match. Hence, defaulting the gameMode to be three-card match.
}
- (MatchismoDeck*)createDeck {
return [[MatchismoSetCardDeck alloc] init];
}
- (void) setCardButtons:(NSArray *)cardButtons {
_cardButtons = cardButtons;
[self updateUI];
}
/*
* @Overridde : Yes, I know this isn't java, but it still seems important to highlight the intent.
* No compile-time checking though. Would be nice if Objective-C had something similar to
* annotations in Java, or attributes in C#.
*/
#pragma begin overrides // I just want to highlight overrides =( | This is probably not the best way to use pragma.
- (void) updateUI {
for (UIButton *cardButton in self.cardButtons) {
long index = [self.cardButtons indexOfObject:cardButton];
MatchismoCard* card = [self.game cardAtIndex:index];
if (![card isKindOfClass:[MatchismoSetCard class]]) {
NSLog(@"The card is not of type MatchismoSetCard, and hence is not valid for the Set game.");
return;
}
NSAttributedString* displayString = [self constructDisplayString:(MatchismoSetCard*)card];
[cardButton setAttributedTitle:displayString forState:UIControlStateNormal];
cardButton.enabled = !card.isMatched;
if (!cardButton.enabled) {
[cardButton setAttributedTitle:[[NSAttributedString alloc] initWithString:@"" attributes:nil] forState:UIControlStateNormal];
}
}
[self updateScore];
[self updateLastMove];
}
- (NSAttributedString*) lastMove {
NSMutableAttributedString* cards = [NSMutableAttributedString new];
NSMutableArray* pickedCards = [[NSMutableArray alloc] init];
for (MatchismoCard* pickedCard in self.game.lastMove) {
[pickedCards addObject:pickedCard.description];
if ([pickedCard isKindOfClass:[MatchismoSetCard class]]) {
MatchismoSetCard* setCard = (MatchismoSetCard*)pickedCard;
NSAttributedString* card = [self constructDisplayString:setCard];
[cards appendAttributedString:card];
if (pickedCard != [self.game.lastMove lastObject]) {
[cards appendAttributedString:[[NSAttributedString alloc] initWithString:@", "]];
}
}
else {
NSLog(@"The picked card is not a MatchismoSetCard.");
}
}
return cards;
}
#pragma end overrides // I just want to highlight overrides =(
- (NSAttributedString*) constructDisplayString:(MatchismoSetCard*) card {
NSAttributedString* stringToDisplay = [NSMutableAttributedString new];
// dictionary to collect the attributes, which need to be applied to the card.
NSMutableDictionary *attributeDictionary = [NSMutableDictionary new];
NSString* shape = @"";
if (card.pattern && card.shape && card.colour && card.number) {
SEL colourSelector = NSSelectorFromString(card.colour);
UIColor* colour = [UIColor clearColor];
if ([UIColor respondsToSelector:colourSelector]) {
colour = [UIColor performSelector:colourSelector];
}
else {
NSLog(@"Unrecognised colour %@", card.colour);
}
[attributeDictionary setObject:colour forKey:NSForegroundColorAttributeName];
if ([card.pattern isEqualToString:@"stripes"]) {
// I don't know how to create stripes/gradients on text, without using a UIImage.
// However, I don't really want to create multiple UIImages simply to create a pattern.
// There has to be a more elegant solution, but for now, just do an outline with a different colour.
// How slack!
[attributeDictionary setObject:[UIColor blueColor] forKey:NSStrokeColorAttributeName];
[attributeDictionary setObject:@-4 forKey:NSStrokeWidthAttributeName];
}
else if ([card.pattern isEqualToString:@"outline"]) {
[attributeDictionary setObject:[UIColor blackColor] forKey:NSStrokeColorAttributeName];
[attributeDictionary setObject:@-4 forKey:NSStrokeWidthAttributeName];
[attributeDictionary setObject:[attributeDictionary[NSForegroundColorAttributeName] colorWithAlphaComponent:0.7] forKey:NSForegroundColorAttributeName];
}
else if ([card.pattern isEqualToString:@"filled"]) {
// Do nothing? The "NSForegroundColorAttributeName" set with the colours should take care of it.
}
else {
NSLog(@"Unrecognised pattern %@", card.pattern);
}
if ([card.shape isEqualToString:@"oval"]) {
shape = @"●";
}
else if ([card.shape isEqualToString:@"squiggle"]) {
shape = @"◼︎";
}
else if ([card.shape isEqualToString:@"diamond"]) {
shape = @"▲";
}
else {
NSLog(@"Unrecognised shape %@", card.shape);
}
shape = [shape stringByPaddingToLength:(card.number * [shape length]) withString:shape startingAtIndex:0];
stringToDisplay = [[NSAttributedString alloc] initWithString:shape attributes:attributeDictionary];
}
else {
// NSLog does the stringWithFormat, so that I don't have to.
NSLog(@"All the card attributes are not set. Pattern:%@, Shape:%@, Colour:%@, Number:%d",
card.pattern, card.shape, card.colour, card.number);
}
return stringToDisplay;
}
@end