아카이브

Dictionary 인벤토리* 본문

C#프로그래밍

Dictionary 인벤토리*

timbercat 2023. 7. 26. 18:25

App

using LearnDotnet;
using System;
using System.Collections;
using System.Collections.Generic;
//using 지시문을 사용하면 네임스페이스에 정의된 형식을 해당 형식의 정규화된 네임스페이스를 지정하지 않고도 사용할 수 있습니다.
namespace LearnDotnet
{
    public class App
    {
        //생성자 
        public App()
        {
            //초기화
            Dictionary<int, ItemData> dic = new Dictionary<int, ItemData>();
            dic.Add(100, new ItemData(100, "장검", 0, 8));
            dic.Add(101, new ItemData(101, "단검", 1, 5));
            dic.Add(102, new ItemData(102, "활", 2, 6));
            dic.Add(103, new ItemData(103, "도끼", 3, 10));

            Inventory inven = new Inventory(3);
            inven.AddItem(new Item(dic[100])); //장검
            inven.AddItem(new Item(dic[102])); //활
            inven.AddItem(new Item(dic[103])); //도끼

            Item item = inven.GetItem(100);
        }
    }
}

인벤토리

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class Inventory
    {
        //아이템을 그룹/관리
        //배열/컬렉션
        List<Item> items; //변수 정의
        int capacity;

        //생성자
        public Inventory(int capacity)
        {
            this.capacity = capacity;
            //컬렉션은 사용자 인스턴스화
            this.items = new List<Item>();
            Console.WriteLine("{0}칸짜리 가방이 생성되었습니다.", this.capacity);
            Console.WriteLine("{0}칸짜리 가방이 생성되었습니다.[{1},{2}]", this.capacity, this.items.Count, this.capacity);
        }

        //아이템 추가
        public void AddItem(Item item)
        {
            this.items.Add(item);
            Console.WriteLine("{0}이 추가되었습니다.[{1},{2}]", item.data.name, this.items.Count, this.capacity);
        }

        public Item GetItem(int id)
        {

        }

    }
}

아이템 데이터

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class ItemData
    {
        public int id;
        public string name;
        public int item_type;
        public int damage;

        //생성자 
        public ItemData(int id, string name, int item_type, int damage)
        {
            this.id = id;
            this.name = name;
            this.item_type = item_type;
            this.damage = damage;
        }
    }
}

아이템

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    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 LearnDotnet
{
    internal class MonsterData
    {
        public int id;
        public string name;
        public int item_id;
        public int hp;

        //생성자
        public MonsterData(int id, string name, int item_id, int hp)
        {
            this.id = id;
            this.name = name;
            this.item_id = item_id;
            this.hp = hp;
        }
    }    
}

몬스터

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class Monster
    {
        public MonsterData data;
        Dictionary<int, ItemData> dicItemDatas;
        public int hp;
        
        //생성자
        public Monster(Dictionary<int, ItemData> dicItemDatas, MonsterData data) 
        {
            this.dicItemDatas = dicItemDatas;
            this.data = data;
            this.hp = this.data.hp;
        }

        //죽는다
        public Item Die()
        {
            Console.WriteLine("{0}가 죽었습니다.", this.data.name);
            ItemData itemData = this.dicItemDatas[this.data.item_id];            
            return new Item(itemData);           
        }

        public void HitDamage(int damage)
        {
            this.hp -= damage;            
        }

        public int Gethp()
        {
            return this.hp;
        }
    }
}

아이템-웨폰

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class Weapon : Item
    {
        public Weapon(ItemData data) : base(data)
        {

        }
    }
}

아이템-아머

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class Armor : Item
    {
        public Armor(ItemData data) : base(data)
        {

        }
    }
}

아이템-포션

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class Potion : Item
    {
        public Potion(ItemData data) : base(data)
        {

        }
    }
}

'C#프로그래밍' 카테고리의 다른 글

Action 대리자 연습(HitDamage)  (0) 2023.07.27
Action 대리자 연습  (0) 2023.07.27
Dictionary 연습2  (0) 2023.07.26
Dictionary 연습  (0) 2023.07.26
2차원 배열 연습  (0) 2023.07.25