Dictionary를 이용한 Inventory구현 및 Json사용

C#/수업내용 2019. 4. 3. 23:33


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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
using Newtonsoft.Json;
 
namespace Dictionary_0403
{
    public class App
    {
        Dictionary<int, ItemData> dicItems = new Dictionary<int, ItemData>();
        public App()
        {
            var readJson = File.ReadAllText(@"weapon_data.json");
            Console.WriteLine(readJson);
            var arrItemData = JsonConvert.DeserializeObject<ItemData[]>(readJson);
            foreach(var item in arrItemData)
            {
                Console.WriteLine(item.name);
            }
        }
 
        public void Start()
        {
            Dictionary<stringstring> dicPeople = new Dictionary<stringstring>();
            dicPeople.Add("940915-1111111""홍길동");
            dicPeople.Add("950915-1111111""임꺽정");
 
            var name = dicPeople["940915-1111111"];
            Console.WriteLine("name = {0}", name);
 
            var hasKey = dicPeople.ContainsKey("940915-1111111");
            if(hasKey)
            {
                name = dicPeople["940915-1111111"];
                dicPeople.Remove("940915-1111111");
                Console.WriteLine("{0}", name);
            }
            else
            {
                Console.WriteLine("해당키의 값은 없습니다.");
            }
 
            foreach(KeyValuePair<stringstring> pair in dicPeople)
            {
                Console.WriteLine("{0} : {1}", pair.Key, pair.Value);
            }
        }
 
        public void Start2()
        {
            var item = new ItemData(0"혈마검"701822.3f, 2231.8f);
            dicItems.Add(item.id, item);
            item = new ItemData(1"카를레이의 주장"701735.5f, 2085.8f);
            dicItems.Add(item.id, item);
            item = new ItemData(2"지존의 손잡이 칼"70321.0f, 321.0f);
            dicItems.Add(item.id, item);
            item = new ItemData(3"은장도"701735.5f, 2085.8f);
            dicItems.Add(item.id, item);
            item = new ItemData(4"손잡이 칼"70321.0f, 325.5f);
            dicItems.Add(item.id, item);
            item = new ItemData(5"그린스톤 경의 부채"701735.5f, 2085.8f);
            dicItems.Add(item.id, item);
            foreach(KeyValuePair<int, ItemData> pair in dicItems)
            {
                Console.WriteLine("\n\n{0}. {1}\n 레벨:{2}, 데미지: {3}~{4}", pair.Value.id, pair.Value.name, pair.Value.maxLevel, pair.Value.damageMin, pair.Value.damageMax);
            }
 
            var firstItem = this.CreateItem(0);
            var secondItem = this.CreateItem(1);
            var thirdItem = this.CreateItem(2);
            var fourthItem = this.CreateItem(3);
            var fifthtItem = this.CreateItem(4);
            var sixthtItem = this.CreateItem(5);
            Console.WriteLine("\n\n{0}. {1}\n 레벨:{2}, 데미지:{3}", firstItem.itemInfo.id, dicItems[firstItem.itemInfo.id].name, dicItems[firstItem.itemInfo.id].maxLevel, firstItem.itemInfo.damage);
            Console.WriteLine("\n\n{0}. {1}\n 레벨:{2}, 데미지:{3}", secondItem.itemInfo.id, dicItems[secondItem.itemInfo.id].name, dicItems[secondItem.itemInfo.id].maxLevel, secondItem.itemInfo.damage);
            Console.WriteLine("\n\n{0}. {1}\n 레벨:{2}, 데미지:{3}", thirdItem.itemInfo.id, dicItems[thirdItem.itemInfo.id].name, dicItems[thirdItem.itemInfo.id].maxLevel, thirdItem.itemInfo.damage);
            Console.WriteLine("\n\n{0}. {1}\n 레벨:{2}, 데미지:{3}", fourthItem.itemInfo.id, dicItems[fourthItem.itemInfo.id].name, dicItems[fourthItem.itemInfo.id].maxLevel, fourthItem.itemInfo.damage);
            Console.WriteLine("\n\n{0}. {1}\n 레벨:{2}, 데미지:{3}", fifthtItem.itemInfo.id, dicItems[fifthtItem.itemInfo.id].name, dicItems[fifthtItem.itemInfo.id].maxLevel, fifthtItem.itemInfo.damage);
            Console.WriteLine("\n\n{0}. {1}\n 레벨:{2}, 데미지:{3}", sixthtItem.itemInfo.id, dicItems[sixthtItem.itemInfo.id].name, dicItems[sixthtItem.itemInfo.id].maxLevel, sixthtItem.itemInfo.damage);
        }
 
        public Item CreateItem(int id)
        {
            double damage = this.GetRandomNumber(dicItems[id].damageMin, dicItems[id].damageMax);
            ItemInfo info = new ItemInfo(id, dicItems[id].maxLevel, (float)damage);
            return new Item(info);
        }
        public double GetRandomNumber(double minimum, double maximum)
        {
            Random rand = new Random();
            return rand.NextDouble() * (maximum - minimum) + minimum;
        }
    }
}
 
cs


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 Dictionary_0403
{
    public class Item
    {
        public ItemInfo itemInfo;
 
        public Item(ItemInfo itemInfo)
        {
            this.itemInfo = itemInfo;
        }
    }
}
 
cs


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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Dictionary_0403
{
    public class ItemData
    {
        public int id;
        public string name;
        public int maxLevel;
        public float damageMin;
        public float damageMax;
 
        public ItemData(int id, string name, int maxLevel, float damageMin, float damageMax)
        {
            this.id = id;
            this.name = name;
            this.maxLevel = maxLevel;
            this.damageMin = damageMin;
            this.damageMax = damageMax;
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Dictionary_0403
{
    public class ItemInfo
    {
        public int id;
        public int level;
        public float damage;
 
        public ItemInfo(int id, int level, float damage)
        {
            this.id = id;
            this.level = level;
            this.damage = damage;
        }
    }
}
 
cs



Json형식의 파일을 역직렬화하여 Item객체를 생성하였다.

역직렬화 하는데 Newtonsoft.Json 패키지를 사용하였다.

: