-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatchizmoViewController.m
More file actions
87 lines (70 loc) · 2.28 KB
/
MatchizmoViewController.m
File metadata and controls
87 lines (70 loc) · 2.28 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
//
// ViewController.m
// Matchizmo
//
// Created by Voitenko Daniil on 28.10.15.
// Copyright © 2015 Voitenko Daniil. All rights reserved.
//
#import "MatchizmoViewController.h"
#import "PlayingDeck.h"
#import "CardMatchingGame.h"
@interface MatchizmoViewController ()
@property (nonatomic,strong) CardMatchingGame *game;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) Card *cardInGame;
@property (weak, nonatomic) IBOutlet UISegmentedControl *numberOfMatches;
@end
@implementation MatchizmoViewController
- (Deck *)createDeck
{
return [[PlayingDeck alloc] init];
}
- (CardMatchingGame *)game
{
if (!_game) _game = [[CardMatchingGame alloc] initWithCardCount:[self.cardButtons count]
usingDeck:[self createDeck]];
_game.numberOfMatches = [self numberOfMatches];
return _game;
}
- (NSUInteger)numberOfMatches
{
return self.numberOfMatches.selectedSegmentIndex+2;
}
- (IBAction)ChangeCard:(UIButton *)sender
{
int chosenButtonIndex = [self.cardButtons indexOfObject:sender];
[self.game chooseCardAtIndex:chosenButtonIndex];
[self updateUI];
}
- (void)updateUI
{
for (UIButton *cardButton in self.cardButtons) {
int cardButtonIndex = [self.cardButtons indexOfObject:cardButton];
self.cardInGame = [self.game cardAtIndex:cardButtonIndex];
[cardButton setTitle:[self titleForCard:self.cardInGame]
forState:UIControlStateNormal];
[cardButton setBackgroundImage:[self backgroundImageForCard:self.cardInGame]
forState:UIControlStateNormal];
cardButton.enabled = !self.cardInGame.isMatched;
self.scoreLabel.text = [NSString stringWithFormat:@"Score: %d", self.game.score];
}
}
- (NSString *)titleForCard:(Card *)card
{
return card.isChosen ? card.contents : @"";
}
- (UIImage *)backgroundImageForCard:(Card *)card
{
return [UIImage imageNamed:card.isChosen ? @"cardFront" : @"cardBack"];
}
- (IBAction)newGameButton:(UIButton *)sender
{
self.game = nil;
[self updateUI];
}
- (IBAction)changeNumberOfmatches:(id)sender {
self.game = nil;
[self updateUI];
}
@end