三国英雄对战——控制台输出模拟

MOSAD_HW1

UML类图

三国英雄对战——控制台输出模拟_第1张图片

类的设计及其实现

Skill——技能类

参照现代游戏的设计,技能可分为伤害类技能恢复类技能,因此变量设计为

  • 技能标识
    • 名字
  • 施放代价
    • 法力消耗
    • 血量消耗
  • 施放效果
    • 法力恢复
    • 血量恢复
    • 伤害值

这个类只需要一个构造函数,用于初始化这个技能的六个信息即可。由于只是简单的赋值语句,因此不再赘述。

Hero——英雄类

英雄类依赖于技能类,即一个英雄可以拥有多个技能,因此英雄类会有一个技能类数组来维护一个英雄的技能信息。除此之外,一个英雄除了技能外还有其他信息,因此成员变量设计如下:

  • 国家
  • 初始法力值
  • 初始血量
  • 姓名
  • 技能数量
  • 技能数组

同理可利用一个简单的构造函数来初始化这些信息,其中数组的初始化可以采用以下方式:

self->skills = [[NSArray alloc]initWithObjects:s1, s2, nil];

其中s1,s2均是指向一个技能的指针。这里采用指针的原因是尚未摸清楚objc中类似于C++的引用变量的使用方式,为了避免过多的内存使用,因此采用指针的方式。

由于英雄需要选择另一名英雄来单挑,因此英雄类应该有一个成员函数来完成单挑的过程并输出单挑过程中的关键结果。因此设计了一个方法:

- (void)PKOneUnit:(Hero *)vs {
    int rand_num = arc4random() % 2;        //随机选择两个技能中的一个
    
    Skill* current_skill = self->skills[rand_num];
    
    if Current skill can be launched {
        ...
        Console output information
        Hero attribute changes after maintenance skill is launched
        ...
    }
    else {
        ...
        Output the information that the skill cannot activate
        the hero attribute does not change
        ...
    }
    
    The console outputs the key attributes of each hero in each round.

主函数调用

主函数的设计较为简单,主要思路是利用随机数选择英雄,然后最多循环10个回合,在每个回合中依次调用每个英雄的PKOneUnit方法,直到某一方血量降到0或0以下或者超过10个回合仍然没有分出胜负,循环跳出。

Hero* h1 = getHero(rand1);  //getHero方法是通过随机数返回对应的英雄实例
Hero* h2 = getHero(rand2);  //这里在取rand2时保证 rand2!=rand1

while (双方血量大于0 && 回合数小于10) {
  [h1 PKOneUnit:h2];
  [h2 PKOneUnit:h1];
}

...
控制台输出胜负信息
...

运行结果

三国英雄对战——控制台输出模拟_第2张图片

项目心得

本次项目是对objc的简单应用,由于有了大一C++的基础,理解起来并不是特别困难,总结主要不同点如下:

  • 类的声明与实现
    • C++中类的声明为class,实现时可以在类外使用作用域符 :: 来告诉编译器是哪个类的方法
    • objc中声明为 @interface@end 中间的代码块,而实现时通过 @implementation@end 的代码块来实现
  • 方法的声明
    • C++方法的声明为Type function(Type1 arg1, ...)
    • objc方法的声明为(Type) function: (Type1) arg1 ...
  • 方法的调用
    • C++中通过指针->方法的形式调用
    • objc中通过[指针 方法]的形式调用

除此之外C++和objc大同小异。总的来说,这一次作业让我加深了对上课objc语法点和面向对象设计思想的理解,期待下一次更有意思的作业!

附录:源代码

main.m

#import 
#import "Hero.h"

Hero* getHero(int rand_num) {
    
    switch (rand_num) {
        case 0:
            return [ZhangFei new];
            break;
        case 1:
            return [JiaWen new];
            break;
        case 2:
            return [GaiLun new];
            break;
        case 3:
            return [YaSuo new];
            break;
        case 4:
            return [LiBai new];
            break;
        case 5:
            return [LiQing new];
            break;
        case 6:
            return [HuaMuLan new];
            break;
        case 7:
            return [LvDongBin new];
            break;
        case 8:
            return [GuanYu new];
            break;
        case 9:
            return [ZhaoYun new];
            break;
        case 10:
            return [DeLaiESi new];
            break;
        default:
            break;
    }
    
    return nil;
}

int main(int argc, char * argv[]) {
    @autoreleasepool {
        
        const int hero_num = 11;
        int rand1, rand2, total = 0;
        rand1 = arc4random() % hero_num;
        do {
            rand2 = arc4random() % hero_num;
        }while (rand1 == rand2);
        Hero* h1 = getHero(rand1);
        Hero* h2 = getHero(rand2);
        
        printf("游戏开始!\n%s vs %s\n", [h1->name UTF8String], [h2->name UTF8String]);
        
        while (++total <= 10) {
            printf("\n----------当前为第%i回合---------\n", total);
            [h1 PKOneUnit:h2];
            if (h2->blood_value <= 0) {
                printf("\n游戏结束, %s获胜!", [h1->name UTF8String]);
                break;
            }
            [h2 PKOneUnit:h1];
            if (h1->blood_value <= 0) {
                printf("\n游戏结束, %s获胜!", [h2->name UTF8String]);
                break;
            }
            
        }
        
        if (total > 10) {
            printf("\n游戏结束,无人出局!");
        }
    }
}

Hero.h

#ifndef Hero_h
#define Hero_h

#import 

@interface Skill : NSObject {
    @public
    NSInteger mana_cost;
    NSInteger blood_cost;
    NSInteger power;
    NSInteger mana_regen;
    NSInteger blood_regen;
    NSString* name;
}

- (id) initSkill: (NSString*) _name
       mana_cost: (NSInteger) _mana_cost
      blood_cost: (NSInteger) _blood_cost
           power: (NSInteger) _power
      mana_regen: (NSInteger) _mana_regen
     blood_regen: (NSInteger) _blood_regen;
@end

@interface Hero : NSObject {
    @public
    NSString* country;
    NSInteger blood_value;
    NSInteger energy_value;
    NSString* name;
    NSInteger skill_num;
    NSArray* skills;
}

- (void)PKOneUnit: (Hero*) vs;
- (NSInteger)getBlood_value;
- (NSInteger)getEnergy_value;
- (NSString *)getCountry;
- (NSString *)getName;

@end

@interface ZhangFei: Hero {
    
}

@end

@interface ZhaoYun: Hero {
    
}

@end

@interface JiaWen: Hero {
    
}

@end

@interface GaiLun: Hero {
    
}

@end

@interface YaSuo: Hero {
    
}

@end

@interface LiQing: Hero {
    
}

@end

@interface HuaMuLan: Hero {
    
}

@end

@interface LiBai: Hero {
    
}

@end

@interface LvDongBin: Hero {
    
}

@end

@interface GuanYu: Hero {
    
}

@end

@interface DeLaiESi: Hero {
    
}

@end

#endif /* Header_h */

Hero.m

#import 
#import "Hero.h"

@implementation Skill
- (id) initSkill: (NSString*) _name
       mana_cost: (NSInteger) _mana_cost
      blood_cost: (NSInteger) _blood_cost
           power: (NSInteger) _power
      mana_regen: (NSInteger) _mana_regen
     blood_regen: (NSInteger) _blood_regen {
    self = [super init];
    if (self) {
        self->mana_cost = _mana_cost;
        self->blood_cost = _blood_cost;
        self->power = _power;
        self->mana_regen = _mana_regen;
        self->blood_regen = _blood_regen;
        self->name = _name;
    }
    
    return self;
}

@end

@implementation Hero
- (NSInteger)getBlood_value {
    return blood_value;
}

- (NSInteger)getEnergy_value {
    return energy_value;
}

- (NSString *)getCountry {
    return country;
}

- (NSString *)getName {
    return name;
}

- (void)PKOneUnit:(Hero *)vs {
    int rand_num = arc4random() % 2;
    
    Skill* now_skill = self->skills[rand_num];
    
    if (self->energy_value > now_skill->mana_cost && self->blood_value > now_skill->blood_cost) {
        printf("\n%s发动技能: %s\n对%s造成%li点伤害\n", [[self getName] UTF8String], [now_skill->name UTF8String], [vs->name UTF8String], now_skill->power);
        vs->blood_value -= now_skill->power;
        self->energy_value -= now_skill->mana_cost;
        self->energy_value += now_skill->mana_regen;
        self->blood_value -= now_skill->blood_cost;
        self->blood_value += now_skill->blood_regen;
    
        
    }
    else {
        printf("\n%s无法发动当前技能: %s\n", [[self getName] UTF8String], [now_skill->name UTF8String]);
    }
    
    printf("\n%s剩余血量: %li\n剩余蓝量: %li\n\n%s剩余血量: %li\n剩余蓝量: %li\n", [self->name UTF8String], (long)self->blood_value, (long)self->energy_value, [vs->name UTF8String], (long)vs->blood_value, (long)vs->energy_value);
    
}
@end

@implementation ZhangFei

-(id) init {
    self = [super init];
    
    if (self) {
        self->name = @"张飞";
        self->country = @"蜀国";
        self->blood_value = 20;
        self->energy_value = 20;
        self->skill_num = 2;
        Skill* s1 = [[Skill alloc]initSkill: @"肉弹冲击" mana_cost:2 blood_cost:0 power:2 mana_regen:0 blood_regen:0];
        Skill* s2 = [[Skill alloc]initSkill: @"醉酒狂暴" mana_cost:2 blood_cost:0 power:0 mana_regen:0 blood_regen:2];
        self->skills = [[NSArray alloc]initWithObjects:s1, s2, nil];
        
    }
    
    return self;
}
@end

@implementation ZhaoYun

-(id) init {
    self = [super init];
    
    if (self) {
        self->name = @"赵云";
        self->country = @"蜀国";
        self->blood_value = 20;
        self->energy_value = 20;
        self->skill_num = 2;
        Skill* s1 = [[Skill alloc]initSkill: @"三重爪击" mana_cost:2 blood_cost:0 power:2 mana_regen:0 blood_regen:0];
        Skill* s2 = [[Skill alloc]initSkill: @"长枪依在" mana_cost:2 blood_cost:0 power:3 mana_regen:0 blood_regen:0];
        self->skills = [[NSArray alloc]initWithObjects:s1, s2, nil];
        
    }
    
    return self;
}
@end

@implementation JiaWen

-(id) init {
    self = [super init];
    
    if (self) {
        self->name = @"嘉文";
        self->country = @"蜀国";
        self->blood_value = 20;
        self->energy_value = 20;
        self->skill_num = 2;
        Skill* s1 = [[Skill alloc]initSkill: @"巨龙撞击" mana_cost:2 blood_cost:0 power:2 mana_regen:0 blood_regen:0];
        Skill* s2 = [[Skill alloc]initSkill: @"天崩地裂" mana_cost:2 blood_cost:0 power:3 mana_regen:0 blood_regen:0];
        self->skills = [[NSArray alloc]initWithObjects:s1, s2, nil];
        
    }
    
    return self;
}
@end

@implementation GaiLun

-(id) init {
    self = [super init];
    
    if (self) {
        self->name = @"盖伦";
        self->country = @"蜀国";
        self->blood_value = 20;
        self->energy_value = 20;
        self->skill_num = 2;
        Skill* s1 = [[Skill alloc]initSkill: @"致命打击" mana_cost:2 blood_cost:0 power:2 mana_regen:0 blood_regen:0];
        Skill* s2 = [[Skill alloc]initSkill: @"德玛西亚正义" mana_cost:2 blood_cost:0 power:4 mana_regen:0 blood_regen:0];
        self->skills = [[NSArray alloc]initWithObjects:s1, s2, nil];
        
    }
    
    return self;
}
@end

@implementation YaSuo

-(id) init {
    self = [super init];
    
    if (self) {
        self->name = @"亚索";
        self->country = @"蜀国";
        self->blood_value = 20;
        self->energy_value = 20;
        self->skill_num = 2;
        Skill* s1 = [[Skill alloc]initSkill: @"斩钢闪" mana_cost:2 blood_cost:0 power:3 mana_regen:0 blood_regen:0];
        Skill* s2 = [[Skill alloc]initSkill: @"狂风绝息斩" mana_cost:2 blood_cost:0 power:5 mana_regen:0 blood_regen:0];
        self->skills = [[NSArray alloc]initWithObjects:s1, s2, nil];
        
    }
    
    return self;
}
@end

@implementation LiBai

-(id) init {
    self = [super init];
    
    if (self) {
        self->name = @"李白";
        self->country = @"蜀国";
        self->blood_value = 20;
        self->energy_value = 20;
        self->skill_num = 2;
        Skill* s1 = [[Skill alloc]initSkill: @"侠客行" mana_cost:2 blood_cost:0 power:2 mana_regen:0 blood_regen:0];
        Skill* s2 = [[Skill alloc]initSkill: @"三尺青锋" mana_cost:2 blood_cost:0 power:3 mana_regen:0 blood_regen:0];
        self->skills = [[NSArray alloc]initWithObjects:s1, s2, nil];
        
    }
    
    return self;
}
@end

@implementation LiQing

-(id) init {
    self = [super init];
    
    if (self) {
        self->name = @"李青";
        self->country = @"蜀国";
        self->blood_value = 20;
        self->energy_value = 20;
        self->skill_num = 2;
        Skill* s1 = [[Skill alloc]initSkill: @"天音波" mana_cost:2 blood_cost:0 power:2 mana_regen:0 blood_regen:0];
        Skill* s2 = [[Skill alloc]initSkill: @"猛龙摆尾" mana_cost:2 blood_cost:0 power:2 mana_regen:0 blood_regen:0];
        self->skills = [[NSArray alloc]initWithObjects:s1, s2, nil];
        
    }
    
    return self;
}
@end

@implementation HuaMuLan

-(id) init {
    self = [super init];
    
    if (self) {
        self->name = @"花木兰";
        self->country = @"蜀国";
        self->blood_value = 20;
        self->energy_value = 20;
        self->skill_num = 2;
        Skill* s1 = [[Skill alloc]initSkill: @"孤注一掷" mana_cost:2 blood_cost:0 power:2 mana_regen:0 blood_regen:0];
        Skill* s2 = [[Skill alloc]initSkill: @"木兰之心" mana_cost:2 blood_cost:0 power:2 mana_regen:0 blood_regen:0];
        self->skills = [[NSArray alloc]initWithObjects:s1, s2, nil];
        
    }
    
    return self;
}
@end

@implementation LvDongBin

-(id) init {
    self = [super init];
    
    if (self) {
        self->name = @"吕洞宾";
        self->country = @"蜀国";
        self->blood_value = 20;
        self->energy_value = 20;
        self->skill_num = 2;
        Skill* s1 = [[Skill alloc]initSkill: @"惊鸿游龙" mana_cost:2 blood_cost:0 power:2 mana_regen:0 blood_regen:0];
        Skill* s2 = [[Skill alloc]initSkill: @"回魂仙梦" mana_cost:2 blood_cost:0 power:0 mana_regen:0 blood_regen:3];
        self->skills = [[NSArray alloc]initWithObjects:s1, s2, nil];
        
    }
    
    return self;
}
@end

@implementation GuanYu

-(id) init {
    self = [super init];
    
    if (self) {
        self->name = @"关羽";
        self->country = @"蜀国";
        self->blood_value = 20;
        self->energy_value = 20;
        self->skill_num = 2;
        Skill* s1 = [[Skill alloc]initSkill: @"顺劈" mana_cost:2 blood_cost:0 power:2 mana_regen:0 blood_regen:0];
        Skill* s2 = [[Skill alloc]initSkill: @"拖刀" mana_cost:0 blood_cost:0 power:0 mana_regen:2 blood_regen:2];
        self->skills = [[NSArray alloc]initWithObjects:s1, s2, nil];
        
    }
    
    return self;
}
@end


@implementation DeLaiESi

-(id) init {
    self = [super init];
    
    if (self) {
        self->name = @"德莱厄斯";
        self->country = @"蜀国";
        self->blood_value = 20;
        self->energy_value = 20;
        self->skill_num = 2;
        Skill* s1 = [[Skill alloc]initSkill: @"大杀四方" mana_cost:2 blood_cost:0 power:2 mana_regen:0 blood_regen:0];
        Skill* s2 = [[Skill alloc]initSkill: @"诺克萨斯断头台" mana_cost:8 blood_cost:0 power:8 mana_regen:0 blood_regen:0];
        self->skills = [[NSArray alloc]initWithObjects:s1, s2, nil];
        
    }
    
    return self;
}

@end

你可能感兴趣的:(现代操作系统应用开发,objective-c,操作系统,开发平台)