티스토리 뷰

C#/연습장

배열을 이용해 인벤토리 만들기 연습 1

Game Client Lee Hwanguk 2023. 1. 18. 20:31
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp8
{
    internal class App
    {
        //1.(1차원)배열을 이용한 인벤토리 만들기 연습 1

        public App()
        {
            //1.Item 클래스를 만들고 속성(name)를 부여하자
            //2.Inventory 클래스를 만들고 Item 인스턴스를 생성하고
            //3.Inventory 에서 만든 배열(intventory 필드)에 넣어보자(AddItem 메서드를 통해)
            //4.Inventory.PrintItems 로 들어있는 아이템 인스턴스 출력
            //5.FindItemByName 으로 아이템 name으로 배열 요소 찾기   

            Inventory inventory=new Inventory(5); //Inventory 인스턴스생성
            Console.WriteLine("{0}의 용량의 인벤토리가 생성",inventory.capacity);
            //inventory의 메서드들을 호출(AddItem,PrintItem,FindItemName)

            //1.AddItem
            inventory.AddItem(new Item("장검"));
            inventory.AddItem(new Item("장검"));
            inventory.AddItem(new Item("단검"));
            Console.WriteLine();
            int cnt=inventory.GetItemCount(); //return받은 count값을 int cnt에 할당
            Console.WriteLine("{0}개의 아이템이 들어있다", cnt);
            inventory.PrintItem();
            //inventory.PrintItem();
            //inventory.FindItemName();



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

namespace ConsoleApp8
{
    internal class Inventory
    {
        public Item[] items; //Item 타입의 items배열 '정의'
        public  int capacity; //items의 최대용량 //초기값 0
        //List와 Dictionary로도 만들어보자
        public int col = 0; //items에 요소를 어디에 추가할건지(0번쨰부터)

        //생성자
        public Inventory(int capacity) //인스턴스 생성시 capacity 5할당
        {
            this.capacity = capacity; //5
            this.items = new Item [this.capacity]; //Item 타입의 용량이 index(1)칸인 items(가방)을 만듬
        }

       public void AddItem(Item item)
        {
            items[col]= item; //0번째 인덱스에만 들어가는게 아니라 차래대로 쌓인다
            
            Console.WriteLine("{0}을 인벤토리에 추가",items[col].name); //items[0] 장검, items[1] 장검, items[2] 단검
            col++;
            
        }

        public void PrintItem()
        {
            //null 체크 해보자(null 이면 [ ])
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] != null)
                {
                    Console.WriteLine(items[i].name);

                }
                else if (items[i] == null)
                {
                    Console.WriteLine("[    ]");
                }

            }
            Console.WriteLine();
            
        }
        public int GetItemCount() //void 대신 return시킬 값의 타입 int
        {
            int count = 0;
            for(int j=0; j<items.Length; j++)
            {
                if (items[j] != null)
                    count++;
            }
            return count; //count값은 메서드 호출이 끝나면 사라짐으로 return 
        }

        public void FindItemName() //매개변수로 item.name을 받아서
        {
            //items에 있는 요소의 name으로 찾아서 출력
            //Console.WriteLine(this.items[index].name);
        }
    }
}

#Inventory의 메서드인 AddItem, GetItemCount, PrintItem까지 구현해봤다

#col++를 해주지 않으면 계속 같은 첫번째 자리에 들어간다 맴버변수로 col=0을 지정해주고

아이템이 추가되면 col++을 해줘야한다

#FindItemName과 Arrange도 구현해봐야겠다

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

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