typdef enum{
FistTypeJiandao=1,
FistTypeShitou=2,
FistTypeBu=3,
}FistType;
#import <Foundation/Foundation.h>
@interface Player : NSObject
{
@public
NSString *_name;
int _score;
FistType _selectedType;
}
-(void)showFist;
-(NSString *)fistTypeWithNumber:(int)number;
@end
@implementation Player
-(void)showFist{
NSLog(@"亲爱的玩家[%@],请选择你要出的拳头,1见到,2石头,3布",_name);
int userSelect=0;
scanf("%d",&userSelect);
NSString *fistType=[self fistTypeWithNumber:userSelect];
NSLog(@"玩家[%@]出的拳头是:%@",_name,fistType);
_selectedType=userSelect;
}
-(NSString *)fistTypeWithNumber:(int)number{
switch(number){
case 1:
return @"剪刀";
case 2:
return @"石头";
case 3:
return @"布";
}
return @"";
}
@end
@interface Robot:NSObject
{
@public
NSString *_name;
int _score;
FistType _selectedType;
}
-(void)showFist;
-(NSString *)fistTypeWithNumber:(int)number;
@end
@implementation Robot
-(void)showFist{
int robotSelect=arc4random_uniform(3)+1;
NSString *type=[self fistTypeWithNumber:robotSelect];
NSLog(@"机器人[%@]出的拳头是:%@",_name,type);
_selectedType=robotSelect;
}
-(NSString *)fistTypeWithNumber:(int)number{
switch(number){
case 1:
return @"剪刀";
case 2:
return @"石头";
case 3:
return @"布";
}
return @"";
}
@end
@interface Judge:NSObject
{
@public
NSString *_name;
}
-(void)caiJueWithPlay:(Player *)player andRobot:(Robot *) robot;
@end
@implementation Judge
-(void)caiJueWithPlay:(Player *)player andRobot:(Robot *) robot{
FistType playerType=player->_selectedType;
FistType robotType=robot->_selectedType;
if(playerType-robotType==-2||playerType-robotType==1){
NSLog(@"恭喜玩家[%@]取得了胜利。",player->_name);
player->_score++;
}else if(playerType==robotType){
NSLog(@"[%@]、[%@],你们真是心有灵犀 一点通啊!",player->_name,robot->_name);
}else{
NSLog(@"恭喜玩家[%@]取得了胜利。",robot->_name);
robot->_score++;
}
NSLog(@"--玩家[%@]:[%d]-------机器人[%@]:[%d]",player->_name,player->_score,robot->_name,robot->_score);
}
@end
int main(int argc,const char * argv[]){
Player *player=[Player new];
player->_name=@"小明";
Robot *robot=[Robot new];
robot->_name=@"阿尔法狗";
Judge *judge=[Judge new];
judge->_name=@"黑哨";
while(1){
[player showFist];
[robot showFist];
[judge caiJueWithPlay:player andRobot:robot];
NSLog(@"请问还要继续吗?y/n");
char ans='a';
rewind(stdin);
scanf("%c",&ans);
if(ans!='y'){
NSLog(@"欢迎 下次来玩。");
break;
}
}
return 0;
}