OC学习--<猜拳游戏> 之 通过面向对象思想实现 2.0版本

对游戏进行了升级, 多加入了一个第三方的类来进行结果的判断,这样子每个类就执行他们职责相关的东西,不会出现方法的混乱,思路更加清晰,并且对玩家的非法输入进行了处理,修复了一些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) ++;
    }
}

@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

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-08-21 22:19  JimmyPeng4iOS  阅读(184)  评论(0编辑  收藏  举报