植物大戰殭屍

來源:互聯網
上載者:User

標籤:class   blog   code   com   2014   string   

#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;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.