C#

인벤토리2

송현호 2023. 7. 26. 16:01
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;

namespace Starcraft
{
    internal class App
    {
        //생성자
        public App()
        {
            Dictionary<int,ItemData> dicI = new Dictionary<int, ItemData> ();
            dicI.Add(100, new ItemData(100, "장검", 8));
            dicI.Add(101, new ItemData(101, "단검", 5));
            dicI.Add(102, new ItemData(102, "활", 6));
            dicI.Add(103, new ItemData(103, "도끼", 10));

            Inventory inven = new Inventory(3);
            inven.AddItem(new Item(dicI[100]));
            inven.AddItem(new Item(dicI[101]));
            inven.AddItem(new Item(dicI[102]));
            Item item = inven.GetItem(100);
            Console.WriteLine(item.GetName());
            Console.WriteLine(inven.items.Count);
            inven.PrintAllItems();
            //단검
            //활
            inven.AddItem(new Item(dicI[100]));
            inven.AddItem(new Item(dicI[101]));
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        public string GetName()
        {
            //반환값 
            return this.data.name;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    //아이템의 정보
    internal class ItemData
    {
        public int id;
        public string name;
        public int damage;
        //생성자
        public ItemData(int id, string name, int damage)
        {
            this.id = id;
            this.name = name;
            this.damage = damage;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class Inventory
    {
        public List<Item> items;
        int capacity;
        //생성자
        public Inventory(int capacity) 
        {
            this.capacity = capacity;
            this.items = new List<Item>();
            Console.WriteLine("{0}칸짜리 가방이 생성되었습니다. ({1}/{0})", this.capacity,this.items.Count);
        }

        public void AddItem(Item item)
        {
            if (this.items.Count < this.capacity)
            {
                this.items.Add(item);
                Console.WriteLine("{0}을 가방에 넣었습니다 ({1}/{2})", item.data.name, this.items.Count, this.capacity);
            }

            else
            {
                Console.WriteLine("가방이 가득찼습니다 ({0}/{1})", this.items.Count,this.capacity) ;
            }

        }

        public Item GetItem(int num)
        {
            foreach (Item item in items)
            {
                if (item.data.id == num)
                {
                    this.items.Remove(item);
                    Console.WriteLine("{0}을 가방에서 꺼냈습니다. ({1}/{2})",item.data.name,this.items.Count,this.capacity);
                    return item;
                }
            }
            return null;
        }

        public void PrintAllItems()
        {
            foreach (Item item in this.items)
            {
                Console.WriteLine(item.data.name);
            }
        }
    }
}

'C#' 카테고리의 다른 글

대리자 연습 1,2  (0) 2023.07.27
싱글톤 패턴으로 대리자 호출  (0) 2023.07.27
몬스터 아이템 드랍  (0) 2023.07.26
2차원 배열  (0) 2023.07.25
2048  (0) 2023.07.25