<n번째 인벤토리> 추가 삭제 이름변경 찾기 목록보기

C#/수업내용 2019. 4. 3. 10:58


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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_08
{
    class App
    {
        public void Start()
        {
            Console.WriteLine("시작");
 
            string searchItemName = "";
            var inventory = new Inventory();
            inventory.AddItem(new Item("피바라기"));
            inventory.AddItem(new Item("무한의 대검"));
            inventory.AddItem(new Item("피바라기"));
            inventory.DisplayItem();
            Console.WriteLine();
            
            searchItemName = "란두인의 예언";
            Item foundItem = inventory.FindItem("란두인의 예언");
            if (foundItem == null)
            {
                Console.WriteLine("{0}를 찾을 수 없습니다.", searchItemName);
            }
            else if (foundItem != null)
            {
                Console.WriteLine("{0}을 찾았습니다.", searchItemName);
            }
 
            Console.WriteLine();
            inventory.UpdateItem(ref foundItem, "피바라기");
            Console.WriteLine();
 
            searchItemName = "피바라기";
            foundItem = inventory.FindItem("피바라기");
            if (foundItem == null)
            {
                Console.WriteLine("{0}를 찾을 수 없습니다.", searchItemName);
            }
            else if (foundItem != null)
            {
                Console.WriteLine("{0}을 찾았습니다.", searchItemName);
            }
            Console.WriteLine();
            
            inventory.UpdateItem(ref foundItem, "워모그의 갑옷");
            Console.WriteLine();
 
            foundItem=inventory.FindItem("무한의 대검");
            if (foundItem == null)
            {
                Console.WriteLine("{0}를 찾을 수 없습니다.", searchItemName);
            }
            else if (foundItem != null)
            {
                Console.WriteLine("{0}을 찾았습니다.", searchItemName);
            }
            Console.WriteLine();
 
            inventory.RemoveItem("루덴의 메아리");
            Console.WriteLine();
            inventory.RemoveItem("피바라기");
            Console.WriteLine();
 
            inventory.DisplayItem();
        }
    }
}
 
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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_08
{
    public class Inventory
    {
        public int inventoryValue = 0;
        public int inventoryMaxValue = 5;
 
        Item[] items = new Item[5];
 
        public Inventory()
        {
        }
 
        public void AddItem(Item item)
        {
            for(int i = 0; i<inventoryMaxValue;i++)
            {
                if(items[i]==null)
                {
                    items[i] = item;
                    inventoryValue++;
                    break;
                }
            }
        }
 
        public void RemoveItem(string name)
        {
            Item foundItem = null;
            int i;
            for (i = 0; i < inventoryMaxValue; i++)
            {
                if (items[i] == null)
                {
 
                }
                else if (items[i].name == name)
                {
                    foundItem = items[i];
                    break;
                }
                i++;
            }
            if (foundItem == null)
            {
                Console.WriteLine("존재하지 않는 아이템은 제거할 수 없습니다.");
            }
            else
            {
                Console.WriteLine("{0}을 제거했습니다.", items[i].name);
                items[i] = null;
                inventoryValue--;
            }
        }
 
        public Item FindItem(string name)
        {
            Item foundItem = null;
            for (int i = 0; i < inventoryMaxValue; i++)
            {
                if (items[i] == null)
                {
 
                }
                else if (items[i].name == name)
                {
                    foundItem = items[i];
                    break;
                }
            }
            if (foundItem == null)
            {
                return null;
            }
            else
            {
                return foundItem;
            }
        }
 
        public void UpdateItem(ref Item item, string name)
        {
            if (item == null)
            {
                Console.WriteLine("업데이트에 실패했습니다.");
            }
            else if(item !=null)
            {
                Console.WriteLine("아이템의 이름을 \"{0}\"에서 \"{1}\"으로 수정했습니다.", item.name, name);
                item.name = name;
            }
        }
 
        public void DisplayItem()
        {
            Item temp;
            for (int i = 0; i < inventoryMaxValue - 1; i++)
            {
                if (items[i] == null)
                {
                    temp = items[i];
                    items[i] = items[i + 1];
                    items[i + 1= temp;
                }
            }
 
            Console.WriteLine("{0}개의 아이템이 있습니다.", inventoryValue);
            int j = 0;
            foreach (var item in items)
            {
                if (items[j] != null)
                {
                    Console.WriteLine("-> {0}", items[j].name);
                    j++;
                }
                else if (items[j] == null)
                {
                    j++;
                }
            }
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_08
{
    public class Item
    {
        public string name;
 
        public Item(string name)
        {
            this.name = name;
        }
    }
}
 
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_08
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
            app.Start();
 
            Console.ReadKey();
        }
    }
}
 
cs


:

<IEnumerable과 IEnumerator> 내일 다시 확인하여 예제 준비할 것

C#/과제 2019. 4. 1. 23:25

IEnumerable Interface

Docs

제네릭이 아닌 컬렉션에서 단순하게 반복할 수 있도록 지원하는 열거자를 노출합니다.


IEnumerator Interface

제네릭이 아닌 컬렉션을 단순하게 반복할 수 있도록 지원합니다.


Current 컬렉션의 열거자의 현재 위치에 있는 '요소'를 가져옵니다.

MoveNext() 열거자를 컬렉션의 다음 요소로 이동합니다.

Reset() 컬렉션의 첫번째 '요소 앞'의 초기 위치에 열거자를 설정합니다.

:

<인벤토리>Array형으로 롤백

C#/수업내용 2019. 4. 1. 19:18
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_06
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
 
            app.Run();
 
            Console.ReadKey();
        }
    }
}
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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Study_06
{
    public class App
    {
        public App()
        {
        }
        public void Run()
        {
            var inventory = new Inventory(5);
            //시작
            while (true)
            {
                Item temp = null;
                for(int i = 0;i<inventory.inventoryVolumeMax-1;i++)
                {
                    if(inventory.items[i]==null)
                    {
                        temp = inventory.items[i];
                        inventory.items[i] = inventory.items[i + 1];
                        inventory.items[i + 1= temp;
                    }
                }
                Console.WriteLine("========================장비가방========================");
                Console.WriteLine("1.아이템추가/2.아이템제거/3.목록보기/4.가방확장(+5)/0.종료({0}/{1})", inventory.inventoryVolume, inventory.inventoryVolumeMax);
                Console.Write("입력(숫자):");
                var input = Console.ReadLine();
                if (input == "0")//종료
                {
                    Console.WriteLine("인벤토리를 닫습니다.");
                    break;
                }
                else if (input == "1")//아이템 생성
                {
                    if (inventory.inventoryVolume >= inventory.inventoryVolumeMax)
                    {
                        Console.WriteLine("인벤토리가 가득 찼습니다.");
                    }
                    else
                    {
                        Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                        Console.Write("생성할 아이템 이름을 입력:");
                        input = Console.ReadLine();
                        if (input == "롱소드")
                        {
                            inventory.AddItem(new Item(new ItemData(00"롱소드"50)));
                        }
                        else if (input == "숏소드")
                        {
                            inventory.AddItem(new Item(new ItemData(10"숏소드"30)));
                        }
                        else if (input == "빨간포션")
                        {
                            inventory.AddItem(new Item(new ItemData(10"빨간포션"00)));
                        }
                        else if (input == "파란포션")
                        {
                            inventory.AddItem(new Item(new ItemData(10"파란포션"00)));
                        }
                        else
                        {
                            Console.WriteLine("잘못된 입력입니다.");
                        }
                    }
                }
                else if (input == "2")//아이템 제거
                {
                    if (inventory.inventoryVolume <= 0)
                    {
                        Console.WriteLine("버릴 아이템이 없습니다.");
                    }
                    else
                    {
                        Item founditem = null;
                        Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                        Console.Write("버릴 아이템 이름을 입력:");
                        input = Console.ReadLine();
                        int j = 0;
                        foreach (var item in inventory.items)
                        {
                            if (item.itemData.name == input)
                            {
                                founditem = item;
                                j++;
                                break;
                            }
                        }
                        if (founditem == null)
                        {
                            Console.WriteLine("해당아이템은 인벤토리에 없습니다.");
                        }
                        else
                        {
                            Console.WriteLine("{0}을(를) 버립니다.", founditem.itemData.name);
                            inventory.RemoveItem(founditem,j);
                        }
                    }
                }
                else if (input == "3")//목록보기
                {
                    if (inventory.inventoryVolume <= 0)
                    {
                        Console.WriteLine("인벤토리가 비었습니다.");
                    }
                    else
                    {
                        Console.WriteLine("인벤토리를 확인합니다.({0}/{1})", inventory.inventoryVolume, inventory.inventoryVolumeMax);
                        foreach (Item item in inventory.items)
                        {
                            if(item!=null)
                            {
                                Console.WriteLine(item.itemData.name);
                            }
                        }
                    }
                }
                else if (input == "4")
                {
                    Console.WriteLine("인벤토리 확장완료!");
                    Array.Resize(ref inventory.items, inventory.items.Length + 5);
                    inventory.inventoryVolumeMax += 5;
                }
                else
                {
                    Console.WriteLine("잘못된 입력입니다.");
                }
            }
        }
    }
}
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class Inventory
    {
        public int inventoryVolumeMax;
        public int inventoryVolume;
        public Item[] items;//아이템을 담을 List
 
        //생성자
        public Inventory(int inventoryVolumeMax)
        {
            this.items = new Item[5];
            this.inventoryVolumeMax = inventoryVolumeMax;
        }
 
        //추가
        public void AddItem(Item item)//아이템 추가
        {
            //아이템 추가
            for (int i = 0; i < this.items.Length; i++)
            {
                if (this.items[i] == null)
                {
                    this.items[i] = item;
                    inventoryVolume++;
                    break;
                }
            }
        }
        //제거
        public void RemoveItem(Item founditem, int j)
        {
            foreach(var item in this.items)
            {
                if(item==founditem)
                {
                    this.items[j] = null;
                    inventoryVolume--;
                    break;
                }
            }
        }
    }
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class Item
    {
        //아이템 타입들
        public enum eItem
        {
            None = -1,
            Weapon = 0,
            Potion = 1
        }
 
        public ItemData itemData;
 
        //생성자
        public Item(ItemData itemData)
        {
            this.itemData = itemData;
        }
    }
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class ItemData
    {
        //아이템 데이터가 가지고 있어야하는 것, 아이디, 타입, 이름, 공격력, 방어력
        public int id;
        public int type;
        public string name;
        public int damage;
        public int armor;
 
        //아이템 데이터의 생성자
        public ItemData(int id, int type, string name, int damage, int armor)
        {
            this.id = id;
            this.type = type;
            this.name = name;
            this.damage = damage;
            this.armor = armor;
        }
    }
}
cs


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

<n+1번째 인벤토리> Json과 파일읽기.  (0) 2019.04.03
<n번째 인벤토리> 추가 삭제 이름변경 찾기 목록보기  (0) 2019.04.03
<인벤토리>List<T> 제네릭형  (0) 2019.04.01
이따가 분류  (0) 2019.03.29
1차원 배열  (0) 2019.03.28
:

<인벤토리>List<T> 제네릭형

C#/수업내용 2019. 4. 1. 18:15
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_06
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
 
            app.Run();
 
            Console.ReadKey();
        }
    }
}
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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class App
    {
        public App()
        {
 
        }
        public void Run()
        {
            var EquipmentInventory = new Inventory<EquipmentItem>(5);
            var PotionItem = new Inventory<PotionItem>(5);
 
            //시작
            while(true)
            {
                Console.WriteLine("1.장비가방/2.포션가방/0.종료");
                var input = Console.ReadLine();
                if (input == "0")//종료
                {
                    Console.WriteLine("인벤토리를 닫습니다.");
                    break;
                }
                else if (input == "1")
                {
                    while (true)
                    {
                        Console.WriteLine("========================장비가방========================");
                        Console.WriteLine("1.아이템추가/2.아이템제거/3.목록보기/4.가방확장(+5)/0.종료({0}/{1})", EquipmentInventory.items.Count, EquipmentInventory.inventoryVolumeMax);
                        Console.Write("입력(숫자):");
                        input = Console.ReadLine();
 
                        if (input == "0")//종료
                        {
                            Console.WriteLine("인벤토리를 닫습니다.");
                            break;
                        }
                        else if (input == "1")//아이템 생성
                        {
                            if (EquipmentInventory.items.Count >= EquipmentInventory.inventoryVolumeMax)
                            {
                                Console.WriteLine("인벤토리가 가득 찼습니다.");
                            }
                            else
                            {
                                Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                                Console.Write("생성할 아이템 이름을 입력:");
                                input = Console.ReadLine();
 
                                if (input == "롱소드")
                                {
                                    EquipmentInventory.AddItem(new EquipmentItem(new ItemData(00"롱소드"50)));
                                }
                                else if (input == "숏소드")
                                {
                                    EquipmentInventory.AddItem(new EquipmentItem(new ItemData(10"숏소드"30)));
                                }
                                else if (input == "빨간포션")
                                {
                                    Console.WriteLine("해당가방에 넣을 수 없습니다.");
                                }
                                else if (input == "파란포션")
                                {
                                    Console.WriteLine("해당가방에 넣을 수 없습니다.");
                                }
                                else
                                {
                                    Console.WriteLine("잘못된 입력입니다.");
                                }
                            }
                        }
                        else if (input == "2")//아이템 제거
                        {
                            if (EquipmentInventory.items.Count <= 0)
                            {
                                Console.WriteLine("버릴 아이템이 없습니다.");
                            }
                            else
                            {
                                EquipmentItem founditem = null;
                                Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                                Console.Write("버릴 아이템 이름을 입력:");
                                input = Console.ReadLine();
 
                                foreach (var item in EquipmentInventory.items)
                                {
                                    if (item.itemData.name == input)
                                    {
                                        founditem = item;
                                        break;
                                    }
                                }
                                if (founditem == null)
                                {
                                    Console.WriteLine("해당아이템은 인벤토리에 없습니다.");
                                }
                                else
                                {
                                    Console.WriteLine("{0}을(를) 버립니다.", founditem.itemData.name);
                                    EquipmentInventory.RemoveItem(founditem);
                                }
                            }
                        }
                        else if (input == "3")//목록보기
                        {
                            if (EquipmentInventory.items.Count <= 0)
                            {
                                Console.WriteLine("인벤토리가 비었습니다.");
                            }
                            else
                            {
                                Console.WriteLine("인벤토리를 확인합니다.({0}/{1})", EquipmentInventory.items.Count, EquipmentInventory.inventoryVolumeMax);
                                foreach (Item item in EquipmentInventory.items)
                                {
                                    Console.WriteLine(item.itemData.name);
                                }
                            }
                        }
                        else if (input == "4")
                        {
                            Console.WriteLine("인벤토리 확장완료!");
                            EquipmentInventory.inventoryVolumeMax += 5;
                        }
                        else
                        {
                            Console.WriteLine("잘못된 입력입니다.");
                        }
                    }
                }
                else if (input == "2")
                {
                    while (true)
                    {
                        Console.WriteLine("========================포션가방========================");
                        Console.WriteLine("1.아이템추가/2.아이템제거/3.목록보기/4.가방확장(+5)/0.종료({0}/{1})", PotionItem.items.Count, PotionItem.inventoryVolumeMax);
                        Console.Write("입력(숫자):");
                        input = Console.ReadLine();
 
                        if (input == "0")//종료
                        {
                            Console.WriteLine("인벤토리를 닫습니다.");
                            break;
                        }
                        else if (input == "1")//아이템 생성
                        {
                            if (PotionItem.items.Count >= PotionItem.inventoryVolumeMax)
                            {
                                Console.WriteLine("인벤토리가 가득 찼습니다.");
                            }
                            else
                            {
                                Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                                Console.Write("생성할 아이템 이름을 입력:");
                                input = Console.ReadLine();
 
                                if (input == "롱소드")
                                {
                                    Console.WriteLine("해당가방에 넣을 수 없습니다.");
                                }
                                else if (input == "숏소드")
                                {
                                    Console.WriteLine("해당가방에 넣을 수 없습니다.");
                                }
                                else if (input == "빨간포션")
                                {
                                    PotionItem.AddItem(new PotionItem(new ItemData(21"빨간포션"00)));
                                }
                                else if (input == "파란포션")
                                {
                                    PotionItem.AddItem(new PotionItem(new ItemData(31"파란포션"00)));
                                }
                                else
                                {
                                    Console.WriteLine("잘못된 입력입니다.");
                                }
                            }
                        }
                        else if (input == "2")//아이템 제거
                        {
                            if (PotionItem.items.Count <= 0)
                            {
                                Console.WriteLine("버릴 아이템이 없습니다.");
                            }
                            else
                            {
                                PotionItem founditem = null;
                                Console.WriteLine("아이템 목록(롱소드, 숏소드, 빨간포션, 파란포션)");
                                Console.Write("버릴 아이템 이름을 입력:");
                                input = Console.ReadLine();
 
                                foreach (var item in PotionItem.items)
                                {
                                    if (item.itemData.name == input)
                                    {
                                        founditem = item;
                                        break;
                                    }
                                }
                                if (founditem == null)
                                {
                                    Console.WriteLine("해당아이템은 인벤토리에 없습니다.");
                                }
                                else
                                {
                                    Console.WriteLine("{0}을(를) 버립니다.", founditem.itemData.name);
                                    PotionItem.RemoveItem(founditem);
                                }
                            }
                        }
                        else if (input == "3")//목록보기
                        {
                            if (PotionItem.items.Count <= 0)
                            {
                                Console.WriteLine("인벤토리가 비었습니다.");
                            }
                            else
                            {
                                Console.WriteLine("인벤토리를 확인합니다.({0}/{1})", PotionItem.items.Count, PotionItem.inventoryVolumeMax);
                                foreach (Item item in PotionItem.items)
                                {
                                    Console.WriteLine(item.itemData.name);
                                }
                            }
                        }
                        else if (input == "4")
                        {
                            Console.WriteLine("인벤토리 확장완료!");
                            PotionItem.inventoryVolumeMax += 5;
                        }
                        else
                        {
                            Console.WriteLine("잘못된 입력입니다.");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("잘못된 입력 입니다.");
                }
            }
        }
    }
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class Inventory<T>
    {
        public int inventoryVolumeMax;
        public List<T> items;//아이템을 담을 List
 
        //생성자
        public Inventory(int inventoryVolumeMax)
        {
            this.items = new List<T>();
            this.inventoryVolumeMax = inventoryVolumeMax;
        }
 
        //추가
        public void AddItem(T item)//아이템 추가
        {
            //아이템 추가
            this.items.Add(item);
        }
        //제거
        public void RemoveItem(T founditem)
        {
            this.items.Remove(founditem);
        }
    }
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class Item
    {
        //아이템 타입들
        public enum eItem
        {
            None = -1,
            Weapon = 0,
            Potion = 1
        }
 
        public ItemData itemData;
 
        //생성자
        public Item(ItemData itemData)
        {
            this.itemData = itemData;
        }
    }
}
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    class EquipmentItem:Item
    {
        public EquipmentItem(ItemData itemData) : base(itemData)
        {
            if (this.itemData.type != 0)
            {
                Console.WriteLine("해당 아이템은 장비가방에 넣을 수 없습니다.");
            }
            else
            {
                this.itemData = itemData;
                Console.WriteLine("{0}이(가) 아이템 생성되었습니다.", itemData.name);
            }
        }
    }
}
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    class PotionItem:Item
    {
        public PotionItem(ItemData itemData):base(itemData)
        {
            if (this.itemData.type != 1)
            {
                Console.WriteLine("해당 아이템은 포션가방에 넣을 수 없습니다.");
            }
            else
            {
                this.itemData = itemData;
                Console.WriteLine("{0}이(가) 아이템 생성되었습니다.", itemData.name);
            }
        }
    }
}
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Study_06
{
    public class ItemData
    {
        //아이템 데이터가 가지고 있어야하는 것, 아이디, 타입, 이름, 공격력, 방어력
        public int id;
        public int type;
        public string name;
        public int damage;
        public int armor;
 
        //아이템 데이터의 생성자
        public ItemData(int id, int type, string name, int damage, int armor)
        {
            this.id = id;
            this.type = type;
            this.name = name;
            this.damage = damage;
            this.armor = armor;
        }
    }
}
cs


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

<n번째 인벤토리> 추가 삭제 이름변경 찾기 목록보기  (0) 2019.04.03
<인벤토리>Array형으로 롤백  (0) 2019.04.01
이따가 분류  (0) 2019.03.29
1차원 배열  (0) 2019.03.28
<foreach in, for, while>  (0) 2019.03.27
:

<인벤토리 1차>중첩방식 목록보기

C#/과제 2019. 3. 31. 18:35



Program.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    class Program
    {
        static void Main(string[] args)
        {
            App app = new App();
 
            app.Run();
 
            Console.ReadKey();
        }
    }
}
 
cs

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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    public class App
    {
        public App()
        {
 
        }
        public void Run()
        {
            var inventory = new Inventory(10);//인벤토리 생성, 및 최대량 지정
            Console.WriteLine("인벤토리 오픈");
 
            //메인메뉴
            while (true)
            {
                Console.WriteLine("===================================================================");
                Console.WriteLine("1.아이템추가/2.아이템제거/3.목록보기/0.종료({0}/{1})", inventory.items.Count, inventory.inventoryVolumeMax);
                Console.Write("입력(숫자):");
                var input = Console.ReadLine();
 
                if (input == "0")//종료
                {
                    Console.WriteLine("인벤토리를 닫습니다.");
                    break;
                }
                else if (input == "1")//아이템 생성
                {
                    if (inventory.items.Count >= inventory.inventoryVolumeMax)
                    {
                        Console.WriteLine("인벤토리가 가득 찼습니다.");
                    }
                    else
                    {
                        Console.WriteLine("아이템 목록(롱소드, 숏소드, 천옷, 판금갑옷, 반지, 목걸이)");
                        Console.Write("생성할 아이템 이름을 입력:");
                        input = Console.ReadLine();
 
                        if (input == "롱소드")
                        {
                            inventory.AddItem(new Item(new ItemData(00"롱소드"50)));
                        }
                        else if (input == "숏소드")
                        {
                            inventory.AddItem(new Item(new ItemData(10"숏소드"30)));
                        }
                        else if (input == "천옷")
                        {
                            inventory.AddItem(new Item(new ItemData(21"천옷"05)));
                        }
                        else if (input == "판금갑옷")
                        {
                            inventory.AddItem(new Item(new ItemData(31"판금갑옷"010)));
                        }
                        else if (input == "반지")
                        {
                            inventory.AddItem(new Item(new ItemData(42"반지"01)));
                        }
                        else if (input == "목걸이")
                        {
                            inventory.AddItem(new Item(new ItemData(52"목걸이"00)));
                        }
                        else
                        {
                            Console.WriteLine("잘못된 입력입니다.");
                        }
                    }
                }
                else if (input == "2")//아이템 제거
                {
                    if (inventory.items.Count <= 0)
                    {
                        Console.WriteLine("버릴 아이템이 없습니다.");
                    }
                    else
                    {
                        Item founditem = null;
                        Console.WriteLine("아이템 목록(롱소드, 숏소드, 천옷, 판금갑옷, 반지, 목걸이)");
                        Console.Write("버릴 아이템 이름을 입력:");
                        input = Console.ReadLine();
 
                        foreach (var item in inventory.items)
                        {
                            if (item.itemData.name == input)
                            {
                                founditem = item;
                                break;
                            }
                        }
                        if (founditem == null)
                        {
                            Console.WriteLine("해당아이템은 인벤토리에 없습니다.");
                        }
                        else
                        {
                            Console.WriteLine("{0}을(를) 버립니다.", founditem.itemData.name);
                            inventory.RemoveItem(founditem);
                        }
                    }
                }
                else if (input == "3")//목록보기
                {
                    int countLongSword = 0, countShortSword = 0, countCloth = 0, countPlateArmor = 0, countRing = 0, countNecklace = 0;//아이템 카운터
                    if (inventory.items.Count <= 0)
                    {
                        Console.WriteLine("인벤토리가 비었습니다.");
                    }
                    else
                    {
                        Console.WriteLine("인벤토리를 확인합니다.({0}/{1})", inventory.items.Count, inventory.inventoryVolumeMax);
                        foreach (Item item in inventory.items)
                        {
                            if (item.itemData.id == 0)
                            {
                                countLongSword++;
                            }
                            else if (item.itemData.id == 1)
                            {
                                countShortSword++;
                            }
                            else if (item.itemData.id == 2)
                            {
                                countCloth++;
                            }
                            else if (item.itemData.id == 3)
                            {
                                countPlateArmor++;
                            }
                            else if (item.itemData.id == 4)
                            {
                                countRing++;
                            }
                            else if (item.itemData.id == 5)
                            {
                                countNecklace++;
                            }
                        }
                        if (countLongSword != 0)
                        {
                            Console.WriteLine("롱소드x{0}", countLongSword);
                        }
                        if (countShortSword != 0)
                        {
                            Console.WriteLine("숏소드x{0}", countShortSword);
                        }
                        if (countCloth != 0)
                        {
                            Console.WriteLine("천옷x{0}", countCloth);
                        }
                        if (countPlateArmor != 0)
                        {
                            Console.WriteLine("판금갑옷x{0}", countPlateArmor);
                        }
                        if (countRing != 0)
                        {
                            Console.WriteLine("반지x{0}", countRing);
                        }
                        if (countNecklace != 0)
                        {
                            Console.WriteLine("목걸이x{0}", countNecklace);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("잘못된 입력입니다.");
                }
            }
        }
    }
}
 
cs


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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    public class Inventory
    {
        public int inventoryVolumeMax;
        public List<Item> items;//아이템을 담을 List
 
        //생성자
        public Inventory(int inventoryVolumeMax)
        {
            this.items = new List<Item>();
            this.inventoryVolumeMax = inventoryVolumeMax;
 
            Console.WriteLine("인벤토리 생성(0/{0})", inventoryVolumeMax);
        }
 
        //추가
        public void AddItem(Item item)//아이템 추가
        {
            //아이템 추가
            this.items.Add(item);
        }
        //제거
        public void RemoveItem(Item founditem)
        {
            this.items.Remove(founditem);
        }
        //목록
    }
}
 
cs

Item.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    public class Item
    {
        //아이템 타입들
        public enum eItem
        {
            None = -1,
            Weapon = 0,
            Armor = 1,
            Accessory = 2
        }
 
        public ItemData itemData;
 
        //생성자
        public Item(ItemData itemData)
        {
            this.itemData = itemData;
            Console.WriteLine("{0}이(가) 아이템 생성되었습니다.", itemData.name);
        }
    }
}
 
cs

ItemData.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace InventoryTest
{
    public class ItemData
    {   
        //아이템 데이터가 가지고 있어야하는 것, 아이디, 타입, 이름, 공격력, 방어력
        public int id;
        public int type;
        public string name;
        public int damage;
        public int armor;
 
        //아이템 데이터의 생성자
        public ItemData(int id, int type, string name, int damage, int armor)
        {
            this.id = id;
            this.type = type;
            this.name = name;
            this.damage = damage;
            this.armor = armor;
        }
    }
}
 
cs


'C# > 과제' 카테고리의 다른 글

<인벤토리> 저장, 불러오기 기능  (0) 2019.04.08
<IEnumerable과 IEnumerator> 내일 다시 확인하여 예제 준비할 것  (0) 2019.04.01
<인벤토리> 0.5차  (0) 2019.03.27
<객체와 개체의 차이>  (0) 2019.03.27
<코드읽기>  (0) 2019.03.26
:

이따가 분류

C#/수업내용 2019. 3. 29. 15:51


C#에서 중요한 키워드:객체지향, IEnumerable, IEnumerator, delegate, event Colletion, generic

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

<인벤토리>Array형으로 롤백  (0) 2019.04.01
<인벤토리>List<T> 제네릭형  (0) 2019.04.01
1차원 배열  (0) 2019.03.28
<foreach in, for, while>  (0) 2019.03.27
methods  (0) 2019.03.22
:

<선택정렬 알고리즘>수정1

Algorithmus 2019. 3. 29. 09:38

선택정렬 알고리즘 - 오름차순/내림차순, 스왑 메서드화

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190329
{
    class SelectSort
    {
        public List<int> list = new List<int> { 12605 };
 
        public void AscendingSelectSort()//오름차순 정렬
        {
            int i = 0;
            int j = 0;
            int min = 0//최소값의 index를 확인할 아이
 
            for (i = 0; i < list.Count - 1; i++)//전체 훑기 Count-1 : 마지막칸은 자동으로 정렬되고 뒷칸이 없으므로 제외
            {
                min = i;//최소값을 들고 있을 제 3의 손//맨 처음엔 검사하는 첫배열을 들고있음
 
                Console.WriteLine("\n{0}회정렬 전", i);
                for (int a = 0; a < list.Count; a++)
                {
                    Console.Write("{0} ", list[a]);
                }
                for (j = i + 1; j < list.Count; j++)// 최소값을 탐색한다.
                {
                    if (list[j] < list[min])//초기값 or 최소값이 j보다 크면 j를 최소값에 넣는다.
                    {
                        min = j;
                    }
                }
                if (min != 0)//스왑 알고리즘
                {
                    Swap(i, min);
                }
 
                Console.WriteLine("\n{0}회정렬 후", i);
                for (int a = 0; a < list.Count; a++)
                {
                    Console.Write("{0} ", list[a]);
                }
            }
        }
 
        public void DescendingSelectSort()//내림 차순 정렬
        {
            int i = 0;
            int j = 0;
            int max = 0//최소값의 index를 확인할 아이
 
            for (i = 0; i < list.Count - 1; i++)//전체 훑기 Count-1 : 마지막칸은 자동으로 정렬되고 뒷칸이 없으므로 제외
            {
                max = i;//최소값을 들고 있을 제 3의 손//맨 처음엔 검사하는 첫배열을 들고있음
 
                Console.WriteLine("\n{0}회정렬 전", i);
                for (int a = 0; a < list.Count; a++)
                {
                    Console.Write("{0} ", list[a]);
                }
                for (j = i + 1; j < list.Count; j++)// 최소값을 탐색한다.
                {
                    if (list[j] > list[max])//초기값 or 최소값이 j보다 크면 j를 최소값에 넣는다.
                    {
                        max = j;
                    }
                }
                if (max != 0)//스왑 알고리즘
                {
                    Swap(i, max);
                }
 
                Console.WriteLine("\n{0}회정렬 후", i);
                for (int a = 0; a < list.Count; a++)
                {
                    Console.Write("{0} ", list[a]);
                }
            }
        }
 
        private void Swap(int index1, int index2)
        {
            int temp; //값을 임시로 저장할 temp변수
 
            temp = list[index2];
            list[index2] = list[index1];
            list[index1] = temp;
        }
    }
}
 
cs


'Algorithmus' 카테고리의 다른 글

[Palindrome Number]05-14  (0) 2019.05.14
[TwoSum]05-04  (0) 2019.05.04
[TwoSum]05-03  (0) 2019.05.03
<선택정렬>  (0) 2019.03.29
코딩테스트  (0) 2019.03.27
:

<선택정렬>

Algorithmus 2019. 3. 29. 00:42
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _20190328
{
    class App
    {
        public List<int> list = new List<int> { 12605 };
 
        public App()
        {
 
        }
 
        public void Run()
        {
            Console.WriteLine("실행");
            this.SelectSort();
        }
 
        public void SelectSort()
        {
            int i = 0;
            int j = 0;
            int min = 0//최소값의 index를 확인할 아이
            int temp; //값을 임시로 저장할 temp변수
            
            for (i = 0; i < list.Count - 1; i++)//전체 훑기 Count-1 : 마지막칸은 자동으로 정렬되고 뒷칸이 없으므로 제외
            {
                min = i;//최소값을 들고 있을 제 3의 손//맨 처음엔 검사하는 첫배열을 들고있음
                
                Console.WriteLine("\n{0}회정렬 전",i);
                for (int a = 0; a < list.Count; a++)
                {
                    Console.Write("{0} ", list[a]);
                }
                for (j = i + 1; j < list.Count; j++)// 최소값을 탐색한다.
                {
                    if (list[j] < list[min])//초기값 or 최소값이 j보다 크면 j를 최소값에 넣는다.
                    {
                        min = j;
                    }
                }
                if (min != 0)//스왑 알고리즘
                {
                    temp = list[min];
                    list[min] = list[i];
                    list[i] = temp;
                }
                
                Console.WriteLine("\n{0}회정렬 후", i);
                for (int a = 0; a < list.Count; a++)
                {
                    Console.Write("{0} ", list[a]);
                }
            }
        }
    }
}
cs


'Algorithmus' 카테고리의 다른 글

[Palindrome Number]05-14  (0) 2019.05.14
[TwoSum]05-04  (0) 2019.05.04
[TwoSum]05-03  (0) 2019.05.03
<선택정렬 알고리즘>수정1  (0) 2019.03.29
코딩테스트  (0) 2019.03.27
: