OC实现猜拳游戏(用了继承特性)

来源:互联网 发布:vb入门教程 编辑:程序博客网 时间:2024/04/28 13:03
/* main.m //  150819-OC2 //  Created by LongMa on 15/8/19.    对象:    1.player         属性:姓名,出的拳头,分数        方法:出拳(公共枚举,接收,显示)             数字转换为汉字 方法    2.bot(player子类)        属性:同上//要重新定义?不需要!    3.judge        属性:姓名        方法:根据玩家和bot的出拳判断并输出结果,赢方加1分 是否继续。 */#import <Foundation/Foundation.h>//#import "Person.h"//bot.h 已经包含,故一般只写子类的头文件即可#import "Bot.h"//我去,不加头文件会报未定义!so:子类也要在主函数里添加头文件!#import "Judge.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        Person* p1 = [Person new];        p1->_name = @"至尊宝";        p1->_score = 0;        Bot *b1 = [Bot new];        b1->_name = @"青霞";        b1->_score = 0;        Judge* j1 = [Judge new];        j1->_name = @"观音菩萨";        char ch1;        do        {            [p1 acptAndshowFist];            [b1 fistRandomShow];            [j1 judgeBetween:p1 and:b1];            NSLog(@"是否继续?继续请按y,取消请按n");            rewind(stdin);            scanf("%c",&ch1);        }while (ch1 == 'y'|| ch1 == 'Y');        NSLog(@"Be Better!");    }    return 0;}/* Person.h     1.player     属性:姓名,出的拳头,分数     方法:出拳(公共枚举,接收,显示)     数字转换为汉字 方法 */#import <Foundation/Foundation.h>//#import "Bot.h"//子类头文件的决不能添加到父类头文件里typedef enum{jiaoDao, shiTou, bu} fistSelected;@interface Person : NSObject{@public    NSString* _name;    fistSelected _fist;    int _score;}- (void)acptAndshowFist;- (NSString*) numToChinese:(int )num;@end//Person.m#import "Person.h"@implementation Person- (void)acptAndshowFist{    do    {        NSLog(@"请选择您要出的拳头:1-剪刀,2-石头,3-布");        _fist = -1;//如果不赋值为1-3之外,循环回来输入‘y’时,_fist等于上次输入的值,while条件会满足,会导致bug        rewind(stdin);        scanf("%d",&_fist);    }while (_fist <1 || _fist > 3);    NSString* fist1 =  [self numToChinese:_fist];    NSLog(@"[%@]出的拳头为:%@",_name, fist1);}- (NSString *)numToChinese:(int)num{    switch (num) {        case 1:            return @"剪刀";            break;        case 2:            return @"石头";        case 3:            return @"布";        default:            break;    }    return 0;}@end//bot.h//2.bot(player子类)#import <Foundation/Foundation.h>#import "Person.h"#import <stdlib.h>@interface Bot : Person{//    fistSelected _fistBot;    }- (void)fistRandomShow;@end//bot.m#import "Bot.h"@implementation Bot-(void)fistRandomShow{    _fist = arc4random_uniform(3) + 1;//直接使用的父类的变量,并没有同名重定义!    NSString* fist1 = [self numToChinese:_fist];    NSLog(@"[%@]出的拳头为:%@",_name, fist1);}@end/*Judge.h     3.judge     属性: 姓名     方法:根据玩家和bot的出拳判断并输出结果,赢方加1分 */#import <Foundation/Foundation.h>#import "Person.h"#import "Bot.h"     //不能少?不能!@interface Judge : NSObject{    @public    NSString* _name;}- (void)judgeBetween:(Person*)person and:(Bot*)bot;@end//Judge.m#import "Judge.h"@implementation Judge- (void)judgeBetween:(Person *)person and:(Bot *)bot{    int sub = person->_fist - bot->_fist;    if (sub == -2 || sub == 1)//  猜拳赢法:1- 3 ,2- 1, 3 - 2    {        NSLog(@"【%@】赢了!",person->_name);        person->_score++;    }else if(sub == 0)    {        NSLog(@"平局");    }else    {        NSLog(@"【%@】赢了",bot->_name);        bot->_score++;    }    NSLog(@"比分为%@:%d-------VS------%@:%d",person->_name, person->_score, bot->_name, bot->_score);}@end


0 0