인벤토리에 아이템 넣기

2020. 4. 14. 17:49C#/수업내용

1) 배열 내의 요소가 null일 가능성을 항상 염두에 두고 조건을 걸어놓자.

- 예외 발생 방지

2) foreach

- 배열 전용 반복문인 듯 하다.

요소를 차례로 최대치까지 맞춰서 꺼내주기 때문에 정적 배열에서의 영역 오접근을 막기 편하다. 

 

 

<Inventory.cs>

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_009
{
    class Inventory
    {
        public Item[] items;        // 인벤토리가 관리하는 아이템들
        int capacity;
        int itemIdx;
        public Inventory(int capacity)
        {
            this.capacity = capacity;
            items = new Item[capacity];
            itemIdx = 0;
            Console.WriteLine("인벤토리가 생성되었습니다.");
        }
        public void AddItem(Item item)
        {
            if (itemIdx < this.capacity)
            {                
                this.items[this.itemIdx++= item;
                Console.WriteLine($"인벤토리에 {item.name}을 넣었습니다. ");
            }
            else
                Console.WriteLine("인벤토리가 가득 찼습니다. ");
        }
        public void PrintItemNames()
        {
            //for(int i = 0; i<this.items.Length; i++)
            //{
            //    Console.WriteLine($"{this.items[i].name}");
            //}
 
            foreach (Item item in this.items)
            {
                if (item == null)
                {
                    Console.WriteLine("Null");
                }
                else
                {
                    Console.WriteLine($"{item.name}");
                }
            }
        }
        public Item FindItem(string itemName)
        {
            foreach (Item item in this.items)
            {
                if (item != null)
                {
                    if (item.name == itemName)
                    {
                        Console.WriteLine($"{itemName}을 찾았습니다. ");
                        return item;            // 아이템을 찾은 경우
                    }
                }
            }
            Console.WriteLine($"{itemName}을 찾지 못했습니다. ");
            return null;                        // 아이템을 찾지 못한 경우
        }
    }
}
 

<Item.cs>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_009
{
    class Item
    {
        public string name;
 
        public Item(string name)
        {
            this.name = name;
            Console.WriteLine($"{this.name}이(가) 생성되었습니다.");
        }
    }
}
 

 

<App.cs>

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_009
{
    class App
    {
        public App()
        {
            Inventory inventory = new Inventory(5);
                        
            inventory.AddItem(new Item("장검"));
            inventory.AddItem(new Item("단검"));
            inventory.AddItem(new Item("도끼"));
            inventory.AddItem(new Item("활"));
            inventory.AddItem(new Item("창"));
            inventory.AddItem(new Item("장검"));
 
            inventory.PrintItemNames();
            inventory.FindItem("활");
            inventory.FindItem("활활");
 
 
 
        }
    }
}
 

 

<실행 결과>

 

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

배열의 복사(Deep/Shallow)  (0) 2020.04.16
스타크래프트 - 배럭  (0) 2020.04.14
아이템 선택 및 강화  (0) 2020.04.09
#Enum - 캐릭터 생성 시 직업 및 종족 선택  (0) 2020.04.08
몬스터와의 교전  (0) 2020.04.07