• 植物大战僵尸


    #import <Foundation/Foundation.h>
    
    @interface CommonZomble : NSObject
    {
        NSString * _zombleKind;//僵尸种类
        NSInteger _totalBloodVolume;//总血量
        NSInteger _everyTimeBloodloss;//每次被攻击的血量
        NSInteger _residualVolume;//剩余血量
        BOOL _zombleDeath;//僵尸是否死亡
    }
    
    - (instancetype)initWithZombleKind:(NSString *)zombleKind totalBloodVolume:(NSInteger)totalBloodVolume everyTimeBloodloss:(NSInteger)everyTimeBloodloss;
    
    - (void)attack;
    
    - (BOOL)death;
    
    - (BOOL)takeDeath;
    
    + (CommonZomble *)commonZombleInitWithZombleKind:(NSString *)zombleKind totalBloodVolume:(NSInteger)totalBloodVolume everyTimeBloodloss:(NSInteger)everyTimeBloodloss;
    @end
    

    #import "CommonZomble.h"
    
    @implementation CommonZomble
    - (instancetype)initWithZombleKind:(NSString *)zombleKind totalBloodVolume:(NSInteger)totalBloodVolume everyTimeBloodloss:(NSInteger)everyTimeBloodloss
    {
        self = [super init];
        if (self) {
            _totalBloodVolume = totalBloodVolume;
            _residualVolume = _totalBloodVolume;
            _zombleKind = zombleKind;
            _everyTimeBloodloss = everyTimeBloodloss;
            _zombleDeath = NO;
        }
        return self;
    }
    
    - (void)attack
    {
        _residualVolume = _residualVolume - _everyTimeBloodloss;
        NSLog(@"%@,剩余血量%ld,每次失血量%ld",_zombleKind,_residualVolume,_everyTimeBloodloss);
    }
    
    - (BOOL)death
    {
        BOOL result = NO;
        if (_residualVolume < _everyTimeBloodloss) {
            _zombleDeath=YES;
             NSLog(@"%@已死",_zombleKind);
            result = YES;
        }
        return result;
    }
    
    - (BOOL)takeDeath
    {
        return _zombleDeath;
    }
    
    + (CommonZomble *)commonZombleInitWithZombleKind:(NSString *)zombleKind totalBloodVolume:(NSInteger)totalBloodVolume everyTimeBloodloss:(NSInteger)everyTimeBloodloss
    {
        return [[CommonZomble alloc]initWithZombleKind:zombleKind totalBloodVolume:totalBloodVolume everyTimeBloodloss:everyTimeBloodloss];
    }
    @end
    
    <pre name="code" class="objc">#import "CommonZomble.h"
    
    @interface BarricadeZombie : CommonZomble
    {
        NSString * _prop;//道具
        NSString * _weakness;//弱点
    }
    
    - (instancetype)initWithZombleKind:(NSString *)zombleKind totalBloodVolume:(NSInteger)totalBloodVolume everyTimeBloodloss:(NSInteger)everyTimeBloodloss prop:(NSString *)prop weakness:(NSString *)weakness;
    
    - (void)attack;
    
    - (void)lossProp;
    
    - (BarricadeZombie *)barricadeZombieInitWithZombleKind:(NSString *)zombleKind totalBloodVolume:(NSInteger)totalBloodVolume everyTimeBloodloss:(NSInteger)everyTimeBloodloss prop:(NSString *)prop weakness:(NSString *)weakness;
    
    
    @end
    


    
    


    #import "BarricadeZombie.h"
    
    @implementation BarricadeZombie
    - (instancetype)initWithZombleKind:(NSString *)zombleKind totalBloodVolume:(NSInteger)totalBloodVolume everyTimeBloodloss:(NSInteger)everyTimeBloodloss prop:(NSString *)prop weakness:(NSString *)weakness
    {
        self = [super initWithZombleKind:zombleKind totalBloodVolume:totalBloodVolume everyTimeBloodloss:everyTimeBloodloss];
        if (self) {
            _prop = prop;
            _weakness = weakness;
        }
        return self;
    }
    
    - (void)attack
    {
        _residualVolume = _residualVolume - _everyTimeBloodloss;
          NSLog(@"%@,剩余血量%ld,每次失血量%ld",_zombleKind,_residualVolume,_everyTimeBloodloss);
        if (_residualVolume<= _totalBloodVolume/2 && _residualVolume > _totalBloodVolume/2 - _everyTimeBloodloss) {
            NSLog(@"%@丢失",_prop);
            [self lossProp];
        }
    }
    
    - (void)lossProp
    {
        _everyTimeBloodloss = 3;
    }
    
    - (BarricadeZombie *)barricadeZombieInitWithZombleKind:(NSString *)zombleKind totalBloodVolume:(NSInteger)totalBloodVolume everyTimeBloodloss:(NSInteger)everyTimeBloodloss prop:(NSString *)prop weakness:(NSString *)weakness
    {
        return [[BarricadeZombie alloc]
                barricadeZombieInitWithZombleKind:zombleKind totalBloodVolume:totalBloodVolume everyTimeBloodloss:everyTimeBloodloss prop:prop weakness:weakness];
    }
    
    @end
    

    #import "BarricadeZombie.h"
    
    @interface DrumZomble : BarricadeZombie
    - (void)attack;
    @end

    #import "DrumZomble.h"
    
    @implementation DrumZomble
    
    - (void)attack
    {
        _residualVolume = _residualVolume - _everyTimeBloodloss;
         NSLog(@"%@,剩余血量%ld,每次失血量%ld",_zombleKind,_residualVolume,_everyTimeBloodloss);
        if (_residualVolume <= _totalBloodVolume/3 && _residualVolume > _totalBloodVolume/3 - _everyTimeBloodloss) {
            [self lossProp];
            NSLog(@"%@掉了",_prop);
        }
    }
    @end
    <pre name="code" class="objc">#import <Foundation/Foundation.h>
    #import "CommonZomble.h"
    #import "BarricadeZombie.h"
    #import "DrumZomble.h"
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            
            CommonZomble * commonZomble = [[CommonZomble alloc]initWithZombleKind:@"普通僵尸" totalBloodVolume:50 everyTimeBloodloss:3];
            BarricadeZombie * barricadeZombie = [[BarricadeZombie alloc]initWithZombleKind:@"路障僵尸" totalBloodVolume:80 everyTimeBloodloss:2 prop:@"路障" weakness:@"怕我"];
            DrumZomble * drumZomble = [[DrumZomble alloc]initWithZombleKind:@"铁桶僵尸" totalBloodVolume:120 everyTimeBloodloss:1 prop:@"铁桶" weakness:@"磁铁"];
            
            NSInteger count = 0;
            while (1) {
                count++;
                 NSLog(@"count = %ld",count);
                if (![commonZomble death]) {
                    [commonZomble attack];
                }
                
                if (![barricadeZombie death]) {
                    [barricadeZombie attack];
                }
                
                if (![drumZomble death]) {
                    [drumZomble attack];
                }
                if ([commonZomble takeDeath] && [barricadeZombie takeDeath] && [drumZomble takeDeath]) {
                    break;
                }
            }
            NSLog(@"count = %ld",count);
            
            
        }
        return 0;
    }
    


    
    


  • 相关阅读:
    用于主题检测的临时日志(594fb726-af0b-400d-b647-8b1d1b477d72
    返璞归真vc++之字符类型
    DIV居中
    程序员职业生涯
    枚举进程句柄
    不使用mutex设计模式解决并发访问cache
    服务器权重分配算法
    xmemecached中的一致性hash算法
    安卓课堂练习
    pythonPTA---分支循环与集合7-1 jmu-python-韩信点兵 (20分) 7-2 打印数字矩形 (10分) 7-3 成绩统计 (10分) 7-4 找列表中最大元素的下标 7-5 删除列表中的重复值
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6741953.html
Copyright © 2020-2023  润新知