ZuoyeOc-04(字典做游戏)
2015-11-29 14:27
323 查看
//main函数 // // main.m // ZuoyeOc-04 // // Created by 刘佳斌 on 15/11/23. // Copyright © 2015年 刘佳斌. All rights reserved. // /* 作业: 1、游戏作业:定义英雄 怪物 武器 衣服 英雄技能 怪物技能 这些类 2、英雄类:初始的名字、攻击力、血量、防御力、技能、武器、装备 3、怪物类:初始的名字、攻击力、血量、防御力 4、怪物技能类:初始的名字、伤害 英雄技能类:初始的名字、伤害 5、武器类:初始的名字、伤害 6、衣服类:初始的名字、防御 */ #import <Foundation/Foundation.h> #import "NSDictionary+ReadFile.h" #import "Cloth.h" #import "Weapon.h" #import "Skill.h" #import "Hero.h" #import "Monster.h" int main(int argc, const char * argv[]) { @autoreleasepool { NSDictionary *allData = [NSDictionary readFileWithName:@"GameData"]; #pragma mark ------衣服---- NSMutableArray *clothList = [NSMutableArray array]; NSArray *cloths = allData[@"clothes"]; for (int i=0; i<cloths.count; i++) { NSDictionary *clothDic = cloths[i]; Cloth *cloth = [[Cloth alloc]initWithName:clothDic[@"name"] andDef:[clothDic[@"def"] intValue]]; [clothList addObject:cloth]; } NSLog(@"%@ %d",[clothList[0] name],[clothList[0] def]); #pragma mark ------武器---- NSMutableArray *weaponList = [NSMutableArray array]; NSArray *weapons = allData[@"weapon"]; for (int i=0; i<weapons.count; i++) { NSDictionary *weaponDic = weapons[i]; Weapon *weapon = [[Weapon alloc]initWithName:weaponDic[@"name"] andAtt:[weaponDic[@"att"] intValue]]; [weaponList addObject:weapon]; } NSLog(@"%@ %d",[weaponList[0] name],[weaponList[0] att]); #pragma mark ------英雄技能-- NSMutableArray *HeroSkillList = [NSMutableArray array]; NSArray *hSkills = allData[@"heroSkill"]; for (int i=0; i<hSkills.count; i++) { NSDictionary *hSkillDic = hSkills[i]; Skill *heroSkill = [[Skill alloc]initWithName:hSkillDic[@"name"] andPower:[hSkillDic[@"att"] intValue]]; [HeroSkillList addObject:heroSkill]; } #pragma mark -----怪物技能--- NSMutableArray *MonsterSkillList = [NSMutableArray array]; NSArray *mSkills = allData[@"monsterSkill"]; for (int i=0; i<mSkills.count; i++) { NSDictionary *mSkillDic = mSkills[i]; Skill *monsterSkill = [[Skill alloc]initWithName:mSkillDic[@"name"] andPower:[mSkillDic[@"att"] intValue]]; [MonsterSkillList addObject:monsterSkill]; } #pragma mark ------英雄----- NSMutableArray *heroList = [NSMutableArray array]; NSArray *heros = allData[@"hero"]; for (int i=0; i<heros.count; i++) { NSDictionary *heroDic = heros[i]; Hero *hero = [[Hero alloc]initWithName:heroDic[@"name"] andBlood:[heroDic[@"blood"] intValue] andAtt:[heroDic[@"att"]intValue] andDef:[heroDic[@"def"] intValue] andSkill:HeroSkillList[0] andWeapon:weaponList[0] andCloth:clothList[0]]; [heroList addObject:hero]; } #pragma mark -----怪物---- NSMutableArray *monsterList = [NSMutableArray array]; NSArray *monsters = allData[@"monster"]; for (NSDictionary *monsterDic in monsters) { Monster *monster = [[Monster alloc]initWithName:monsterDic[@"name"] andBlood: [monsterDic[@"blood"]intValue] andAtt:[monsterDic[@"att"]intValue] andDef:[monsterDic[@"def"]intValue] andSkill:MonsterSkillList[0]]; [monsterList addObject:monster]; } NSLog(@"%@ %d",[monsterList[0]name],[monsterList[0]att]); } return 0; }
//Monster类 //.h文件 // // Monster.h // ZuoyeOc-04 // // Created by 刘佳斌 on 15/11/23. // Copyright © 2015年 刘佳斌. All rights reserved. // #import <Foundation/Foundation.h> @class Skill; @interface Monster 4000 : NSObject @property NSString *name; @property int blood; @property int att; @property int def; @property Skill *monsterSkill; - (instancetype)initWithName:(NSString *)name andBlood:(int)blood andAtt:(int)att andDef:(int)def andSkill:(Skill *)monsterSkill; @end //.m文件 // // Monster.m // ZuoyeOc-04 // // Created by 刘佳斌 on 15/11/23. // Copyright © 2015年 刘佳斌. All rights reserved. // #import "Monster.h" #import "Skill.h" @implementation Monster - (instancetype)initWithName:(NSString *)name andBlood:(int)blood andAtt:(int)att andDef:(int)def andSkill:(Skill *)monsterSkill { self = [super init]; if (self) { _name = name; _blood = blood; _att = att; _def = def; _monsterSkill = monsterSkill; } return self; } @end
//Hero类 //.h文件 // // Hero.h // ZuoyeOc-04 // // Created by 刘佳斌 on 15/11/23. // Copyright © 2015年 刘佳斌. All rights reserved. // #import <Foundation/Foundation.h> @class Skill; @class Weapon; @class Cloth; @interface Hero : NSObject @property NSString *name; @property int blood; @property int att; @property int def; @property Skill *heroSkill; @property Weapon *heroWeapon; @property Cloth *heroCloth; - (instancetype)initWithName:(NSString *)name andBlood:(int)blood andAtt:(int)att andDef:(int)def andSkill:(Skill *)heroSkill andWeapon:(Weapon *)heroWeapon andCloth:(Cloth *)heroCloth; @end //.m文件 // // Hero.m // ZuoyeOc-04 // // Created by 刘佳斌 on 15/11/23. // Copyright © 2015年 刘佳斌. All rights reserved. // 名字、攻击力、血量、防御力、技能、武器、装备 #import "Hero.h" #import "Skill.h" #import "Weapon.h" #import "Cloth.h" @implementation Hero - (instancetype)initWithName:(NSString *)name andBlood:(int)blood andAtt:(int)att andDef:(int)def andSkill:(Skill *)heroSkill andWeapon:(Weapon *)heroWeapon andCloth:(Cloth *)heroCloth { self = [super init]; if (self) { _name = name; _blood = blood; _att = att; _def = def; _heroSkill = heroSkill; _heroCloth = heroCloth; _heroWeapon = heroWeapon; } return self; } @end
//Skill类 //.h文件 // // Skill.h // ZuoyeOc-04 // // Created by 刘佳斌 on 15/11/23. // Copyright © 2015年 刘佳斌. All rights reserved. // #import <Foundation/Foundation.h> @interface Skill : NSObject @property NSString *name; @property int att; - (instancetype)initWithName:(NSString *)name andPower:(int)att; @end //.m文件 // // Skill.m // ZuoyeOc-04 // // Created by 刘佳斌 on 15/11/23. // Copyright © 2015年 刘佳斌. All rights reserved. // #import "Skill.h" @implementation Skill - (instancetype)initWithName:(NSString *)name andPower:(int)att { self = [super init]; if (self) { _name = name; _att = att; } return self; } @end
//Weapon类 //.h文件 // // Weapon.h // ZuoyeOc-04 // // Created by 刘佳斌 on 15/11/23. // Copyright © 2015年 刘佳斌. All rights reserved. // #import <Foundation/Foundation.h> @interface Weapon : NSObject @property NSString *name; @property int att; - (instancetype)initWithName:(NSString *)name andAtt:(int)att; @end //.m文件 // // Weapon.m // ZuoyeOc-04 // // Created by 刘佳斌 on 15/11/23. // Copyright © 2015年 刘佳斌. All rights reserved. // #import "Weapon.h" @implementation Weapon - (instancetype)initWithName:(NSString *)name andAtt:(int)att { self = [super init]; if (self) { _name = name; _att = att; } return self; } @end
//Cloth类 //.h文件 // // Cloth.h // ZuoyeOc-04 // // Created by 刘佳斌 on 15/11/23. // Copyright © 2015年 刘佳斌. All rights reserved. // #import <Foundation/Foundation.h> @interface Cloth : NSObject @property NSString *name; @property int def; - (instancetype)initWithName:(NSString *)name andDef:(int)def; @end //.m文件 // // Cloth.m // ZuoyeOc-04 // // Created by 刘佳斌 on 15/11/23. // Copyright © 2015年 刘佳斌. All rights reserved. // #import "Cloth.h" @implementation Cloth - (instancetype)initWithName:(NSString *)name andDef:(int)def { self = [super init]; if (self) { _name = name; _def = def; } return self; } @end
//读取文件的封装方法 //.h文件 // // NSDictionary+ReadFile.h // OC-Game // // Created by Bruce on 15/11/20. // Copyright © 2015年 Bruce. All rights reserved. // #import <Foundation/Foundation.h> @interface NSDictionary (ReadFile) + (NSDictionary *)readFileWithName:(NSString *)name; @end //.m文件 // // NSDictionary+ReadFile.m // OC-Game // // Created by Bruce on 15/11/20. // Copyright © 2015年 Bruce. All rights reserved. // #import "NSDictionary+ReadFile.h" @implementation NSDictionary (ReadFile) + (NSDictionary *)readFileWithName:(NSString *)name { NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Desktop/%@.plist",name]]; return [NSDictionary dictionaryWithContentsOfFile:path]; } @end
相关文章推荐
- jquery 使用 jquery-migrate-1.2.1 判断浏览器型号及版本信息
- Android开发--UI之Bundle的使用
- Objective-C:swift、objective-c、C++、C混合编程
- 取消延迟执行函数 cancelPreviousPerformRequestsWithTarget
- centos忘记root密码解决方法
- 创建自定义控件
- unbuntu server12.04配置hadoop2.7.1(四):hadoop 2.7.1的安装(所有2.x版本都可以)
- LintCode-剑指Offer-(46)主元素
- UITextView的常用方法
- 前端性能优化 CSS动画
- [LeetCode]Binary Tree Level Order Traversal II
- JavaWeb-结合七牛搭建个人相册
- Ubuntu Server VMWare Install
- ibatis的简单入门
- 2015年11月 广州深圳 MVP 线下活动
- Linux终端的总结和shell
- POJ1840 Eqr (哈希)
- Fragment生命周期方法的说明
- Ping使用
- framebuffer 子系统分析