C#/수업내용

2020.04.26. 승급하기 연습 (메이플 스토리)

dev_sr 2020. 4. 26. 23:12

 

 

엑셀테이블

 

1) character_data

 

2) promote_data

 

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace study_016_2
{
    class App
    {
        PromoteData promoteData;
        DataManager dataManager;
        Character hong;
 
        public App()
        {
            //데이터 매니저 생성
            dataManager = new DataManager();
 
            //초기화
            dataManager.Init();
 
            //json 파일 불러오기
            dataManager.LoadData();
 
            //딕셔너리에서 사용할 캐릭터 데이터 가져오기
            CharacterData chardata = dataManager.GetDataById(1000);
 
            //변할 데이터 값을 저장할 info객체 만들기
            CharacterInfo info = new CharacterInfo(chardata.id,chardata.promote_id,chardata.level,chardata.exp,chardata.class_name,0);
 
            //캐릭터 생성하기
            hong = new Character(info, chardata);
 
            //캐릭터 성장시키기
            this.Growup();
                      
        }
 
        //10, 30, 60, 100, 140 레벨마다 전직한다
        public void Growup()
        {
            while (true)
            {
 
                int level = hong.Levelup();
 
                if (level == 10)
                {
                    //전직할 진로 선택하기
                    hong.SelectCourse();
 
                    //전직 데이터 가져오기
                    promoteData = dataManager.GetPromoteData(hong.info.promote_id, hong.info.gradeIndex);
 
                    //전직
                    hong.Promote(promoteData);
                }
 
                if (level == 30)
                {
                    promoteData = dataManager.GetPromoteData(hong.info.promote_id, hong.info.gradeIndex);
                    hong.Promote(promoteData);
                }
 
                if (level == 60)
                {
                    promoteData = dataManager.GetPromoteData(hong.info.promote_id, hong.info.gradeIndex);
                    hong.Promote(promoteData);
                }
 
                if (level == 100)
                {
                    promoteData = dataManager.GetPromoteData(hong.info.promote_id, hong.info.gradeIndex);
                    hong.Promote(promoteData);
                }
 
                if (level == 140)
                {
                    promoteData = dataManager.GetPromoteData(hong.info.promote_id, hong.info.gradeIndex);
                    hong.Promote(promoteData);
                    break;
                }
 
            }
        }
      
    }
}
 
 

 

 

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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
 
namespace study_016_2
{
    class DataManager
    {
        Dictionary<int, CharacterData> dicCharacterDatas;
        Dictionary<int, PromoteData> dicPromoteDatas;
        List<PromoteData> promoteList;
 
        public DataManager()
        {
 
        }
 
 
        //불러올 데이터를 담을 딕셔너리 생성, 전직데이터를 담을 리스트 생성
        public void Init()
        {
            
            this.dicCharacterDatas = new Dictionary<int, CharacterData>();
            this.dicPromoteDatas = new Dictionary<int, PromoteData>();
            this.promoteList = new List<PromoteData>();
 
 
        }
 
        //데이터 불러오기
 
        public void LoadData()
        {
            string charjson = File.ReadAllText("./character_data.json");
            string promtejson = File.ReadAllText("./promote_data.json");
 
            this.dicCharacterDatas = JsonConvert.DeserializeObject<CharacterData[]>(charjson).ToDictionary(x => x.id, x => x);
            this.dicPromoteDatas = JsonConvert.DeserializeObject<PromoteData[]>(promtejson).ToDictionary(x => x.promote_id, x => x);
            
 
        }
 
        //캐릭터 id에 해당하는 데이터 값 가져오기
        public CharacterData GetDataById(int id)
        {
            return this.dicCharacterDatas[id];
        }
 
        //전직데이터 값 가져오기
        public PromoteData GetPromoteData(int promoteId, int gradeIndex)
        {
 
            foreach (var data in this.dicPromoteDatas)
            {
                if (data.Key== promoteId)
                {
                    
                    promoteList.Add(data.Value);
                    
                } 
            }
 
            return promoteList[gradeIndex];
 
        }
    }
}
 
 

 

 

3. 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace study_016_2
{
    class CharacterInfo
    {
 
        public int id;
        public int promote_id;
        public int level;
        public int exp;
        public string className;
        public int gradeIndex;
 
        public CharacterInfo(int id, int promote_id, int level, int exp, string className, int gradeIndex=0)
        {
            this.id = id;
            this.promote_id = promote_id;
            this.level = level;
            this.exp = exp;
            this.className = className;
            this.gradeIndex = gradeIndex;
 
        }
    }
}
 
 

 

 

4. 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
 
namespace study_016_2
{
    class Character
    {
        public CharacterInfo info;
        public CharacterData data;
 
        public Character(CharacterInfo info, CharacterData chardata)
        {
            this.info = info;
            this.data = chardata;
 
            Console.WriteLine("{0}(직업: {1}, lv: {2} exp: {3})이 생성되었습니다"
                , this.data.name, this.data.class_name, this.info.level, this.info.exp);
        }
 
        //레벨업하고 값 반환하기
        public int Levelup()
        {
            Console.WriteLine("\n레벨이 상승했습니다.");
 
            this.info.level += 1;
            this.info.exp += 100;
 
            Console.WriteLine("현재 레벨 : {0} (누적 exp: {1})",this.info.level,this.info.exp);
 
            return this.info.level;
            
        }
 
        //진로 결정하기
        public void SelectCourse()
        {
            Console.WriteLine("직업을 선택하세요 1: 검사, 2: 궁수");
 
            int input = Int32.Parse(Console.ReadLine());
 
            if (input == 1)
            {
                this.info.promote_id = 1000;
                
            }
            else if (input == 2)
            {
                this.info.promote_id = 1005;
                
            }
 
            return;
        }
 
        //현재 직업의 이름을 변경하고, 다음 전직 데이터를 가져오기위해 값을 변경함
        public void Promote(PromoteData promoteData)
        {
            Console.WriteLine("\n--------------------------------------------------------\n");
            Console.WriteLine("{0}에서 {1}로 전직하였습니다.",this.info.className,promoteData.class_name);
            Console.WriteLine("\n--------------------------------------------------------\n");
 
            this.info.className = promoteData.class_name;
            this.info.promote_id++;
            this.info.gradeIndex++;
        }
 
       
    }
}
 
 

 

 

5. CharacterData class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace study_016_2
{
    class CharacterData
    {
        public int id;
        public string name;
        public string class_name;
        public int promote_id;
        public int level;
        public int exp;
    }
}
 
 

 

 

6. PromoteData class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace study_016_2
{
    class PromoteData
    {
        public int promote_id;
        public string class_name;
        public int promote_level;
    }
}
 
 

 

 

7. 결과값

 

1) 레벨 10 달성하고 진로 선택하기(검사 계열인지, 궁수 계열인지 입력받기)

 

2) 진로 선택하고 레벨 30, 60, 100, 140 달성하고 전직하기

레벨 30 달성하고 전직하기

 

레벨 60 달성하고 전직하기

 

레벨 100 달성하고 전직하기

 

레벨 140 달성하고 전직하기