C#/수업내용

2020.04.24. 수업내용 - 승급하기(바람의 나라)

dev_sr 2020. 4. 25. 19:48

사용한 엑셀테이블

 

1) character_data

 

2) advancement_data

 

*테이블 항목 뒤에 공백이 있으면 컨버터 해도 0만 나온다..

 

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study_016
{
    class App
    {
        public App()
        {
            //데이터 메니저 객체 생성하기
            DataManager dataManager = new DataManager();
            //초기화 호출
            dataManager.Init();
            //데이터 불러오기
            dataManager.LoadData();
            //저장한 데이터에서 아이디로 캐릭터 데이터 불러오기
            CharacterData data = dataManager.GetCharacterDataById(1000);
            //캐릭터 info data만들기(변할 값만 저장한다)
            CharacterInfo Info = new CharacterInfo(data.advancement_id,data.name,data.hp,data.mp,0);
            //캐릭터 생성하기
            Character character = new Character(Info, data);
            //승급 데이터 가져오기
            AdvancementData NextAdvancementData = dataManager.GetAdvancementDataById(character.info.advancement_id, character.info.grade);
            //다음 승급 데이터 가져오기
            AdvancementData NextAdvancementData2 = dataManager.GetAdvancementDataById(character.info.advancement_id+1character.info.grade+1);
            //레벨업 하기
            character.Levelup(NextAdvancementData, NextAdvancementData2); 
        }
    }
}
 
 

 

 

2. CharacterData 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_016
{
    class CharacterData
    {
        public int id;
        public string name;
        public int hp;
        public int mp;
        public int advancement_id;
        public int require_level;
 
    }
}
 
 

 

 

3. AdvancementData class

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_016
{
    class AdvancementData
    {
        public int id;
        public string name;
        public int hp;
        public int mp;
    }
}
 
 

 

 

4. 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
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
{
    class DataManager   
    {
        //데이터들을 불러오고 dictionary로 관리
        Dictionary<int, CharacterData> dicCharacterDatas;
        Dictionary<int, AdvancementData> dicAdvancementDatas;
        List<AdvancementData> advanceDataList;
 
        public DataManager()
        {
 
        }
 
        /// <summary>
        /// 초기화를 한다 (데이터를 담을 딕셔너리 생성)
        /// </summary>
        public void Init()
        {
            dicCharacterDatas = new Dictionary<int, CharacterData>();
            dicAdvancementDatas = new Dictionary<int, AdvancementData>();
            advanceDataList = new List<AdvancementData>();
 
        }
 
        /// <summary>
        /// 데이터 불러오고 딕셔너리에 할당하기
        /// </summary>
        public void LoadData()
        {
            string charjson = File.ReadAllText("./character_data.json");
            string advanjson = File.ReadAllText("./advancement_data.json");
 
            CharacterData[] arrCharacterDatas = JsonConvert.DeserializeObject<CharacterData[]>(charjson);
            
 
            foreach(CharacterData data in arrCharacterDatas)
            {
                this.dicCharacterDatas.Add(data.id, data);   
            }
 
            //딕셔너리에 바로 할당하기
            this.dicAdvancementDatas = JsonConvert.DeserializeObject<AdvancementData[]>(advanjson).ToDictionary(x => x.id, x => x);
        }
 
        /// <summary>
        /// 아이디에 해당하는 캐릭터 데이터 반환하기
        /// </summary>
        /// <param name="id"></param>
        public CharacterData GetCharacterDataById(int id)
        {
            return this.dicCharacterDatas[id];
        }
 
        /// <summary>
        /// 전달받은 아이디 값이랑 인덱스로 딕셔너리에서 해당 AdvancementData값들을 찾아서 반환해주기
        /// </summary>
        /// <param name="advanceID"></param>
        /// <param name="grade"></param>
        /// <returns></returns>
 
        public AdvancementData GetAdvancementDataById(int advanceID, int grade)
        {
            AdvancementData data = this.dicAdvancementDatas[advanceID];
 
            if (advanceID == data.id)
            {
                advanceDataList.Add(data);
            }
 
            return advanceDataList[grade];
        }
    }
}
 
 

 

 

5. 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.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_016
{
    class CharacterInfo
    {
        //변할 값을 지정한다.
        public int level;   //레벨
        public int hp;  //체력
        public int mp;  //마력
        public string name; //이름
        public int grade;   //인덱스
        public int advancement_id; //변할 아이디
 
        public CharacterInfo(int advancement_id, string name, int hp, int mp, int grade, int level = 1 )
        {
            this.name = name;
            this.level = level;
            this.hp = hp;
            this.mp = mp;
            this.grade = grade;
            this.advancement_id = advancement_id;
        }
    }
}
 
 

 

 

6. 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
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_016
{
    class Character
    {
        public CharacterData data;
        public CharacterInfo info;
 
        int currenthp;
        int currentmp;
 
        public Character(CharacterInfo info, CharacterData data)
        {
            this.data = data;
            this.info = info;
 
            this.currenthp = info.hp;
            this.currentmp = info.mp;
 
            Console.WriteLine("{0}가 생성되었습니다."this.info.name);
            Console.WriteLine("Lv.{0} ( {1}/{2} , {3}/{4} )"this.info.level, this.currenthp, this.info.hp
                , this.currentmp, this.info.mp);
            Console.WriteLine();
        }
 
        public void Levelup(AdvancementData currentData, AdvancementData NextData)
        {
            this.info.level = 99;
 
            if (this.info.advancement_id==currentData.id && this.info.level >= 99)
            {
                this.info.hp = currentData.hp;
                this.info.mp = currentData.mp;
 
                this.currenthp = info.hp;
                this.currentmp = info.mp;
 
                Console.WriteLine("{0}가 99 레벨이 되었습니다."this.info.name);
                Console.WriteLine("Lv.{0} ( {1}/{2} , {3}/{4} )"this.info.level, this.currenthp, this.info.hp
                    , this.currentmp, this.info.mp);
 
 
            }
 
 
            //전직하기
            if (this.info.hp >= currentData.hp && this.info.mp >= currentData.mp)
            {
                Console.WriteLine("\n{0}에서 {1}으로 승급 하였습니다."this.info.name, currentData.name);
                Console.WriteLine("Lv.{0} ( {1}/{2} , {3}/{4} )"this.info.level, this.currenthp, this.info.hp
              , this.currentmp, this.info.mp);
                this.info.name = currentData.name;
 
                //다음 전직 테스트를 위해 hp mp를 다음 전직클래스(검제)의 hp, mp로 변경
 
                this.info.hp = NextData.hp;
                this.info.mp = NextData.mp;
 
                this.currenthp = info.hp;
                this.currentmp = info.mp;
 
            }
 
            if(this.info.hp >= currentData.hp && this.info.mp >= currentData.mp)
            {
                Console.WriteLine("\n{0}에서 {1}으로 승급 하였습니다."this.info.name, NextData.name);
                Console.WriteLine("Lv.{0} ( {1}/{2} , {3}/{4} )"this.info.level, this.currenthp, this.info.hp
             , this.currentmp, this.info.mp);
                this.info.name = NextData.name;
 
            }
 
        }
 
    }
}
 
 

 

 

7.  결과 값