/** 出拳枚举 */ 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
|