/* 出拳判断 1 - 3 = -2 2 - 1 = 1 3 - 2 = 1 */ #import "HMJudge.h" @implementation HMJudge - (void)caiJueWithPlayer:(HMPlayer *)player andRobot:(HMRobot *)robot {
int playerFist = player->_fistType; int robotFist = robot->_fistType; NSLog(@"裁判[%@]宣布:",_name); if (playerFist - robotFist == -2 || playerFist - robotFist == 1) { player->_score++; NSLog(@"恭喜玩家[%@],你赢了!",player->_name); } else if (playerFist == robotFist) { NSLog(@"玩家[%@]和机器人[%@]平局.",player->_name,robot->_name); } else { robot->_score++; NSLog(@"机器人[%@]赢了!",robot->_name); } NSLog(@"---玩家[%@]得分是[%d]---机器人[%@]得分是[%d]---", player->_name, player->_score, robot->_name, robot->_score ); NSLog(@"玩家[%@],你还想再玩一次吗?y/n",player->_name);
} + (HMJudge *)judge { HMJudge *j1 = [HMJudge new]; return j1; } + (HMJudge *)judgeWithName:(NSString *)name { HMJudge *j1 = [HMJudge new]; j1->_name = name; return j1; }
@end
//主函数 #import <Foundation/Foundation.h> #import "HMRobot.h" #import "HMPlayer.h" #import "HMJudge.h" int main(int argc, const char * argv[]) { //用类方法创建玩家类对象 HMPlayer *p1 = [HMPlayer playerWithName:@"小明"]; //用类方法创建机器人类对象 HMRobot *r1 = [HMRobot robotWithName:@"阿法狗"]; //用类方法创建裁判类对象 HMJudge *j1 = [HMJudge judgeWithName:@"黑哨"]; //循环多次游戏 while (1) { //调用玩家对象出拳方法 [p1 showFist]; //调用机器人对象出拳方法 [r1 showFist]; //调用裁判对象裁决方法 [j1 caiJueWithPlayer:p1 andRobot:r1]; //让用户选择是否继续游戏 char ch = 'a'; rewind(stdin); scanf("%c",&ch); if (ch != 'y') { NSLog(@"你是怕了么.再见吧."); break; } } return 0;
}
|