<n+1번째 인벤토리> Json과 파일읽기.

C#/수업내용 2019. 4. 3. 19:00
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 _20190403
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
            app.Start();
 
            Console.ReadKey();
        }
    }
}
 
cs

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 _20190403
{
    public class Item
    {
        public ItemData itemData;
        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 _20190403
{
    public class ItemData
    {
        public int id;
        public string name;
        public int max_level;
        public float damage_min;
        public float damage_max;
 
        public ItemData(int id, string name, int max_level, float damage_min, float damage_max)
        {
            this.id = id;
            this.name = name;
            this.max_level = max_level;
            this.damage_min = damage_min;
            this.damage_max = damage_max;
        }
    }
}
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190403
{
    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

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
 
using Newtonsoft.Json;
 
namespace _20190403
{
    public class App
    {
        Dictionary<int, ItemData> dicItems = new Dictionary<int, ItemData>();
        public App()
        {
            var json = File.ReadAllText("weapon_data.json");
            Console.WriteLine(json);
            var arrItemData = JsonConvert.DeserializeObject<ItemData[]>(json);
            foreach (var item in arrItemData)
            {
                dicItems.Add(item.id, item);
            }
        }
        
        public void Start()
        {
            for (int i = 0; i<dicItems.Count;i++)
            {
                var item = CreateItem(i);
                var data = dicItems[item.itemInfo.id];
                Console.WriteLine("{0}, {1}:{2:0.0}~{3:0.0}", item.itemInfo.id, dicItems[i].name, dicItems[i].damage_min, dicItems[i].damage_max);
            }
        }
 
        public Item CreateItem(int id)
        {
            var itemData = dicItems[id];
            var damage = this.GetRandomNumber(itemData.damage_min, itemData.damage_max);
            return new Item(new ItemInfo(itemData.id, 1, (float)damage));
        }
 
        public double GetRandomNumber(double minimum, double maximum)
        {
            var random = new Random();
            return random.NextDouble() * (maximum - minimum) + minimum;
        }
    }
}
 
cs


: