상세 컨텐츠

본문 제목

Study_010-2

C#/수업내용

by McRobbin 2020. 4. 16. 18:13

본문

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_010
{
    public class Inventory
    {
        Item[] itemArray;
        int nullIndex;
        public Item[] ItemsArray { get { return this.itemArray; } }
 
        public Inventory(int capacity)
        {
            this.itemArray = new Item[capacity];
            nullIndex = 0;
        }
 
        //아이템 객체를 받아 아이템 추가. 다른 곳에서 바꾸면 이곳에서도 바뀜.
        public void AddLinkItem(Item item)
        {
            if (this.isFull())
            {
                Console.WriteLine("인벤토리가 가득 찼습니다. ");
                return;
            }
 
            if(item == null)
            {
                Console.WriteLine("넣을 아이템이 없습니다.");
                return;
            }
 
            this.itemArray[nullIndex] = item;
            this.nullIndex = this.GetNullIndex();
        }
 
        //아이템 배열 모두의 아이템을 그대로 참조하게함.
        public void AddLinkItem(Item[] items)
        {
            if (items == null)
            {
                Console.WriteLine("넣을 아이템이 없습니다.");
                return;
            }
 
            foreach (Item item in items)
                AddLinkItem(item);
        }
 
        //아이템을 복제해서 인벤토리 아이템 배열에 추가.
        public void AddCloneItem(Item item)
        {
            AddLinkItem(item.Clone());
            return;
        }
 
        //아이템 이름을 받아 아이템 추가.
        public void AddCloneItem(string itemName)
        {
            if (this.isFull())
            {
                Console.WriteLine("인벤토리가 가득 찼습니다. ");
                return;
            }
 
            if (itemName == null || itemName == "")
            {
                Console.WriteLine("넣을 아이템이 없습니다.");
                return;
            }
 
            if (itemName.Contains(","))
            {
                string[] itemNameArray = itemName.Split(',');
                foreach (string name in itemNameArray)
                    this.AddLinkItem(new Item(name));
                return;
            }
 
            this.itemArray[nullIndex] = new Item(itemName);
            this.nullIndex = this.GetNullIndex();
        }
 
        //아이템 배열의 모든 아이템을 복사하여 추가.
        public void AddCloneItem(Item[] items)
        {
            if(items == null)
            {
                Console.WriteLine("넣을 아이템이 없습니다. ");
                return;
            }
 
            foreach (Item item in items)
                this.AddCloneItem(item);
        }
 
        //가장 최근에 만나는 null인덱스 반환. 아이템 추가를 앞에서부터 하기 위함.
        private int GetNullIndex()
        {
            for(int i = 0; i < this.itemArray.Length; i++)
            {
                if (this.itemArray[i] == null)
                    return i;
            }
            return itemArray.Length;
        }
 
        //인벤토리가 가득 찼는지 반환.
        public bool isFull()
        {
            return this.nullIndex == itemArray.Length;
        }
 
        //입력받은 이름과 같은 첫번째 아이템 반환.
        public Item FindFirstItem(string itemName)
        {
            if(itemName == null)
            {
                Console.WriteLine("없는 아이템 입니다.");
                return null;
            }
 
            int index = 0;
            foreach(Item target in this.itemArray)
            {
                if(target.Name == itemName)
                {
                    Console.WriteLine("{0}아이템이 배열의 {1}칸에 있었습니다."target.Name, index);
                    return target;
                }
                index++;
            }
 
            Console.WriteLine("{0}아이템이 인벤토리에 없습니다.", itemName);
            return null;
        }
 
        //입력받은 이름과 같은 첫번째 아이템 반환, 아이템 객체를 받음. 이름만 비교.
        public Item FindFirstItem(Item item)
        {
            if(item == null)
            {
                Console.WriteLine("없는 아이템 입니다.");
                return null;
            }
 
            return this.FindFirstItem(item.Name);
        }
 
        //인벤토리의 모든 아이템 출력.
        public void PrintAllItem()
        {
            for(int i = 0; i < this.itemArray.Length; i++)
            {
                if (itemArray[i] == null)
                {
                    Console.WriteLine("{0} : [     ]", i);
                }
 
                else
                {
                    Console.WriteLine("{0} : [{1}]", i, this.itemArray[i].Name);
                }
            }
            Console.WriteLine();
        }
 
        //아이템 이름 입력받아 해당 아이템을 인벤토리에서 제거.
        public Item DropItem(string itemName)
        {
            if(itemName == null)
            {
                Console.WriteLine("null 아이템 입니다.");
                return null;
            }
 
            for(int i = 0; i < this.itemArray.Length; i++)
            {
                if(this.itemArray[i] != null)
                {
                    if(this.itemArray[i].Name == itemName)
                    {
                        Console.WriteLine("{0}번째에 있는 {1} 아이템을 제거합니다.", i, this.itemArray[i].Name);
                        Item item = this.itemArray[i];
                        this.itemArray[i] = null;
                        this.nullIndex = this.GetNullIndex();
                        return item;
                    }
                }
                Item a = new Item("이름");
                
            }
 
            Console.WriteLine("{0} 아이템이 인벤토리에 없습니다.", itemName);
            return null;
        }
 
        //아이템 객체를 입력받아 해당 아이템을 인벤토리에서 제거.
        public Item DropItem(Item item)
        {
            if(item == null)
            {
                Console.WriteLine("null 아이템 입니다.");
                return null;
            }
            return this.DropItem(item.Name);
        }
 
        //아이템 인덱스를 받아 제거.
        public Item DropItem(int index)
        {
            if(this.itemArray[index] == null)
            {
                Console.WriteLine("해당 칸에는 아이템이 없습니다.");
                return null;
            }
 
            Item item = this.itemArray[index];
            this.itemArray[index] = null;
            this.nullIndex = this.GetNullIndex();
            Console.WriteLine("{0} 번째 있는 {1} 아이템을 제거합니다.", index, this.itemArray[index]);
            return item;
        }
    }
}
 
 
 

 

Inventory 클래스 내용입니다. 추가, 삭제 Find등의 기능을 가지고 있습니다.

역시나 빈칸을 알아서 찾아가도록 했습니다.

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_010
{
    public class Item
    {
        string name;
        public string Name { get { return this.name; } }
 
        int damage;
        public int Damage { get { return this.damage; } }
 
        int upgrade;
        public int Upgrade {get{return this.upgrade;} set{this.upgrade = value;}}
        public Item(string name)
        {
            this.name = name;
            this.damage = 10;
        }
 
        public Item(string name, int damage) : this(name)
        {
            this.damage = damage;
        }
 
        public Item(string name, int damage, int upgrade) : this(name, damage)
        {
            this.upgrade = upgrade;
        }
 
        public Item(Item item)
        {
            this.name = item.Name;
            this.damage = item.Damage;
            this.upgrade = item.Upgrade;
        }
 
        public Item Clone()
        {
            return new Item(this);
        }
    }
}
 
 
 

 

아이템 클래스 입니다. Clone이라는 함수를 만들었는데 이는 자신과 똑같은 내용을 갖는

객체를 하나 만드는 함수 입니다. 아이템 객체 자체를 받는 생성자를 하나 만들고 이를 호출해

리턴 했습니다.

 

Clone을 만든 이유는 Inventory에 AddItem의 경우 아이템을 복사해서 넣는 것, 복사하지 않고

참조해서 넣는 것 두가지로 구현해서 입니다.

 

App 생성자의 내용은 적지 않겠습니다.

관련글 더보기