2019-10-17 19일차 클래스의 상속 뽑기상자, json사용, get;set사용 , rand.Next사용

Console Programming/C# Console 2019. 10. 18. 08:51

Equipment

    string name 

 

Weapon : Equipment

Armor : Equipment 

 

 

뽑기상자 

-----------------------

이름 

아이템들 List<Equipment>

-----------------------

상자를 연다 

 

 

 

무기상자 : 뽑기상자 

무기상자를 열었더니 장검, 대검, 도끼가 나왔습니다.

 

 

방어구상자 : 뽑기상자 

방어구상자를 열었더니 가죽옷, 천옷, 사슬옷이 나왔습니다.

 

 

뽑기상자를 열면 무기상자인지 방어구상자인지 무작위로 결정되고

상자가 결정되면 그 상자에 맞는 아이템 중에서 무작위로 아이템이 생성된다.

 

출력은 아이템 이름이 나온다.

 

 

 

item_data.json

[
  {
    "item_id": 1000,
    "name": "단검",
    "type": 1
  },
  {
    "item_id": 1100,
    "name": "장검",
    "type": 1
  },
  {
    "item_id": 1200,
    "name": "대검",
    "type": 1
  },
  {
    "item_id": 1300,
    "name": "세검",
    "type": 1
  },
  {
    "item_id": 2000,
    "name": "천옷",
    "type": 2
  },
  {
    "item_id": 2000,
    "name": "가죽갑옷",
    "type": 2
  },
  {
    "item_id": 2000,
    "name": "사슬갑옷",
    "type": 2
  },
  {
    "item_id": 2000,
    "name": "판금갑옷",
    "type": 2
  }
]

 

 

Program.cs

using System;
namespace Syntax06
{
    class Program
    {
        static void Main(string[] args)
        {
            new App();

            Console.ReadKey();
        }
    }
}

App.cs

using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
namespace Syntax06
{
    public class App
    {
        public App()
        {
            var itemdata = MakeItemData();           
            
            List listItemData = new List();

            foreach(var data in itemdata)
            {
                listItemData.Add(data);
            }

            List listWeaponData = new List();
            foreach(var weaponlist in listItemData)
            {
                if(weaponlist.item_id>999 && weaponlist.item_id<2000)
                {
                    listWeaponData.Add(weaponlist);
                }
            }

            List listArmordata = new List();
            foreach (var armorlist in listItemData)
            {
                if (armorlist.item_id > 1999 && armorlist.item_id < 3000)
                {
                    listArmordata.Add(armorlist);
                }
            }


            var item1 = new Item();

            var gachaBox = new GachaBox();
            
                var randomBox1 = gachaBox.DoGacha();

                if (randomBox1.GetType() == typeof(WeaponBox))
                {
                    item1.listItemData = listWeaponData;
                }
                else
                {
                    item1.listItemData = listArmordata;
                }
                //Console.WriteLine(randomBox1.GetType());
           

            Random rand = new Random(unchecked((int)DateTime.Now.Ticks));

            int rNumber = rand.Next(4);

            if(rNumber==0)
            {
                Console.WriteLine(item1.listItemData[0].name);
            }
            else if(rNumber == 1)
            {
                Console.WriteLine(item1.listItemData[1].name);
            }
            else if(rNumber == 2)
            {
                Console.WriteLine(item1.listItemData[2].name);
            }
            else
            {
                Console.WriteLine(item1.listItemData[3].name);
            }
            //Console.WriteLine(item1.listItemData[0].name);
            //Console.WriteLine(item1.listItemData[1].name);
            //Console.WriteLine(item1.listItemData[2].name);
            //Console.WriteLine(item1.listItemData[3].name);




        }
        public ItemData[] MakeItemData()
        {
            ItemData[] itemdata;
            string path = @"C:\Workspace\Console1017\Syntax06\bin\Debug\item_data.json";
            string json = File.ReadAllText(path);
            itemdata = JsonConvert.DeserializeObject<ItemData[]>(json);
            return itemdata;
        }
    }
    
}

GachaBox.cs

using System;
namespace Syntax06
{
    public class GachaBox
    {
        public Item Item { get; set; }
        public GachaBox()
        {
            
        }
        public virtual GachaBox DoGacha()
        {
            GachaBox box=null;
            Random rand = new Random(unchecked((int)DateTime.Now.Ticks));

            int rNumber = rand.Next(2);

            if(rNumber == 0)
            {
                box = new WeaponBox();
            }
            else if(rNumber ==1)
            {
                box = new ArmorBox();
            }
            return box;
        }
    }
}

WeaponBox.cs


namespace Syntax06
{
    public class WeaponBox : GachaBox
    {
        public WeaponBox()
        {

        }
        public override GachaBox DoGacha()
        {
            return null;
        }
    }
}

ArmorBox.cs

namespace Syntax06
{
    public class ArmorBox : GachaBox
    {
        public ArmorBox()
        {

        }
        public override GachaBox DoGacha()
        {
            return null;
        }
    }
}

Item.cs

using System.Collections.Generic;

namespace Syntax06
{
    public class Item
    {
        public List listItemData { get; set; }

        public Item()
        {

        }
    }
}

ItemData.cs

namespace Syntax06
{
    public class ItemData
    {
        public int item_id { get; set; }
        public string name { get; set; }
        public int type { get; set; }
         
        public ItemData()
        {

        }
    }
}

 

 

: