iOS7应用开发3、Objective-C

来源:互联网 发布:stylus rmx mac 编辑:程序博客网 时间:2024/05/16 09:44

【跟随教授的讲解和演示,并重做了课上的demo之后,惊奇地发现自己写的程序有bug,界面上12张卡牌出现后,点击任何一个,其他所有卡牌都会变成一块白板……在经历了长时间的调试之后悲催地发现,在最后的updateUI这个函数中改变背景的函数写错了,setBackgroundImage:写成了setImage:,被这一无脑的失误纠结了这么久,丢人啊……各种bug改完后,前几次课的代码都已经传到博客的资源中了,可到我的资源页下载。】

这一课中,之前实现的扑克牌翻转被扩展为一个比较完整的卡牌点数和花色比较游戏。程序的xib界面和包含的文件如下图所示:


其中的各个文件的具体内容,课程附带的pdf中均有详细的代码,也可以参照下载资源中我敲进去的代码。界面上的12张卡牌,每一个都与touchCardButton:函数联系,也就是说点击任意一张牌都会调用touchCardButton这个函数。

- (IBAction)touchCardButton:(UIButton *)sender{
int index = [self.cardButtons indexOfObject:sender];
[self.game chooseCardAtIndex:index];   
[self updateUI];
}

这个函数中只有三条语句,但是背后实现了全部的功能。

第一句:

int index = [self.cardButtons indexOfObject:sender];
从界面上的12张卡牌的按钮中获取选中的索引,也就是点了哪一张牌;

第二句:

[self.game chooseCardAtIndex:index];   
看似简单,背后却包含大量的工作。首先self.game调用了view controller中重载的getter方法:

-(CardMatchingGame *) game{    if (!_game)    {        _game = [[CardMatchingGame alloc] initWithCardCount:[self.cardButtons count] usingDeck:[self createDeck]];    }    return _game;}
在这个函数中又调用了createDeck和CardMatchingGame类的initWithCardCount方法,模拟了“发牌”的这个过程。当self.game这个函数执行完后,可以认为不但已经完成了游戏规则的定义,同时也确定了扣在牌桌上的12张牌是哪些。

随后调用的chooseCardAtIndex是CardMatchingGame类的成员函数,实现了翻牌后对这张牌状态的判断,包括判断牌之前是否被选中、是否处于与其他牌匹配的状态中等等。

- (void)chooseCardAtIndex:(NSUInteger)index{    Card *card = [self cardAtIndex:index];    if (!card.isMatched)    {        if (card.isChosen)        {            card.chosen = NO;        }        else        {            //与其他比较            for (Card *otherCard in self.cards)            {                if (otherCard.isChosen && !otherCard.isMatched)                {                    int matchScore = [card match:@[otherCard]];                    if (matchScore)                    {                        self.score += matchScore * MATCH_BONUS;                        card.matched = YES;                        otherCard.matched = YES;                    }                    else                    {                        self.score -= MISMATCH_PENALTY;                        otherCard.chosen = NO;                    }                    break;                }            }            self.score -= COST_TO_CHOOSE;            card.chosen = YES;        }    }}

该函数执行完成后,Model数据(也就是在self.game中的卡牌数据)根据规则发生了变化(主要是卡牌的match和chosen两个属性)。随后调用的[self updateUI]中将重新遍历这些数据,并根据其变化更新界面,包括显示选定的牌、将处于匹配状态下的牌锁定。

-(void) updateUI{    for (UIButton *cardButton in self.cardButtons)    {        int index = [self.cardButtons indexOfObject:cardButton];        Card *card = [self.game cardAtIndex:index];        [cardButton setTitle:[self titleForCard:card] forState:UIControlStateNormal];        [cardButton setBackgroundImage:[self backgroundImageForCard:card] forState:UIControlStateNormal];                cardButton.enabled = !card.isMatched;    }    self.scoreLabel.text = [NSString stringWithFormat:@"Score: %d",self.game.score];}

回顾整个工程,教授的确是将之前讲述的MVC的思想深刻地贯彻到了工程之中,把数据、规则和界面区分的非常清楚。这样做的好处就是不但程序清晰明了,而且扩展性强,在未来对某个模块进行扩展的时候,可以获得更好的兼容性。



原创粉丝点击