猜拳游戏 3.0升级版

    使用了继承对成员属性进行优化,除去繁琐的定义,优化了玩家出拳时输入其他字符不能正常运行的bug,使用了@property 进行属性的封装, 重写了构造方法, 使得玩家可以创建对象时候直接定义某些属性;


main.c


#import <Foundation/Foundation.h>
#import "Human.h"
#import "Robot.h"
#import "Roles.h"
#import "Judge.h"

int main(int argc, const char * argv[])
{
    Human *h = [[Human alloc] initWithName:@"小白"];
    Robot *t =[[Robot alloc] initWithName:@"外星人"];
    Judge *j = [[Judge alloc] initWithName:@"黑哨"];
    char flag;
    
    while (1)
    {
        [h guess];
        [t guess];
        [j judge:h and:t];
        [j showScore:h and:t];
        NSLog(@"请问你还要继续吗?y/n");
        rewind(stdin);
        scanf("%c",&flag);
        if (flag == 'n')
        {
            NSLog(@"欢迎你下次再来");
            break;
        }

        
    }
        return 0;
}



Judge.h




#import <Foundation/Foundation.h>
#import "Human.h"
#import "Robot.h"

@interface Judge : NSObject

@property NSString *name;


- (void)showScore:(Human *)human and:(Robot *)robot;

- (void)judge:(Human *)human and:(Robot *)robot;

- (instancetype)initWithName:(NSString *)name;
@end



Judge.m




#import "Judge.h"

@implementation Judge

- (instancetype)initWithName:(NSString *)name
{
    if (self = [super init])
    {
        _name=name;
    }
    return self;
}


- (void)judge:(Human *)human and:(Robot *)robot
{
    if (human.guessing-robot.guessing==1 || human.guessing-robot.guessing==-1)
    {
        NSLog(@"玩家输了,请继续加油");
        robot.score++;
        
    }else if (human.guessing-robot.guessing==0)
    {
        NSLog(@"你们打平了,真是心有灵犀");
        
    }else
    {
        NSLog(@"你赢了,真厉害");
        human.score++;
    }
    
}


- (void)showScore:(Human *)human and:(Robot *)robot
{
    NSLog(@"比赛很激烈,玩家和电脑比分是 %d : %d",human.score,robot.score);
}
@end




Robot.h

#import "Roles.h"

@interface Robot : Roles

@end


<pre name="code" class="objc">Robot.m




#import "Robot.h"

@implementation Robot

- (void)guess
{
    self.guessing=arc4random_uniform(3)+1;
    NSLog(@"电脑出的是%@",[self change]);
}
@end




Human.h



#import "Roles.h"

@interface Human : Roles

@end



Human.m



#import "Human.h"

@implementation Human

- (void)guess
{
    do
    {
        NSLog(@"请玩家出拳");
        int g;
        rewind(stdin);
        scanf("%d",&g);
        self.guessing = g;
        if (self.guessing!=1 &&self.guessing!=2 &&self.guessing!=3 )
        {
            NSLog(@"输入错误,请重新输入");
        }else
        {
            NSLog(@"玩家出的是%@",[self change]);
            break;
        }
        
    }while (self.guessing!=1 &&self.guessing!=2 &&self.guessing!=3 );
    
}
@end



Roles.h




#import <Foundation/Foundation.h>

typedef enum {shitou=1,jiandao,bu} GuessingEnum;

@interface Roles : NSObject

@property NSString *name;
@property GuessingEnum guessing;
@property int score;

- (NSString *)change;

- (void)guess;

- (instancetype)initWithName:(NSString *)name;
@end




Roles.m




#import "Roles.h"

@implementation Roles

- (instancetype)initWithName:(NSString *)name
{
    if (self = [super init])
    {
        _name=name;
    }
    return self;
}

- (NSString *)change
{
    switch (_guessing)
    {
        case 1:
            return @"✊";
            break;
        case 2:
            return @"✌️";
            break;
        default:
            return @"✋";
            break;
    }
}

- (void)guess
{}
@end



以上便是本次优化的内容吐舌头

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-08-27 00:36  JimmyPeng4iOS  阅读(169)  评论(0编辑  收藏  举报