Objective-C打僵尸代码


}

#import <Foundation/Foundation.h>

#import "CommonZoombie.h"

#import "LuZhangZoombie.h"

#import "TieTongZoombie.h"

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

{

   CommonZoombie *zoombie1 = [[CommonZoombie alloc]initWithZoombieType:@"普通僵尸甲" blood:50 loseBlood:3];

   int count = 0;

   LuZhangZoombie *zoombie3 = [[LuZhangZoombie alloc]initWithZoombieType:@"路障僵尸" blood:75 loseBlood:2];

   [zoombie3 setProps:@"路障"];

   TieTongZoombie *tieTong = [[TieTongZoombie alloc]initWithZoombieType:@"铁桶僵尸" blood:100 loseBlood:1];

   while (1) {

       //普通僵尸

               if ([zoombie1 getBlood]) {

                   [zoombie1 attacked];

                   [zoombie1 info];

               }else{

                   [zoombie1 die];

                   break;

               }

       //路障僵尸

               if ([zoombie3 getBlood]) {

                    if ([zoombie3 loseProps]) {

                        [zoombie3 setLoseBlood:3];

                   }

                   [zoombie3 attacked];

                   [zoombie3 info];

                   if ([zoombie3 getBlood]<=0) {

                    [zoombie3 die];

                        break;

                    }

       

                }

       //铁桶僵尸

       [tieTong setProps:@"铁桶"];

       [tieTong setWeakness:@"磁力菇"];

       [tieTong setMark:(arc4random()%50+1)];

       if ([tieTong getBlood])

       {

           if ([tieTong loseProps]) {

                [tieTong setLoseBlood:3];

           }

       }

       [tieTong attacked];

       [tieTong info];

       if ([tieTong getBlood]<=0)

       {

           [tieTong die];

           break;

       }

       count++;

    }

   NSLog(@"僵尸被攻击了%d次",count);

   return 0;

}

 

 

//普通僵尸

@interface CommonZoombie : NSObject

{

   NSString *_zoombieType;

   int _blood;

   int _loseBlood;

}

-(id) initWithZoombieType:(NSString*)zoombieType blood:(int)blood loseBlood:(int) loseBlood;

+(CommonZoombie*)commonZoombieWithZoombieType:(NSString *)zoombieType blood:(int)bloodloseBlood:(int) loseBlood;

-(int)getBlood;

-(void) attacked;

-(void) die;

-(void) info;

@end

@implementation CommonZoombie

-(id)initWithZoombieType:(NSString*)zoombieType blood:(int)blood loseBlood:(int)loseBlood{

   if (self = [super init]) {

       _zoombieType = zoombieType;

       _blood = blood;

       _loseBlood = loseBlood;

    }

   return self;

}

+(CommonZoombie*)commonZoombieWithZoombieType:(NSString *)zoombieType blood:(int)bloodloseBlood:(int)loseBlood

{

   CommonZoombie *zoombie = [[CommonZoombie alloc]initWithZoombieType:zoombieType blood:blood loseBlood:loseBlood];

   return zoombie;

}

-(int)getBlood

{

   return _blood;

}

-(void)attacked

{

   if (_blood < _loseBlood) {

       _blood = 0;

   }else

       _blood -= _loseBlood;

}

-(void)die{

   if (_blood <= _loseBlood) {

       NSLog(@"%@死了",_zoombieType);

    }

}

-(void)info{

   NSLog(@"%@被攻击,血量减少%d,剩余血量为%d",_zoombieType,_loseBlood,_blood);

}

@end

 

 

//路障僵尸

#import "CommonZoombie.h"

 

@interface LuZhangZoombie : CommonZoombie

{

   NSString *_propsType;

   int _flag;

}

-(void) setProps:(NSString*)propsType;

-(BOOL) loseProps;

-(void) setLoseBlood:(int) loseBlood;

@end

@implementation LuZhangZoombie

-(void)setProps:(NSString *)propsType

{

   _propsType = propsType;

}

-(BOOL)loseProps

{

   if (_blood <= 40&&_flag == 0) {

       NSLog(@"我的%@丢了",_propsType);

       _flag = 1;

       return YES;

    }

   return NO;

}

-(void)setLoseBlood:(int)loseBlood

{

   _loseBlood = loseBlood;

}

@end

 

//铁桶僵尸

@interface TieTongZoombie : LuZhangZoombie

{

   NSString *_weakness;

   int _mark;

}

-(void) setWeakness:(NSString*)weakness;

-(NSString*)getWeakness;

-(void) setMark:(int)mark;

-(int) getMark;

@end

@implementation TieTongZoombie

-(void)setMark:(int)mark

{

   _mark = mark;

}

-(int)getMark

{

   return _mark;

}

-(void)setWeakness:(NSString *)weakness

{

   _weakness = weakness;

}

-(NSString*)getWeakness

{

   return _weakness;

}

-(BOOL)loseProps

{

   if ((_blood <= 40||[self getMark]==3)&&_flag == 0) {

       NSLog(@"遇到%@,我的%@丢了",[self getWeakness],_propsType);

       _flag = 1;

       return YES;

    }

   return NO;

}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值