码迷,mamicode.com
首页 > 其他好文 > 详细

第一、二课源代码

时间:2015-11-21 00:29:41      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 1 //
 2 //  Card.h
 3 //  Machismo
 4 //
 5 //  Created by qiuda bin on 15/11/18.
 6 //  Copyright © 2015年 qiuda bin. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface Card : NSObject
12 
13 @property (strong, nonatomic) NSString *contents;
14 @property (nonatomic, getter=isChoosen) BOOL choosen;
15 @property (nonatomic, getter=isMatched) BOOL matched;
16 
17 - (int)match:(NSArray *)otherCards;
18 @end
Card.h
技术分享
 1 //
 2 //  Card.m
 3 //  Machismo
 4 //
 5 //  Created by qiuda bin on 15/11/18.
 6 //  Copyright © 2015年 qiuda bin. All rights reserved.
 7 //
 8 
 9 #import "Card.h"
10 
11 @implementation Card
12 
13 - (int)match:(NSArray *)otherCards
14 {
15     int score = 0;
16     
17     for (Card *card in otherCards){
18         if ([card.contents isEqualToString:self.contents]) {
19             score = score + 1;
20         }
21         
22     }
23     
24     return score;
25 }
26 
27 @end
Card.m
技术分享
 1 //
 2 //  Deck.h
 3 //  Machismo
 4 //
 5 //  Created by qiuda bin on 15/11/18.
 6 //  Copyright © 2015年 qiuda bin. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "Card.h"
11 
12 @interface Deck : NSObject
13 
14 - (void)addCard:(Card *)card;
15 
16 - (void)addCard:(Card *)card atTop:(BOOL)atTop;
17 
18 - (Card *)drawRandomCard;
19 
20 @end
Deck.h
技术分享
 1 //
 2 //  Deck.m
 3 //  Machismo
 4 //
 5 //  Created by qiuda bin on 15/11/18.
 6 //  Copyright © 2015年 qiuda bin. All rights reserved.
 7 //
 8 
 9 #import "Deck.h"
10 
11 @interface Deck()
12 
13 @property (strong, nonatomic) NSMutableArray *cards;
14 
15 @end
16 
17 @implementation Deck
18 
19 - (NSMutableArray *)cards
20 {
21     if (!_cards) _cards = [[NSMutableArray alloc] init];
22     
23     return _cards;
24 }
25 
26 - (void)addCard:(Card *)card
27 {
28     [self addCard:card atTop:NO];
29 }
30 
31 - (void)addCard:(Card *)card atTop:(BOOL)atTop
32 {
33     if (atTop) {
34         [self.cards insertObject:card atIndex:0];
35     } else {
36         [self.cards addObject:card];
37     }
38 }
39 
40 - (Card *)drawRandomCard
41 {
42     Card *randomCard = nil;
43     
44     if ([self.cards count]) {
45         unsigned index = arc4random() % [self.cards count];
46         randomCard = self.cards[index];
47         [self.cards removeObjectAtIndex:index];
48     }
49     
50     return randomCard;
51 }
52 
53 @end
Deck.m
技术分享
 1 //
 2 //  PlayingCard.h
 3 //  Machismo
 4 //
 5 //  Created by qiuda bin on 15/11/19.
 6 //  Copyright © 2015年 qiuda bin. All rights reserved.
 7 //
 8 
 9 #import "Card.h"
10 
11 @interface PlayingCard : Card
12 
13 @property (nonatomic, strong) NSString *suit;
14 @property (nonatomic) NSUInteger rank;
15 
16 + (NSArray *)validSuits; // public class methord
17 + (NSUInteger)maxRank;
18 @end
PlayingCard
技术分享
 1 //
 2 //  PlayingCard.m
 3 //  Machismo
 4 //
 5 //  Created by qiuda bin on 15/11/19.
 6 //  Copyright © 2015年 qiuda bin. All rights reserved.
 7 //
 8 
 9 #import "PlayingCard.h"
10 
11 
12 @implementation PlayingCard
13 - (NSString *) contents{
14     NSArray *rankStrings = [PlayingCard rankStrings];
15     return [rankStrings[self.rank] stringByAppendingString:self.suit];
16 }
17 
18 + (NSArray *)rankStrings // private class methord
19 {
20     return @[@"?", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"J", @"Q", @"K"];
21 }
22 
23 @synthesize suit = _suit;
24 + (NSArray *)validSuits
25 {
26     return @[@"♦?", @"♠?", @"♥?", @"♣?"];
27 }
28 
29 - (void)setSuit:(NSString *)suit
30 {
31     if ([[PlayingCard validSuits] containsObject:suit]) {
32         _suit = suit;
33     }
34 }
35 
36 - (NSString *)suit
37 {
38     return _suit ? _suit : @"?";
39 }
40 
41 + (NSUInteger)maxRank
42 {
43     return [[PlayingCard rankStrings] count] - 1;
44 }
45 
46 -(void)setRank:(NSUInteger)rank
47 {
48     if (rank <= [PlayingCard maxRank]) {
49         _rank = rank;
50     }
51 }
52 
53 @end
PlayingCard.m
技术分享
 1 //
 2 //  PlayingCardDeck.h
 3 //  Machismo
 4 //
 5 //  Created by qiuda bin on 15/11/19.
 6 //  Copyright © 2015年 qiuda bin. All rights reserved.
 7 //
 8 
 9 #import "Deck.h"
10 
11 @interface PlayingCardDeck : Deck
12 
13 @end
PlayingCardDeck.h
技术分享
 1 //
 2 //  PlayingCardDeck.m
 3 //  Machismo
 4 //
 5 //  Created by qiuda bin on 15/11/19.
 6 //  Copyright © 2015年 qiuda bin. All rights reserved.
 7 //
 8 
 9 #import "PlayingCardDeck.h"
10 #import "PlayingCard.h"
11 
12 @implementation PlayingCardDeck
13 - (instancetype) init
14 {
15     if (self = [super init]) {
16         for (NSString *suit in [PlayingCard validSuits]) {
17             for (NSUInteger rank = 1; rank <= [PlayingCard maxRank]; rank++) {
18                 PlayingCard *card = [[PlayingCard alloc ] init];
19                 card.suit = suit;
20                 card.rank = rank;
21                 [self addCard:card];
22             }
23         }
24     }
25     
26     return self;
27 }
28 @end
PlayingCardDeck.m
技术分享
 1 //
 2 //  ViewController.m
 3 //  Machismo
 4 //
 5 //  Created by qiuda bin on 15/11/12.
 6 //  Copyright © 2015年 qiuda bin. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "PlayingCardDeck.h"
11 
12 @interface ViewController ()
13 
14 @property (weak, nonatomic) IBOutlet UILabel *flipLabel;
15 @property (nonatomic) int flipCount;
16 @property (nonatomic, strong) Deck *deck;
17 
18 @end
19 
20 @implementation ViewController
21 
22 -(Deck *)deck
23 {
24     if (!_deck) {
25         _deck = [self createDeck];
26     }
27     
28     return _deck;
29 }
30 
31 -(Deck *)createDeck
32 {
33     return [[PlayingCardDeck alloc] init];
34 }
35 
36 - (void)setFlipCount:(int)flipCount
37 {
38     _flipCount = flipCount;
39     self.flipLabel.text = [NSString stringWithFormat:@"Flips: %d", self.flipCount];
40 }
41 
42 
43 - (IBAction)touchCardButton:(UIButton *)sender {
44     
45     if ([sender.currentTitle length]) {
46         [sender setBackgroundImage:[UIImage imageNamed:@"cardBackground"]
47                           forState:UIControlStateNormal];
48         [sender setTitle:@"" forState:UIControlStateNormal];
49         
50         self.flipCount++;
51         
52     } else {
53         Card * card = [self.deck drawRandomCard];
54         if (card) {
55             [sender setBackgroundImage:[UIImage imageNamed:@"cardFront"]
56                               forState:UIControlStateNormal];
57             [sender setTitle:card.contents forState:UIControlStateNormal];
58             
59             self.flipCount++;
60         }
61     }
62 
63 }
64 
65 @end
ViewController.m

 

第一、二课源代码

标签:

原文地址:http://www.cnblogs.com/startiOS/p/4982571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!