本帖最后由 僞誰學ザ乖 于 2016-4-13 23:04 编辑
//首先创建一个枚举的头文件 HMCaiQuanType.h
- //使用枚举定义猜拳的种类
- //1.剪刀, 2.石头, 3.布
- typedef enum{
- //定义剪刀
- chuQuanJianDao = 1,
- //定义石头
- chuQuanShiTou = 2,
- //定义布
- chuQuanBu = 3
-
- }chuQuanType;
- //创建一个人类文件 .h 写上人类的声明 HMPerson.h
- #import <Foundation/Foundation.h>
- #import "HMCaiQuanType.h"
- @interface HMPerson : NSObject
- {
- @public
- //玩家的姓名.
- NSString *_name;
- //玩家的得分.
- int _score;
- //玩家的出拳.
- chuQuanType _playerType;
-
- }
- - (void)showType;
- - (NSString *)chuQuanWithNum:(int)num;
- @end
- //在创建一个 .m文件写人类的实现 HMPerson.m
- #import "HMPerson.h"
- @implementation HMPerson
- - (void)showType
- {
- //提示玩家输入
- //接收玩家输入的出拳.
- int playerChuQuan = 0;
- do{
- NSLog(@"亲爱的玩家[%@],请输入你的出拳:1.剪刀,2.石头,3.布",_name);
- scanf("%d",&playerChuQuan);
-
- }while (playerChuQuan > 3 || playerChuQuan < 0);//如果输入的是其他的数据,则重新输入.
-
- NSString *Type = [self chuQuanWithNum:playerChuQuan];
- NSLog(@"玩家[%@]出拳是%@.",_name,Type);
- //将玩家输入的结果保存到玩家的属性
- _playerType = playerChuQuan;
-
- }
- - (NSString *)chuQuanWithNum:(int)num
- {
- switch (num) {
- case 1:
- return @"剪刀";
- case 2:
- return @"石头";
- default:
- return @"布";
- }
-
-
- }
- @end
- //创建一个 .h 机器人声明头文件 HMRobot.h
- #import <Foundation/Foundation.h>
- #import "HMCaiQuanType.h"
- @interface HMRobot : NSObject
- //机器人的属性
- {
- @public
- //机器人的姓名
- NSString *_name;
- //机器人的得分
- int _score;
- //机器人的出拳
- chuQuanType _robotChuQuan;
-
- }
- - (void)showType;
- - (NSString *)chuQuanWithNum:(int)num;
- @end
- //创建一个 .m 机器人的实现 HMRobot.m
- #import "HMRobot.h"
- #import <stdlib.h>
- @implementation HMRobot
- - (void)showType
- {
- //机器人随机出拳
- int res = arc4random_uniform(3)+1;
- //显示随机输出的拳头
- NSString *type = [self chuQuanWithNum:res];
- NSLog(@"机器人[%@]出拳是%@",_name,type);
- //将机器人出拳保存在机器人的属性之中.
- _robotChuQuan = res;
- }
- - (NSString *)chuQuanWithNum:(int)num
- {
- switch (num) {
- case 1:
- return @"剪刀";
- case 2:
- return @"石头";
- default:
- return @"布";
- }
-
- }
- @end
- //创建一个裁判 .h 的声明文件 HMJudge.h
- #import <Foundation/Foundation.h>
- #import "HMPerson.h"
- #import "HMRobot.h"
- @interface HMJudge : NSObject
- //裁判的属性.
- {
- @public
- //裁判的姓名
- NSString *_name;
- }
- //裁判判断玩家和机器人的输赢
- - (void)panDuanWithPlayer:(HMPerson *)player :(HMRobot *)robot;
- @end
- //创建一个 .m 的裁判实现文件 HMJudge.m
- #import "HMJudge.h"
- @implementation HMJudge
- - (void)panDuanWithPlayer:(HMPerson *)player :(HMRobot *)robot
- {
- //先拿到玩家和机器人出的拳头
- chuQuanType playerType = player->_playerType;
- chuQuanType robotType = robot->_robotChuQuan;
- //判断输赢,指定胜利的对象,显示结果
- //剪刀 1
- //石头 2
- //布 3
- // 1 3 -2
- //2 1 1
- //3 2 1
- NSLog(@"我是裁判[%@],我来宣布比赛结果.",_name);
- //玩家胜利
- if (playerType - robotType == -2 || playerType - robotType == 1) {
- NSLog(@"恭喜玩家[%@],取得胜利.",player->_name);
- player->_score++;
- }else if(playerType == robotType){
- //平局
- NSLog(@"这一局是平局.");
- }
- else{
- //机器人胜利
- NSLog(@"机器人[%@]取得胜利",robot->_name);
- robot->_score++;
- }
- //显示得分
- NSLog(@"玩家[%@]:%d--------机器人[%@]:%d",
- player->_name,
- player->_score,
- robot->_name,
- robot->_score);
- }
- @end
- //在 main.m 中写上实现
- #import <Foundation/Foundation.h>
- #import "HMJudge.h"
- int main(){
- HMPerson *xiaoming = [HMPerson new];
- xiaoming->_name = @"小明";
- HMRobot *jiQiRen = [HMRobot new];
- jiQiRen->_name = @"呵呵";
- HMJudge *caiPan = [HMJudge new];
- caiPan->_name = @"犀利哥";
- while(1)
- {
- [xiaoming showType];
- [jiQiRen showType];
- //判断他们出的拳头
- [caiPan panDuanWithPlayer:xiaoming :jiQiRen];
- NSLog(@"客官,玩的怎么样,想不想再来一次?y/n:");
- char a = 'a';
- rewind(stdin);//清理缓存区
- scanf("%c",&a);
- if (a != 'y') {
- NSLog(@"客官,欢迎下次再来玩哦!");
- break;
- }
- }
- return 0;
- }
复制代码
|