OC第三讲继承-模拟打僵尸

第二题 模拟打僵尸。需求:

  定义僵尸类:

公共成员变量:类型、总血量、每次失血量

方法:初始化方法(设置僵尸种类,总血量)、被打击失血、死亡


定义有防具僵尸类继承于僵尸类:

特有成员变量:防具类型

特有方法:防具被打烂


定义铁桶僵尸类继承于有防具僵尸:

特有成员变量:弱点

特有方法:防具被磁铁吸走


1、创建普通僵尸对象,设置总血量50,每次失血量为 3

2、创建路障僵尸对象,设置总血量80,有路障时,每次失血量为 2

3、创建铁桶僵尸对象,设置总血量120,有铁桶时,每次失血量为 1


4、选作:

在main.m里用循环语句模拟攻击:

三个僵尸对象同时被攻击:

(1)、普通僵尸被打击时:每次失血3.

(2)、路障僵尸被打击时:有路障时,每次失血2,血量剩余一半时,防具被打拦,之后每次失血3.

(3)、铁桶僵尸被打击时:有铁桶时,每次失血1,血量剩余1/3时,铁桶被打拦,之后每次失血3.

循环攻击过程中:每个僵尸被攻击时,输出本次丢失血量,剩余血量。失去道具时,输出丢失的道具。僵尸死亡时,输出已死亡。

最后一个僵尸死亡时,攻击停止,循环结束。输出总攻击次数。


代码如下:

// 创建普通僵尸类Zombie

// Zombie.h

#import <Foundation/Foundation.h>

@interface Zombie : NSObject
{
    NSString *_type;       //类型
    NSInteger _blood;      //总血量
    NSInteger _restBlood;  //剩余血量
    NSInteger _lostBlood;  //失血量
    BOOL _isDeath;         //是否死亡
}
/// 初始化
- (id)initWithType:(NSString *)type
             blood:(NSInteger)blood
         restBlood:(NSInteger)restBlood
         lostBlood:(NSInteger)lostBlood
           isDeath:(BOOL)isDeath;
/// 便利构造器
+ (id)zombieWithType:(NSString *)type
               blood:(NSInteger)blood
           lostBlood:(NSInteger)lostBlood;
/// 赋值
- (void)setIsDeath:(BOOL)isDeath;
/// 取值
- (NSString *)type;
- (NSInteger)blood;
- (NSInteger)lostBlood;
- (BOOL)isDeath;
/// 被打击失血
- (void)attack;
/// 死亡
- (void)dead;
@end

  // Zombie.m

#import "Zombie.h"

@implementation Zombie
/// 初始化
- (id)initWithType:(NSString *)type
             blood:(NSInteger)blood
         restBlood:(NSInteger)restBlood
         lostBlood:(NSInteger)lostBlood
           isDeath:(BOOL)isDeath
{
    self = [super init];
    if (self) {
        _type = type;
        _blood = blood;
        _restBlood = blood;
        _lostBlood = lostBlood;
        _isDeath  = NO;
    }
    return self;
}
/// 便利构造器
+ (id)zombieWithType:(NSString *)type
               blood:(NSInteger)blood
           lostBlood:(NSInteger)lostBlood
{
    return [[Zombie alloc] initWithType:type blood:blood restBlood:blood lostBlood:lostBlood isDeath:NO];
}
- (void)setIsDeath:(BOOL)isDeath
{
    _isDeath = isDeath;
}
/// 取值
- (NSString *)type
{
    return _type;
}
- (NSInteger)blood
{
    return _blood;
}
- (NSInteger)lostBlood
{
    return _lostBlood;
}
- (BOOL)isDeath
{
    return _isDeath;
}
/// 被打击失血
- (void)attack
{
    if (_restBlood <= _lostBlood) {
        _isDeath = YES;
        [self dead];
    } else {
        NSLog(@"普通僵尸每次失血量:%ld", _lostBlood);
        _restBlood -= _lostBlood;
        NSLog(@"普通僵尸剩余血量:%ld", _restBlood);
    }
}
/// 死亡
- (void)dead
{
    NSLog(@"。。。。。普通僵尸已死亡!!!");
}
@end

// 创建路障僵尸类ArmorZombie

//ArmorZombie.h

#import "Zombie.h"

@interface ArmorZombie : Zombie
{
    NSString *_armorType;    // 防具类型
    BOOL _isArmorDrop;       // 防具是否掉落
    
}
/// 初始化
- (id)initWithType:(NSString *)type
             blood:(NSInteger)blood
         lostBlood:(NSInteger)lostBlood
           isDeath:(BOOL)isDeath
         armorType:(NSString *)armorType;
/// 便利构造器
+ (id)armorZombieType:(NSString *)type
                blood:(NSInteger)blood
            lostBlood:(NSInteger)lostBlood
            armorType:(NSString *)armorType;
/// 赋值
- (void)setLostBlood:(NSInteger)lostBlood;
/// 取值
- (NSString *)type;
- (NSInteger)blood;
- (NSInteger)lostBlood;
- (NSString *)armorType;
- (BOOL)isArmorDrop;
/// 被打击失血
- (void)attack;
/// 防具被打拦
- (void)armorBreak;
/// 死亡
- (void)dead;
@end

// ArmorZombie.m

#import "ArmorZombie.h"

@implementation ArmorZombie
/// 初始化
- (id)initWithType:(NSString *)type
             blood:(NSInteger)blood
         lostBlood:(NSInteger)lostBlood
           isDeath:(BOOL)isDeath
         armorType:(NSString *)armorType
{
    self = [super initWithType:type blood:blood restBlood:blood lostBlood:lostBlood isDeath:NO];
    if (self) {
        _armorType = armorType;
        _isArmorDrop = NO;
    }
    return self;
}
/// 便利构造器
+ (id)armorZombieType:(NSString *)type
                blood:(NSInteger)blood
            lostBlood:(NSInteger)lostBlood
            armorType:(NSString *)armorType
{
    return [[ArmorZombie alloc] initWithType:type blood:blood lostBlood:lostBlood isDeath:NO armorType:armorType];
}
/// 赋值
- (void)setLostBlood:(NSInteger)lostBlood
{
    _lostBlood = lostBlood;
}
/// 取值
- (NSString *)type
{
    return _type;
}
- (NSInteger)blood
{
    return _blood;
}
- (NSInteger)lostBlood
{
    return _lostBlood;
}
- (NSString *)armorType
{
    return _armorType;
}
- (BOOL)isArmorDrop
{
    return _isArmorDrop;
}
/// 被打击失血
- (void)attack
{
    static int flag = 1;
    if (_restBlood <= _blood / 2 && 1 == flag) {
        _isArmorDrop = YES;
        [self armorBreak];
        [self setLostBlood:3];
        flag = 0;
     }
    if (_restBlood <= _lostBlood) {
        _isDeath = YES;
        [self dead];
    }else {
        NSLog(@"路障僵尸每次失血量:%ld", _lostBlood);
        _restBlood -= _lostBlood;
        NSLog(@"路障僵尸剩余血量:%ld", _restBlood);
    }
}
// 防具被打拦
- (void)armorBreak
{
    NSLog(@"路障掉落。。。");
}
/// 死亡
- (void)dead
{
    NSLog(@"。。。。。路障僵尸已死亡!!!");
}
@end

  // 创建铁桶僵尸类IronZombie

// IronZombie.h

#import "ArmorZombie.h"

@interface IronZombie : ArmorZombie
{
    NSString *_weakness;   //弱点
}
///初始化
- (id)initWithType:(NSString *)type
             blood:(NSInteger)blood
         lostBlood:(NSInteger)lostBlood
           isDeath:(BOOL)isDeath
         armorType:(NSString *)armorType
          weakness:(NSString *)weakness;
/// 便利构造器
+ (id)ironZombieType:(NSString *)type
               blood:(NSInteger)blood
           lostBlood:(NSInteger)lostBlood
           armorType:(NSString *)armorType
            weakness:(NSString *)weakness;
/// 取值
- (NSString *)type;
- (NSInteger)blood;
- (NSInteger)lostBlood;
- (NSString *)armorType;
- (NSString *)weakness;
/// 被打击失血
- (void)attack;
/// 防具被打拦
- (void)armorBreak;
/// 防具被磁铁吸走
- (void)absorb;
/// 死亡
- (void)dead;
@end

// IronZombie.m

#import "IronZombie.h"

@implementation IronZombie
///初始化
- (id)initWithType:(NSString *)type
             blood:(NSInteger)blood
         lostBlood:(NSInteger)lostBlood
           isDeath:(BOOL)isDeath
         armorType:(NSString *)armorType
          weakness:(NSString *)weakness
{
    self = [super initWithType:type blood:blood lostBlood:lostBlood isDeath:NO armorType:armorType];
    if (self) {
        _weakness = weakness;
    }
    return self;
}
/// 便利构造器
+ (id)ironZombieType:(NSString *)type
               blood:(NSInteger)blood
           lostBlood:(NSInteger)lostBlood
           armorType:(NSString *)armorType
            weakness:(NSString *)weakness
{
    return [[IronZombie alloc] initWithType:type blood:blood lostBlood:lostBlood isDeath:NO armorType:armorType weakness:weakness];
}
/// 取值
- (NSString *)type
{
    return _type;
}
- (NSInteger)blood
{
    return _blood;
}
- (NSInteger)lostBlood
{
    return _lostBlood;
}
- (NSString *)armorType
{
    return _armorType;
}
- (NSString *)weakness
{
    return _weakness;
}
/// 被打击失血
- (void)attack
{
    static int flag = 1;
    if (_restBlood <= _blood / 3 && 1 == flag) {
        _isArmorDrop = YES;
        [self armorBreak];
        [self setLostBlood:3];
        flag = 0;
    }
    if (_restBlood <= _lostBlood) {
        _isDeath = YES;
        [self dead];
    }else {
        NSLog(@"铁桶僵尸每次失血量:%ld", _lostBlood);
        _restBlood -= _lostBlood;
        NSLog(@"铁桶僵尸剩余血量:%ld", _restBlood);
    }
}
// 防具被打拦
- (void)armorBreak
{
    
    NSLog(@"铁桶掉落。。。");
}
/// 防具被磁铁吸走
- (void)absorb
{
    NSLog(@"铁桶被吸走");
}
/// 死亡
- (void)dead
{
    NSLog(@"。。。。。铁桶僵尸已死亡!!!");
}
@end

// 主函数main.m

#import <Foundation/Foundation.h>
#import "Zombie.h"
#import "ArmorZombie.h"
#import "IronZombie.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        Zombie *zombie = [Zombie zombieWithType:@"普通僵尸" blood:50 lostBlood:3];
        ArmorZombie *armorZombie = [ArmorZombie armorZombieType:@"防具僵尸" blood:80 lostBlood:2 armorType:@"路障防具"];
        IronZombie *ironZombie = [IronZombie ironZombieType:@"铁桶僵尸" blood:120 lostBlood:1 armorType:@"铁桶防具" weakness:@"铁桶被吸走"];
        int i = 0;
        while (![zombie  isDeath]||![armorZombie isDeath]||![ironZombie isDeath]) {
            if (![zombie  isDeath]) {
                [zombie attack];                 // 攻击普通僵尸
            }
            if (![armorZombie isDeath]) {
                [armorZombie attack];            // 攻击路障僵尸
            }
            if (![ironZombie isDeath]) {
                [ironZombie attack];             // 攻击铁桶僵尸
            }
            i++;
        }
        NSLog(@"总攻击数:%d", i);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值