A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© muyan091115 中级黑马   /  2016-5-24 23:37  /  527 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一共8个文件

main。m
  1. #import <Foundation/Foundation.h>
  2. #import "Judge.h"

  3. int main(int argc, const char * argv[]) {
  4.     @autoreleasepool {
  5.         
  6.         Player *player = [Player new];
  7.         player->_name = @"王瑶";
  8.         player->_score = 0;
  9.         Robot *robot = [Robot new];
  10.         robot->_name = @"呆瓜";
  11.         robot->_score = 0;
  12.         Judge *judge = [Judge new];
  13.         judge->_name = @"黑哨";
  14.         
  15.         while (1) {
  16.             [player show];
  17.             [robot show];
  18.             [judge show];
  19.             [judge caiJueWithPlayer :player andRobot:robot];
  20.         }
  21.     }
  22.     return 0;
  23. }

复制代码


Player。h
  1. #import <Foundation/Foundation.h>
  2. #import "Select.h"

  3. @interface Player : NSObject{
  4.     @public
  5.     NSString *_name;
  6.     Select _select;
  7.     int _score;
  8. }
  9. - (void)show;
  10. - (NSString *)showSelectWith :(Select)select;
  11. @end
复制代码


Player。m
  1. #import "Player.h"

  2. @implementation Player
  3. - (void)show{
  4.     NSLog(@"\n亲爱的玩家[%@],请选择你要出的拳头 1,剪刀 2,石头 3,布:",_name);
  5.     scanf("%d",&_select);
  6.     while (_select<1 || _select>3) {
  7.         NSLog(@"请重新输入:");
  8.         scanf("%d",&_select);
  9.     }
  10.     NSLog(@"\n玩家[%@]出的拳头是:%@",_name,[self showSelectWith :_select]);
  11. }
  12. - (NSString *)showSelectWith :(Select)select{
  13.     if(select == 1){
  14.         return @"剪刀";
  15.     }else if (select == 2){
  16.         return @"石头";
  17.     }else{
  18.         return @"布";
  19.     }
  20. }
  21. @end
复制代码


Robot。h
  1. #import <Foundation/Foundation.h>
  2. #import "Select.h"
  3. #import "stdlib.h"

  4. @interface Robot : NSObject{
  5.     @public
  6.     NSString *_name;
  7.     Select _select;
  8.     int _score;
  9. }
  10. - (void)show;
  11. - (NSString *)showWithSelect :(Select)select;
  12. @end
复制代码


Robot。m
  1. #import "Robot.h"

  2. @implementation Robot
  3. - (void)show{
  4.     _select = arc4random_uniform(3)+1;
  5.     NSLog(@"\n机器人[%@]出的拳头是:%@",_name,[self showWithSelect :_select]);
  6. }
  7. - (NSString *)showWithSelect:(Select)select {
  8.     if(select == 1){
  9.         return @"剪刀";
  10.     }else if (select == 2){
  11.         return @"石头";
  12.     }else{
  13.         return @"布";
  14.     }
  15. }
  16. @end
复制代码

1 个回复

倒序浏览
Judge。h

  1. #import <Foundation/Foundation.h>
  2. #import "Player.h"
  3. #import "Robot.h"

  4. @interface Judge : NSObject{
  5.     @public
  6.     NSString *_name;
  7. }
  8. - (void)show;
  9. - (void)caiJueWithPlayer :(Player *)player andRobot:(Robot *)robot;
  10. @end
复制代码


judge。m

  1. #import "Judge.h"

  2. @implementation Judge
  3. - (void)show{
  4.     NSLog(@"\n我是裁判[%@],我来宣布结果!",_name);
  5. }
  6. - (void)caiJueWithPlayer :(Player *)player andRobot:(Robot *)robot{
  7.     if (player->_select - robot->_select == -2 || player->_select - robot->_select == 1) {
  8.         NSLog(@"恭喜选手[%@]获得胜利!",player->_name);
  9.         player->_score++;
  10.         NSLog(@"目前比分 : [%@]%d : [%@]%d",
  11.               player->_name,player->_score,robot->_name,robot->_score);
  12.     }else if (player->_select == robot->_select){
  13.         NSLog(@"平局");
  14.         NSLog(@"目前比分 : [%@]%d : [%@]%d",
  15.               player->_name,player->_score,robot->_name,robot->_score);
  16.     }else{
  17.         NSLog(@"恭喜机器人[%@]获得胜利!",robot->_name);
  18.         robot->_score++;
  19.         NSLog(@"目前比分 : [%@]%d : [%@]%d",
  20.               player->_name,player->_score,robot->_name,robot->_score);

  21.     }
  22. }
  23. @end
复制代码


select。h

  1. typedef enum{
  2.     SelectJianDao = 1,
  3.     SelectShiTou = 2,
  4.     SelectBu = 3
  5. } Select ;
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马