黑马程序员技术交流社区

标题: 面向对象的猜拳游戏[玩家和机器人模块] [打印本页]

作者: wx_byrDDXsc    时间: 2016-4-8 22:52
标题: 面向对象的猜拳游戏[玩家和机器人模块]
/**
出拳枚举
*/
typedef enum
{
    HMFistTypeJianDao = 1,
    HMFistTypeShiTou =2,
    HMFistTypeBu =3
} HMFistType;

//声明玩家类
#import <Foundation/Foundation.h>
#import "HMFistType.h"
@interface HMPlayer : NSObject
{
    @public
    NSString *_name;
    HMFistType _fistType;
    int _score;
}

- (void)showFist;

- (NSString *)fistTypeWithNumber:(int)num;

+ (HMPlayer *)player;

+ (HMPlayer *)playerWithName:(NSString *)name;
@end

#import "HMPlayer.h"

@implementation HMPlayer
- (void)showFist
{

    NSLog(@"请出拳: 1.剪刀 2.石头 3.布");

    int userSelected = 0;
    scanf("%d",&userSelected);

    NSString *type = [self fistTypeWithNumber:userSelected];
    NSLog(@"玩家[%@]出拳是[%@]",_name,type);

    _fistType = userSelected;
}

- (NSString *)fistTypeWithNumber:(int)num
{
    switch (num)
    {
        case 1:
            return @"剪刀";
        case 2:
            return @"石头";
        case 3:
            return @"布";
        default:
            return @"想使诈,你输了.";
    }
}

+ (HMPlayer *)player
{
    HMPlayer *p1 = [HMPlayer new];
    return p1;
}

+ (HMPlayer *)playerWithName:(NSString *)name
{
    HMPlayer *p1 = [HMPlayer new];
    p1->_name = name;
    return p1;
}


@end



#import <Foundation/Foundation.h>
#import "HMFistType.h"
@interface HMRobot : NSObject
{
    @public
    NSString *_name;
    HMFistType _fistType;
    int _score;
}
- (void)showFist;
- (NSString *)fistTypeWithNumber:(int)num;
+ (HMRobot *)robot;
+ (HMRobot *)robotWithName:(NSString *)name;
@end

#import "HMRobot.h"

@implementation HMRobot
- (void)showFist
{
    int robotSelected = arc4random_uniform(3)+1;
    NSString *type = [self fistTypeWithNumber:robotSelected];
    NSLog(@"机器人[%@]出拳是[%@]",_name,type);
    _fistType = robotSelected;
}
- (NSString *)fistTypeWithNumber:(int)num
{
    switch (num)
    {
        case 1:
            return @"剪刀";
        case 2:
            return @"石头";
        case 3:
            return @"布";
        default:
            return @"想使诈,你输了.";
    }

}
+ (HMRobot *)robot
{
    HMRobot *r1 = [HMRobot new];
    return r1;
}
+ (HMRobot *)robotWithName:(NSString *)name
{
    HMRobot *r1 = [HMRobot new];
    r1->_name = name;
    return r1;
}


@end


#import <Foundation/Foundation.h>
#import "HMPlayer.h"
#import "HMRobot.h"
@interface HMJudge : NSObject
{
    @public
    NSString *_name;

}
- (void)caiJueWithPlayer:(HMPlayer *)player andRobot:(HMRobot *)robot;
+ (HMJudge *)judge;
+ (HMJudge *)judgeWithName:(NSString *)name;
@end






作者: 毛小猪    时间: 2016-4-18 23:27
今天刚敲了两遍,再来温习一下
作者: 张立鹏    时间: 2016-4-19 00:12
可以可以,这个游戏还是可以再优化的
作者: chenbin19930916    时间: 2016-4-24 01:29
好人一生平安...




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2