게임 알고리즘

1일차 미니테스트 (정적배열 인벤토리 만들기)

park-gom 2024. 8. 12. 14:06

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Schema;

namespace HelloWorld
{
    internal class Inventory
    {
        Item[] itemlist;

        //생성자
        public Inventory(int n)
        {
            itemlist = new Item[n];
            Console.WriteLine($"인벤토리 {n}칸 생성 성공");
        }

        //인벤토리에 아이템을 추가하는 메서드
        public void AddItem(Item item)
        {
            bool chk = true;
            for (int i = 0; i < itemlist.Length; i++)
            {
                if (itemlist[i] == null)
                {
                    itemlist[i] = item;
                    Console.WriteLine($"{i + 1}번칸에 {item.Name}을(를) 넣었습니다");
                    chk = false;
                    break;
                }
            }

            if (chk)
            {
                Console.WriteLine("AddItem 실패, 빈공간이 없습니다");
            }
        }

        //인벤토리에 모든 아이템을 출력하는 메서드
        public void ShowAllItems()
        {
            for (int i = 0; i < itemlist.Length; i++)
            {
                Item item = itemlist[i];
                if (itemlist[i] == null)
                {
                    Console.WriteLine($"{i+1}. [비어있음]");
                }
                else
                {
                    Console.WriteLine($"{i + 1}. {item.Name}");
                }
            }
        }

        //인벤토리내에 특정 문자열과 일치하는 아이템을 찾는 메서드
        public Item FindItem(string name)
        {
            Item item;

            for (int i = 0; i < itemlist.Length; i++)
            {
                item = itemlist[i];
                if( item.Name == name)
                {
                    return item;
                }
            }

            Console.WriteLine("해당 아이템이 없습니다.");
            return null;
        }

        //인벤토리에 있는 장비의 갯수를 세는 메서드
        public int Count()
        {
            int cnt = 0;

            for (int i = 0;i < itemlist.Length; i++)
            {
                if (itemlist[i] != null)
                {
                    cnt++;
                }
            }
            return cnt;
        }


        public void RemoveItemByName(string name)
        {
            Item item;
            bool chk = true;
            for (int i = 0; i <= itemlist.Length; i++)
            {
                item = itemlist[i];
                if(item.Name == name)
                {
                    itemlist[i] = null;
                    Console.WriteLine($"{name}을(를) 인벤토리에서 제거했습니다.");
                    chk = false;
                    break;
                }
            }

            if (chk){
                Console.WriteLine("장비 제거 실패");
            }

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

namespace HelloWorld
{
    public class App
    {
        Inventory inven;

        //생성자 
        public App()
        {
            inven = new Inventory(5);

            inven.AddItem(new Item("장검"));
            inven.AddItem(new Item("단검"));
            inven.AddItem(new Item("활"));

            inven.ShowAllItems();
            //1. 장검
            //2. 단검
            //3. 활
            //4. [비어있음]
            //5. [비어있음]


            
            Item item = inven.FindItem("장검");
            Console.WriteLine(item.Name);
            
            
            int cnt = inven.Count();
            Console.WriteLine(cnt); //3

            
            inven.RemoveItemByName("장검");


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

            inven.ShowAllItems();


            //1. [비어있음]
            //2. 단검
            //3. 활
            //4. [비어있음]
            //5. [비어있음]


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

namespace HelloWorld
{
    internal class Item
    {
        public string Name;

        public Item(string name)
        {
            this.Name = name;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
    internal class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
        }
    }
}