자료구조

Dictionary + 인벤토리

hyunjin-dev-log 2021. 12. 26. 00:00

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class App
    {
        Dictionary<int, ItemData> dicItemDatas;
        //생성자
        public App()
        {
            this.dicItemDatas = new Dictionary<int, ItemData>();

            ItemData data0 = new ItemData(100, "장검", 5f);
            ItemData data1 = new ItemData(101, "단검", 3.4f);
            ItemData data2 = new ItemData(102, "활", 4f);
            ItemData data3 = new ItemData(103, "창", 5.3f);
            ItemData data4 = new ItemData(104, "지팡이", 3f);

            this.dicItemDatas.Add(data0.id, data0);
            this.dicItemDatas.Add(data1.id, data1);
            this.dicItemDatas.Add(data2.id, data2);
            this.dicItemDatas.Add(data3.id, data3);
            this.dicItemDatas.Add(data4.id, data4);

            Console.WriteLine(this.dicItemDatas.Count);

            ItemData itemData = this.dicItemDatas[100];
            Item item = new Item(itemData);

            Inventory inventory = new Inventory();
            inventory.AddItem(item);

            Item item1 = new Item(this.dicItemDatas[101]);
            inventory.AddItem(item1);

            inventory.AddItem(new Item(this.dicItemDatas[102]));

            Console.WriteLine("count: {0}", inventory.Count);

            Item foundItem = inventory.GetItem(100);
            if (foundItem != null)
            {
                Console.WriteLine("찾은 아이템의 id: {0}", foundItem.data.name);
                Console.WriteLine("count: {0}", inventory.Count);
            }
            else
            {
                Console.WriteLine("아이템을 찾지 못했습니다.");
            }

            inventory.PrintItems();
            inventory.AddItem(foundItem);
            Console.WriteLine();
            inventory.PrintItems();
            Console.WriteLine();

            inventory.AddItem(new Item(this.dicItemDatas[100]));
            inventory.PrintItems();
            Console.WriteLine();

            Item foundItem1 = inventory.GetItem(100);
            if (foundItem1 != null)
            {
                Console.WriteLine("꺼낸아이템: {0} {1} x {2}", foundItem1.data.id, foundItem1.data.name, foundItem1.amount);
            }
            Console.WriteLine("인벤토리에 남아있는 아이템");
            inventory.PrintItems();
        }
    }
}

인벤토리

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Inventory
    {
        //Item객체들을 그룹화하고 관리
        //그룹화하는 방법 : 배열을 사용, 컬렉션 사용
        private List<Item> list;    //동적배열 List<>
        public int Count
        {
            get
            {
                return list.Count;
            }
        }

        //생성자
        public Inventory()
        {
            this.list = new List<Item>();
            Console.WriteLine("인벤토리가 생성되었습니다.");
        }

        public void AddItem(Item item)
        {
            Item foundItem = null;   //임시공간

            for (int i = 0; i < this.list.Count; i++)
            {
                if (this.list[i].data.id == item.data.id)
                {
                    foundItem = this.list[i];
                    break;
                }
            }

            //리스트에 없다면
            if (foundItem == null)
            {
                this.list.Add(item);
            }
            else//있다면
            {
                foundItem.amount++;
            }
            Console.WriteLine("아이템({0})이 추가되었습니다.", item.data.id);
        }

        public Item GetItem(int id)     //id : ItemData의 id
        {
            Item item = null;

            for(int i = 0; i < this.list.Count; i++)
            {
                if (this.list[i].data.id == id)
                {
                    item = this.list[i];
                    break;
                }
            }

            if (item != null)
            {
                if(item.amount == 1)
                {
                    //찾은 아이템의 개수가 1이라면 리스트에서 제거해준다
                    this.list.Remove(item);
                }
                else
                {
                    //그렇지 않다면 item의 개수를 1 감소시킨다 + 리스트에서 제거하지 않는다
                    item.amount--;
                }
                //중복되지 않도록 반환하는값을 새로만들어서 해준다.
                ItemData data = new ItemData(item.data.id, item.data.name, item.data.damage);
                Item rtnItem = new Item(data);
                return rtnItem;
            }
            else
            {
                return null;
            }
        }

        public void PrintItems()
        {
            if (this.list.Count <= 0)
            {
                Console.WriteLine("인벤토리가 비어있습니다.");
                return;
            }

            foreach (Item item in this.list)
            {
                Console.WriteLine("{0} {1} x {2}", item.data.id, item.data.name, item.amount);
            }
        }
    }
}

아이템

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    class Item
    {
        public ItemData data;
        public int amount = 1;

        //생성자
        public Item(ItemData data)
        {
            this.data = data;
        }
    }
}

아이템데이터

더보기
using System;

namespace HelloWorld
{
    class ItemData
    {
        public int id;
        public string name;
        public float damage;

        public ItemData(int id, string name, float damage)
        {
            this.id = id;
            this.name = name;
            this.damage = damage;
        }
    }
}