21.12.29 아이템 & 인벤토리

2021. 12. 30. 00:00C#/수업 내용

null 그 자체는 오류를 발생시키지 않더라도 null에 접근하려는 것(ex. 점연산자) 때문에 오류가 발생한다.

 

컬렉션의 대부분은 IEnumerable 을 사용하기 때문에 foreach를 사용할 수 있다.

 

이하 수업내용-----------------------------

 

1. 아이템 객체 만들고 메인함수에서 아이템 이름 출력하기.

2. 아이템 배열에 아이템 객체 넣어서 반복문으로 출력하기.

using System;
using System.Collections.Generic;
using System.Text;

namespace helloworldConsoleApp1
{
    class Item
    {
        public string itemName;

        public Item(string name)
        {
            this.itemName = name;
        }

    }
}

--------------------------------------------------------
using System;

namespace helloworldConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Item[] inventory = new Item[5];
            Item item0 = new Item("그레이트 소드");
            Console.WriteLine("{0}", item0.itemName);
            inventory[0] = item0;
            for(int i = 0; i < inventory.Length; i++)
            {
                if (inventory[i] != null)
                {
                    Console.WriteLine("=> {0}", inventory[i].itemName);
                }
                else
                {
                    Console.WriteLine("=>");
                }

            }
            Console.WriteLine("");
            foreach(Item item in inventory)
            {
                if(item == null)
                {
                    Console.WriteLine("=>");
                    continue;
                }
                Console.WriteLine("=> {0}", item.itemName);
            }
        }
    }
}

 

3. 아이템 객체를 전달받아 관리하는 인벤토리 클래스를 만든 후 메서드를 이용해 각종 조작, 관리, 출력하기.

using System;

namespace helloworldConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Inventory inven = new Inventory(5);
            Item item0 = new Item("그레이트 소드");
            Item item1 = new Item("정밀한 단도");
            inven.AddItem(item0);
            inven.AddItem(item1);
            foreach (Item item in inven.inventory)
            {
                if (item == null)
                {
                    Console.WriteLine("=>");
                    continue;
                }
                Console.WriteLine("=> {0}", item.itemName);
            }

            Console.WriteLine(inven.GetCount());

            Item findItem = inven.FindItemByName("정밀한 도");
            if (findItem != null)
            {
                Console.WriteLine(findItem.itemName);
            }
        }
    }
}

---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace helloworldConsoleApp1
{
    class Inventory
    {
        public Item[] inventory;

        public Inventory(int index)
        {
            inventory = new Item[index];
            Console.WriteLine("인벤토리에 {0}칸이 있습니다.", index);
        }

        public void AddItem(Item item)
        {
            int index;
            for(index = 0; index < inventory.Length; index++)
            {
                if(inventory[index] == null)
                {
                    break;
                }
            }
            if(index == inventory.Length)
            {   
                Console.WriteLine("인벤토리 칸 부족!");
                return;
            }
            inventory[index] = item;
        }

        public int GetCount()
        {
            int i = 0;
            foreach(Item item in inventory)
            {
                if(item != null)
                {
                    i++;
                }
            }
            return i;
        }

        public Item FindItemByName(string name)
        {
            Item foundItem = null;
            foreach(Item item in inventory)
            {
                if (item != null)
                {
                    if (item.itemName == name)
                    {
                        foundItem = item;
                        return foundItem;
                    }
                }
            }
            Console.WriteLine("{0} 미보유", name);
            return null;
        }
    }
}

실행

 

1. 알렉스 리의  C# 자료구조.

2. 백준 알고리즘

 

IEnumerable 공부

https://smilejsu.tistory.com/1601

 

IEnumerator, IEnumerable 상속받은 Inventory 구현 (인덱서)

아이템 추가 하기 출력하기 꺼내기 foreach가능 하게 만들기 인덱서 구현 하기 null값 제거하고 순서 정렬하기 배열길이 자동증가 1 2 3 4 5 6 7 8 9 10 11 12 13 14 using System; namespace ConsoleApp9 {  ..

smilejsu.tistory.com

 

심화

닷넷 프레임워크 마이크로소프트 원서 

 

'C# > 수업 내용' 카테고리의 다른 글

22.01.03 인벤토리에서 아이템 꺼내오기, 다시 넣기  (0) 2022.01.03
22.01.01 캐리어  (2) 2022.01.01
21.12.30 부대 관리 & 드랍쉽  (0) 2021.12.30
21.12.28 벌쳐 vs 질럿  (0) 2021.12.29
21.12.27 마린 vs 저글링  (0) 2021.12.28