OC学習--対象思想による実現


まず大体の需要を見て、それから自分の考えによって1つのプロジェクトを書いて、主に人のクラスとロボットの2つのクラスを作成することを通じて、対象に向かう思想で1つの小さいプロジェクトを書いて、プレイヤーに拳を出すことを提示して、それからロボットはランダムに拳を出して、勝負を判断してスコアを記録する機能を実現します;まず本編ではbetaバージョンを記録します.
#import 

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

@interface Human : NSObject
{
    @public
    NSString *_name;
    int _fisttype;
    int _score;
}


-(void)fistChuWith:(Robot *)robot;

- (NSString *)zhuanHuan:(int)fistType;

- (void)biJiao:(Robot *)robot;

@end

@implementation Human

-(void)fistChuWith:(Robot *)robot;
{
    char yesOrNo='y';
    NSLog(@"   ~~![%@]   [%@]      ~!!",_name,robot->_name);
    while(yesOrNo=='y')
    {
        NSLog(@"[%@],   ! 1.    2.    3. ",_name);
        scanf("%d",&_fisttype);
        NSLog(@"[%@]   %@",_name,[self zhuanHuan:(_fisttype)]);
        [self biJiao:robot];
        NSLog(@" ,     ?y/n?");
        rewind(stdin);
        scanf("%c",&yesOrNo);
        rewind(stdin);
    }
    if (yesOrNo=='n')
    {
        NSLog(@"       !��");
    }

}

- (NSString *)zhuanHuan:(int)fistType
{
    switch (fistType)
    {
        case 1:
            return @"  "    ;
            break;
        case 2:
            return @"  "    ;
            break;
        case 3:
            return @" "    ;
            break;
        default:
            return @"  ";
            break;
    }
}

- (void)biJiao:(Robot *)robot
{
    [robot fistChu];
    NSLog(@"[%@]    %@",_name,[self zhuanHuan:(robot->_fistType)]);

    if (robot->_fistType - _fisttype == -1 || robot->_fistType - _fisttype == 2 )
    {
        NSLog(@"[%@]  ",_name);
        (robot->_score) ++;
    }
    else if (robot->_fistType - _fisttype == 0 )
    {
        NSLog(@"     ");
    }else
    {
        NSLog(@"[%@]  ",robot->_name);
        _score++;
    }

    NSLog(@"[%@] [%@]     %d:%d",_name,robot->_name,_score,robot->_score);
}
@end

@interface Robot : NSObject

{
    @public
    NSString *_name;
    int _fistType;
    int _score;
}

- (void)fistChu;

@end

@implementation Robot

- (void)fistChu
{
    _fistType = arc4random_uniform(3)+1;
}

@end

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        Human *h1 = [Human new];
        Robot *r1 = [Robot new];

        h1->_name = @"  ";
        r1->_name = @"  ";

        [h1 fistChuWith:r1];

    }
    return 0;
}

しかし、このような不足点は、考え方がはっきりしていないことであり、人のじゃんけんを呼び出した行為だけでこれらの行為をすべてやり終え、じゃんけんの人ではなく、第三者のクラスを加えて結果を判断し、発表しなければならない.そのため、次の改善はこの方面である.