您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: