C#/과제

2020.04.27. 과제 - 승급하기 3 (메이플 스토리 / 몬스터, 레벨업)

dev_sr 2020. 4. 28. 01:17

 

각 엑셀테이블에 해당하는 멤버변수를 선언하는 클래스의 코드는 생략했습니다.

첫번째 전직 이후의 전직을 위해 

인덱스로 사용할 heroclassGrade 값을 계속 증가시키는 작업이 필요하다고 생각되어서

매개변수가 하나인 Upgrade 메서드를 하나 더 생성하고

classGrade값을 인덱스로 사용하여 ClassData 값을 찾는 GetClassDataByIndex메서드를 추가했습니다.

 

 

1. App class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
 
namespace Study_017
{
    class App
    {
        public App()
        {
            //싱글톤으로 데이터매니저 인스턴스 생성
            DataManager dataManager = DataManager.GetInstance();
            dataManager.LoadData();
 
            //영웅(캐릭터) 생성
            Hero hong = this.CreateHero(1000);
 
            //몬스터(캐릭터) 생성
            Monster monster = this.CreateMonster(1000);
 
            //영웅(캐릭터) 공격
            hong.Attack(monster);
            hong.Attack(monster);
            hong.Attack(monster);
 
            //몬스터가 죽으면 아이템과 경험치를 얻는다
            if (monster.IsDie())
            {
                //경험치를 얻는다
                int exp = monster.monsterData.exp;
                hong.GetExp(exp);
 
                //아이템을 얻는다
                Item item= this.DropItem(monster.monsterData.drop_item_id);
                hong.GetItem(item);
 
                //결과를 한번에 보기 위해서 반복문 사용
                for (int i = 0; i < 5; i++)
                {
                    //히어로가 직업이 없으면
                    if (hong.info.classGrade < 0)
                    {
                        //다음 전직할 클래스 데이터를 리스트로 가져온다
                        List<ClassData> Nextclassdatalist = DataManager.GetInstance().GetAvailableClassDatas(hong.info.classType);
 
                        //레벨이 요구레벨 이상이면 전직 선택
                        if (hong.info.level >= Nextclassdatalist[0].require_level)
                        {
                            Console.WriteLine("--------------------------------------");
                            Console.WriteLine("\n{0}나 {1}로 전직할 수 있습니다", Nextclassdatalist[0].class_name, Nextclassdatalist[1].class_name);
                            //검사 선택
                            ClassData selectClass = Nextclassdatalist[0];
 
                            //전직
                            hong.Upgrade(null, selectClass);
 
                        }
 
                    }
                    else
                    {
                        //다음 전직할 클래스 데이터를 리스트로 가져온다
                        List<ClassData> Nextclassdatalist = DataManager.GetInstance().GetAvailableClassDatas(hong.info.classType);
 
                        if (hong.info.level >= Nextclassdatalist[hong.info.classGrade].require_level)
                        {
                            
                            ClassData NextData = Nextclassdatalist[hong.info.classGrade];
 
                            //전직
                            hong.Upgrade(NextData);
 
                        }
                    }
                }
              
            }
 
        }
 
        public Hero CreateHero(int id)
        {
            HeroData characterData = DataManager.GetInstance().GetCharacterDataById(id);
            HeroInfo info = new HeroInfo("홍길동"characterData.hp, characterData.mp,characterData.damage);
 
            return new Hero(info);
        }
 
        public Monster CreateMonster(int id)
        {
            MonsterData monsterData = DataManager.GetInstance().GetMonsterDataById(id);
 
            return new Monster(monsterData);
        }
 
        public Item DropItem(int id)
        {
            ItemData itemData = DataManager.GetInstance().GetItemDataById(id);
 
            Console.WriteLine("\n{0}이 바닥에 떨어졌습니다.",itemData.name);
 
            return new Item(itemData.id, itemData.name);
        }
    }
}
 
 

 

 

 

2. DataManager class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
 
namespace Study_017
{
    class DataManager
    {
        static DataManager instance;
        private Dictionary<int, BudgetData> dicBudgetDatas;
        private Dictionary<int, HeroData> dicCharacterDatas;
        private Dictionary<int, ClassData> dicClassDatas;
        private Dictionary<int, ItemData> dicItemDatas;
        private Dictionary<int, LevelupData> dicLevelupDatas;
        private Dictionary<int, MonsterData> dicMonsterDatas;
 
        private DataManager()
        {
            this.dicBudgetDatas = new Dictionary<int, BudgetData>();
            this.dicCharacterDatas = new Dictionary<int, HeroData>();
            this.dicClassDatas = new Dictionary<int, ClassData>();
            this.dicItemDatas = new Dictionary<int, ItemData>();
            this.dicLevelupDatas = new Dictionary<int, LevelupData>();
            this.dicMonsterDatas = new Dictionary<int, MonsterData>();
 
        }
 
        public static DataManager GetInstance()
        {
            if(DataManager.instance==null)
            {
                DataManager.instance = new DataManager();
                return DataManager.instance;
            }
            else
            {
                return DataManager.instance;
            }
        }
 
        public void LoadData()
        {
            string budgetJson = File.ReadAllText("./data/budget_data.json");
            string characterJson = File.ReadAllText("./data/character_data.json");
            string classJson = File.ReadAllText("./data/class_data.json");
            string itemJson = File.ReadAllText("./data/item_data.json");
            string levelupJson = File.ReadAllText("./data/levelup_date.json");
            string monsterJson = File.ReadAllText("./data/monster_data.json");
 
            this.dicBudgetDatas = JsonConvert.DeserializeObject<BudgetData[]>(budgetJson).ToDictionary(x => x.id, x => x);
            this.dicCharacterDatas = JsonConvert.DeserializeObject<HeroData[]>(characterJson).ToDictionary(x => x.character_id, x => x);
            this.dicClassDatas = JsonConvert.DeserializeObject<ClassData[]>(classJson).ToDictionary(x => x.class_id, x => x);
            this.dicItemDatas = JsonConvert.DeserializeObject<ItemData[]>(itemJson).ToDictionary(x => x.id, x => x);
            this.dicLevelupDatas = JsonConvert.DeserializeObject<LevelupData[]>(levelupJson).ToDictionary(x => x.exp_id, x => x);
            this.dicMonsterDatas = JsonConvert.DeserializeObject<MonsterData[]>(monsterJson).ToDictionary(x => x.id, x => x);
        }
 
        public BudgetData GetBudgetDataById(int id)
        {
            return this.dicBudgetDatas[id];
        }
 
        public HeroData GetCharacterDataById(int id)
        {
            return this.dicCharacterDatas[id];
        }
        public ClassData GetClassDataById(int id)
        {
            return this.dicClassDatas[id];
        }
 
        public ItemData GetItemDataById(int id)
        {
            return this.dicItemDatas[id];
        }
 
        public LevelupData GetLevelupDataByIndex(int id)
        {
            List<LevelupData> levelDataList = new List<LevelupData>();
 
            foreach(var pair in this.dicLevelupDatas)
            {
                levelDataList.Add(pair.Value);
            }
 
            return levelDataList[id];
        }
 
 
        public MonsterData GetMonsterDataById(int id)
        {
            return this.dicMonsterDatas[id];
        }
 
 
        public List<ClassData> GetAvailableClassDatas(int classtype)
        {
 
 
            //전사계열
            List<ClassData> list1 = new List<ClassData>();
            //궁수계열
            List<ClassData> list2 = new List<ClassData>();
            //두 계열의 첫번째 값만 담기
            List<ClassData> list3 = new List<ClassData>();
 
            if (classtype <= 0)
            {
                foreach (KeyValuePair<int, ClassData> pair in this.dicClassDatas)
                {
                    if (pair.Value.class_type == 1)
                    {
                        list1.Add(pair.Value);
                    }
 
                    if (pair.Value.class_type == 2)
                    {
                        list2.Add(pair.Value);
                    }
                }
 
                list3.Add(list1[0]);
                list3.Add(list2[0]);
            }
 
            else
            {
                foreach(var pair in this.dicClassDatas)
                {
                    if(classtype==pair.Value.class_type)
                    {
                        list3.Add(pair.Value);
                    }
                }
            }
 
            return list3;
 
        }
 
        public ClassData GetClassDataByIndex(int classType,int classGrade)
        {
            List<ClassData> classDataList = new List<ClassData>();
 
            foreach (var pair in this.dicClassDatas)
            {
                if(pair.Value.class_type==classType)
                {
                    classDataList.Add(pair.Value);
                }
                
            }
 
            return classDataList[classGrade-1];
        }
 
 
    }
}
 
 

 

 

3. Hero class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_017
{
    class Hero : Character
    {
        public HeroInfo info;
        private Item item;
        public ClassData currentClassData;
 
        public Hero(HeroInfo info)
        {
            this.info = info;
            Console.WriteLine("{0} (Lv: {1}  Hp: {2}  Mp: {3})이 생성되었습니다",this.info.name,this.info.level,this.info.hp,this.info.mp);
        }
 
        public override void Attack(Character target)
        {
            Console.WriteLine("\n{0}이 공격합니다 (공격력: {1})",this.info.name,this.info.damage);
            target.Hit(this.info.damage);
        }
 
        public override void Hit(float damage)
        {
 
            this.info.hp -= damage;
            
 
            if (this.info.hp <= 0)
            {
                this.info.hp = 0;
                Console.WriteLine("{0}이 {1}만큼 공격당했습니다 (남은 체력: {2})"this.info.name, damage, this.info.hp);
                this.Die();
 
                return;
            }
 
            Console.WriteLine("{0}이 {1}만큼 공격당했습니다 (남은 체력: {2})"this.info.name, damage, this.info.hp);
 
        }
 
        public override void Die()
        {
            Console.WriteLine("{0}이 죽었습니다.",this.info.name);
        }
 
 
        public bool IsDie()
        {
 
            if (this.info.hp <= 0)
            {
                return true;
            }
            return false;
        }
 
        public void GetItem(Item item)
        {
            this.item = item;
            Console.WriteLine("\n{0}을 획득했습니다.",item.name);
        }
 
        public void GetExp(int exp)
        {
            this.info.exp = exp;
            Console.WriteLine("\n경험치 {0}를 얻었습니다.",exp);
 
 
            //레벨업 표에서 다음 인덱스 가르키기
            int nextLevel = this.info.level + 1;
            int nextLevelIndex = nextLevel - 1;
 
            LevelupData levelupData = DataManager.GetInstance().GetLevelupDataByIndex(nextLevelIndex);
 
            //레벨업 데이터의 다음 레벨 요구 경험치가 현재 가진 경험치보다 작으면 레벨업
            if(levelupData.require_exp<=this.info.exp)
            {
                this.info.exp = levelupData.require_exp;
                this.Levelup();
            }
 
            //레벨 150
            this.info.level = 150;
 
        }
 
        public void Levelup()
        {
            this.info.level ++;
            Console.WriteLine("\n레벨이 올랐습니다. Lv. {0}",this.info.level);
 
        }
 
        //처음 전직
        public void Upgrade(ClassData beforeData, ClassData NextData)
        {
            ClassData beforeClassData = null;
 
            if(beforeData==null)
            {
                beforeClassData = DataManager.GetInstance().GetClassDataById(100);
            } 
            else
            {
                beforeClassData = beforeData;
            }
 
            Console.WriteLine("\n{0}에서 {1}로 전직하였습니다.",beforeClassData.class_name,NextData.class_name);
 
            this.info.classType = NextData.class_type;
            this.info.classGrade = 1;
            this.currentClassData = NextData;
 
        }
 
        //다음 전직
        public void Upgrade(ClassData NextData)
        {
            this.currentClassData = DataManager.GetInstance().GetClassDataByIndex(this.info.classType,this.info.classGrade);
 
 
            Console.WriteLine("\n{0}에서 {1}로 전직하였습니다."this.currentClassData.class_name, NextData.class_name);
 
            this.info.classGrade ++;
 
        }
    }
}
 
 

 

 

4. Monster class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_017
{
    class Monster : Character
    {
        public MonsterData monsterData;
        public Monster(MonsterData monsterData)
        {
            this.monsterData = monsterData;
        }
 
        public override void Attack(Character target)
        {
            Console.WriteLine("\n{0}이 공격합니다 (공격력: {1})"this.monsterData.name, this.monsterData.damage);
            target.Hit(this.monsterData.damage);
        }
 
        public override void Hit(float damage)
        {
 
            this.monsterData.hp -= damage;
 
            if (this.monsterData.hp <= 0)
            {
                this.monsterData.hp = 0;
 
                Console.WriteLine("{0}가 {1}만큼 공격당했습니다 (남은 체력: {2})"this.monsterData.name, damage, this.monsterData.hp);
 
                this.Die();
 
                return;
            }
 
            
            Console.WriteLine("{0}가 {1}만큼 공격당했습니다 (남은 체력: {2})"this.monsterData.name, damage, this.monsterData.hp);
        }
 
        public override void Die()
        {
            Console.WriteLine("{0}이 죽었습니다."this.monsterData.name);
        }
 
        //몬스터가 죽었는지 확인
        public bool IsDie()
        {
 
            if (this.monsterData.hp <= 0)
            {
                return true;
            }
            return false;
        }
    }
}
 
 

 

 

5. Character class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_017
{
    class Character
    {
        public Character()
        {
 
        }
 
        public virtual void Attack(Character target)
        {
            
        }
 
        public virtual void Hit(float damage)
        {
 
        }
 
        public virtual void Die()
        {
 
        }
 
    }
}
 
 

 

 

6. CharacterInfo class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_017
{
    class HeroInfo
    {
        public string name;
        public int level;
        public int exp;
        public float hp;
        public int mp;
        public float damage;
        public int classGrade;
        public int classType;
        public HeroInfo(string name, int hp, int mp, float damage, 
int level = 1int exp = 0int classType=0int classGrade = -1)
        {
            this.name = name;
            this.hp = hp;
            this.mp = mp;
            this.exp = exp;
            this.level = level;
            this.damage = damage;
            this.classGrade = classGrade;
            this.classType = classType;
        }
    }
}
 
 

 

 

7. Item class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_017
{
    class Item
    {
        public int id;
        public string name;
        public Item(int id, string name)
        {
            this.id = id;
            this.name = name;
        }
    }
}
 
 

 

 

8. 결과값