OC学习--<猜拳游戏> 之 通过面向对象思想实现 2.0版本
2015-08-21 22:19
513 查看
对游戏进行了升级, 多加入了一个第三方的类来进行结果的判断,这样子每个类就执行他们职责相关的东西,不会出现方法的混乱,思路更加清晰,并且对玩家的非法输入进行了处理,修复了一些bug.
main.m // 猜拳游戏2.0 #import <Foundation/Foundation.h> #import "Judge.h" int main() { //分别创建3个对象用于调用方法 Human *human1 = [Human new]; //用户可以在此定义自己名字 human1.playerName=@"小花花"; Robot *robot1 = [Robot new]; //定义电脑名字 robot1.robotName=@"大白"; Judge *judge1 = [Judge new]; judge1.judgeName = @"铁面判官-大黑哨��"; //游戏正式开始 [judge1 start:human1 and:robot1]; //循环控制比赛进行 char judge; while (1) { if (judge=='n') { NSLog(@"欢迎你下次再来!��"); break; } [human1 guess]; [robot1 guess]; [judge1 judgeWith:human1 and:robot1]; [judge1 showScore:human1 and:robot1]; rewind(stdin); scanf("%c",&judge); } return 0; }
EnumGuessing.h /** 定义一个枚举类型,石头剪刀布分别为1,2,3 */ typedef enum{kGuessingShitou =1,kGuessingJiandao,kGuessingBu} GuessingEnum;
Human.h #import <Foundation/Foundation.h> #import "EnumGuessing.h" /** * * 定义一个玩家类 */ @interface Human : NSObject @property NSString * playerName; @property int playerScore; @property GuessingEnum playerGuess; /** * * 玩家出拳的方法 */ - (void)guess; /** * 把枚举类型结果转换成相应字符串输出的方法 * * @param guess 随机出拳的结果 * * @return 返回相应字符串 */ - (NSString *)changeGuessWith:(GuessingEnum)guess; @end
Human.m #import "Human.h" @implementation Human //玩家出拳的方法 - (void)guess { LOOP: NSLog(@"[%@]请出招~! 1.✊ 2.✌️ 3.✋",_playerName); rewind(stdin); scanf("%i",&_playerGuess); if (_playerGuess!=1 && _playerGuess!=2 && _playerGuess!=3) { NSLog(@"输入错误,请重新输入"); goto LOOP; } NSLog(@"[%@]出的是%@",_playerName,[self changeGuessWith:_playerGuess]); } //把枚举类型结果转换成相应字符串输出的方法 - (NSString *)changeGuessWith:(GuessingEnum)guess; { switch (guess) { case 1: return @"✊"; break; case 2: return @"✌️"; break; case 3: return @"✋"; break; } } @end
Judge.h #import <Foundation/Foundation.h> #import "Robot.h" #import "Human.h" #import "EnumGuessing.h" /** * * 定义一个裁判类 */ @interface Judge : NSObject @property NSString * judgeName; /** * * 宣布比赛开始的方法 * * @param human 玩家 * @param robot 机器人 */ - (void)start:(Human *)human and:(Robot *)robot; /** * * 展示比分的方法 * * @param human 玩家 * @param robot 机器人 */ - (void)showScore:(Human *)human and:(Robot *)robot; /** * * 判断输赢并加分的方法 * * @param human 玩家 * @param robot 机器人 */ - (void)judgeWith:(Human *)human and:(Robot *)robot; @end
Judge.m #import "Judge.h" @implementation Judge //宣布比赛开始的方法 - (void)start:(Human *)human and:(Robot *)robot { NSLog(@"叮叮叮~! 猜拳比赛正式开始~! \n 我是裁判[%@]! 现在, 有请双方运动员[%@]和[%@]入场",_judgeName,human.playerName,robot.robotName); } //展示比分的方法 - (void)showScore:(Human *)human and:(Robot *)robot { NSLog(@"比赛很激烈,现在[%@]和[%@]的比分是 %d:%d ",human.playerName,robot.robotName,human.playerScore,robot.robotScore); NSLog(@"亲,还要继续比赛嘛?y/n"); } //判断输赢并加分的方法 - (void)judgeWith:(Human *)human and:(Robot *)robot { if (human.playerGuess - robot.robotGuess ==-1 || human.playerGuess - robot.robotGuess == 2) { NSLog(@"恭喜~![%@]赢了",human.playerName); (human.playerScore) ++; } else if (human.playerGuess - robot.robotGuess ==0) { NSLog(@"你们真是心有灵犀,打平了!"); } else { NSLog(@"恭喜~![%@]赢了",robot.robotName); (robot.robotScore) a828 ++; } } @end
Robot.h #import <Foundation/Foundation.h> #import "EnumGuessing.h" /** * * 定义一个机器人类 */ @interface Robot : NSObject @property NSString * robotName; @property int robotScore; @property GuessingEnum robotGuess; /** * * 机器人随机出拳的方法 */ - (void)guess; /** * 把枚举类型结果转换成相应字符串输出的方法 * * @param guess 随机出拳的结果 * * @return 返回相应字符串 */ - (NSString *)changeGuessWith:(GuessingEnum)guess; @end
Robot.m #import "Robot.h" @implementation Robot //机器人随机出拳的方法 - (void)guess { _robotGuess=arc4random_uniform(3)+1; NSLog(@"[%@]出的是%@",_robotName,[self changeGuessWith:_robotGuess]); } //把枚举类型结果转换成相应字符串输出的方法 - (NSString *)changeGuessWith:(GuessingEnum)guess; { switch (guess) { case 1: return @"✊"; break; case 2: return @"✌️"; break; case 3: return @"✋"; break; } } @end
相关文章推荐
- 我是运营,我没有假期
- 每个 Linux 游戏玩家都绝不想要的恼人体验
- 在 Fedora 上使用 Steam play 和 Proton 来玩 Windows 游戏
- Steam 让我们在 Linux 上玩 Windows 的游戏更加容易
- 如何使用 Steam Play 在 Linux 上玩仅限 Windows 的游戏
- 新一代iPad适配应用之游戏篇
- VB实现的《QQ美女找茬游戏》作弊器实例
- Lua编程示例(二):面向对象、metatable对表进行扩展
- C#中面向对象编程机制之多态学习笔记
- 浅谈Lua的面向对象特性
- Lua面向对象之类和继承浅析
- JavaScript面向对象的两种书写方法以及差别
- 浅谈c# 面向对象之类与对象
- C#面向对象特征的具体实现及作用详解
- C# 面向对象的基本原则
- 浅谈对c# 面向对象的理解
- C#实现洗牌游戏实例
- C#实现的算24点游戏算法实例分析
- C#实现简单的井字游戏实例
- C++编写简单的打靶游戏