티스토리 뷰

C#/연습장

List를 이용한 인벤토리 만들기

Game Client Lee Hwanguk 2023. 1. 22. 01:10
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListInventory
{
    internal class Inventory
    {
        List<Weapon> weapons;
        public int capacity;
        
        public int Count
        {
            get;
            set;
        }
        public Inventory(int capacity) 
        {
            this.capacity = capacity;
            this.weapons=new List<Weapon>();
            //Console.WriteLine(weapons);
        }
        public void AddItem(Weapon weapon) //매개변수
        {
            Weapon findItem = null; //비교할 변수를 하나 만들어야함
            for (int i = 0; i < this.weapons.Count; i++) //for문을 돌며 weapons를 순회
            {
                if (this.weapons[i].Name == weapon.Name) //weapons 리스트에Name과 매개변수의 weapon 의 Name이 같다면
                {
                    findItem = weapons[i]; //findItem에 weapons[i] 할당 하고 
                    break; //break;
                }
            }
            if (Count < capacity) //Count<5
            {
                if (weapons.Contains(findItem)) //Contains로 findItem 인스턴스 찾기
                {
                    findItem.count++; //findItem count ++
                }
                else
                {
                    weapons.Add(weapon);
                    weapon.count++;
                }
                this.Count++;
            }
            else
            {
                Console.WriteLine("인벤토리가 가득 찼습니다");
            }

        }

        public void PrintAllItems()
        {
            for (int i = 0; i < weapons.Count; i++)
            {
                Console.WriteLine("{0} X {1}", weapons[i].Name, weapons[i].count);
            }

        }
        public Weapon GetItem(string weaponName)
        {
            Weapon findItem = null;
            for (int i = 0; i < this.weapons.Count; i++)
            {
                if (this.weapons[i].Name == weaponName)
                {
                    findItem = this.weapons[i];
                    if (findItem.count > 1)
                    {
                        findItem.count--;
                    }
                    else
                    {
                        this.weapons.Remove(this.weapons[i]);
                    }

                    if (this.Count > 0)
                    {
                        this.Count--;
                    }

                    break;
                }
            }
            return findItem;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ListInventory
{
    internal class App
    {
        public App() 
        {
            Inventory inven = new Inventory(5);
            inven.AddItem(new Weapon("장검"));
            inven.AddItem(new Weapon("장검"));
            inven.AddItem(new Weapon("단검"));

            inven.PrintAllItems();
            //장검x2
            //단검x1

            Weapon sword = inven.GetItem("장검"); //return받은 값
            Console.WriteLine(sword.Name);  //장검 

            Console.WriteLine(inven.Count); //2

            inven.PrintAllItems();
            //장검 x 1
            //단검 x 1


            Weapon dagger = inven.GetItem("단검");
            Console.WriteLine(dagger.Name); //단검 

            Console.WriteLine(inven.Count); //1

            inven.PrintAllItems();
            //장검 x 1


        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace ListInventory
{
    internal class Weapon:Item
    {
        public string Name
        {
            get; //읽기 가능 
            private set; //쓰기 불가(private)
        }

        public int count;
        
        public Weapon(string name)
        {
            
            this.Name = name;
        }
    }
}

 

'C# > 연습장' 카테고리의 다른 글

1.30  (0) 2023.01.30
배열을 이용한 인벤토리 만들기 2  (0) 2023.01.19
배열을 이용해 인벤토리 만들기 연습 1  (0) 2023.01.18
대리자 (익명메서드 + 람다식)  (0) 2023.01.11
Queue<T> 연습  (0) 2023.01.10
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
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
글 보관함