Study_009 -2 아이템 인벤토리 배열
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_009_2
{
public class Inventory
{
public Item[] Items;
public int Count;
public Inventory(int capacity)
{
this.Items = new Item[capacity];
this.Count = 0;
}
public void PutItem(string itemName)
{
this.PutItem(new Item(itemName));
}
public void PutItem(Item item)
{
if(item == null)
{
Console.WriteLine("null은 넣을수 없습니다.");
return;
}
{
Console.WriteLine("인벤토리가 가득 찼습니다.");
return;
}
{
if (Items[i] == null)
{
this.Items[i] = item;
this.Count++;
break;
}
}
}
public Item GetItem(string itemName)
{
int targetIndex;
Item targetItem;
{
if (Items[targetIndex] == null)
continue;
if(itemName == Items[targetIndex].Name)
{
targetItem = Items[targetIndex];
Count--;
Console.WriteLine("{0} 번째 칸의 {1} 아이템을 뽑았습니다.", targetIndex, targetItem.Name);
Items[targetIndex] = null;
return targetItem;
}
}
Console.WriteLine("{0} 아이템은 인벤토리에 없습니다. ", itemName);
return null;
}
public Item FindItem(string itemName)
{
int targetIndex;
{
if (Items[targetIndex] == null)
continue;
if (Items[targetIndex].Name == itemName)
{
Console.WriteLine("{0}번째 칸에 {1} 아이템을 찾았습니다.", targetIndex, Items[targetIndex].Name);
return Items[targetIndex];
}
}
Console.WriteLine("{0} 아이템을 찾을 수 없습니다.", itemName);
return null;
}
public int GetItemCount()
{
Console.WriteLine("인벤토리에 아이템 갯수 : {0}", this.Count);
return this.Count;
}
public void PrintAllItems()
{
{
if (Items[i] == null)
Console.WriteLine("{0} : [ ]", i);
else
{
Console.WriteLine("{0} : [ {1} ]", i, Items[i].Name);
}
}
}
}
}
|
Inventory클래스 코드 입니다. 저는 아이템 넣을 인덱스 변수를 따로 두는게 아니라 아이템을 넣을 때마다 for문을 돌아
빈칸을 찾아가도록 했습니다. index 멤버변수는 가지고 있지 않으며 갯수만 세는 Count 멤버변수만 있습니다.
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_009_2
{
public class Item
{
public string Name;
public Item(string name)
{
this.Name = name;
}
}
}
|
Item 클래스 내용 입니다.
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study_009_2
{
public class App
{
public App()
{
Inventory inventory = new Inventory(3);
inventory.PrintAllItems();
inventory.PutItem(new Item("장검"));
inventory.PrintAllItems();
inventory.GetItem("도끼");
inventory.PutItem(new Item("도끼"));
inventory.PrintAllItems();
Item getItem = inventory.GetItem("장검");
inventory.PrintAllItems();
inventory.PutItem("철퇴");
inventory.FindItem("철퇴");
inventory.PrintAllItems();
inventory.GetItem("도끼");
inventory.PrintAllItems();
inventory.PutItem(getItem);
inventory.PrintAllItems();
inventory.PutItem("활");
inventory.PrintAllItems();
inventory.PutItem("주먹");
inventory.PrintAllItems();
inventory.GetItem("철퇴");
inventory.PrintAllItems();
}
}
}
|
App 생성자 내용입니다. 내용은 바꿔가며 해보시면 되겠습니다.
//위로 아이템 빈칸 땡기는 함수.
public void LiftUp()
{
int nullIndex = 0;
for(int i = 0; i < Items.Length; i++)
{
if(Items[i] == null)
{
nullIndex = i;
for(int j = nullIndex; j < Items.Length; j++)
{
if(Items[j] != null)
{
Items[i] = this.GetItem(Items[j].Name);
break;
}
}
}
}
Console.WriteLine("위로 땡기기 완료.");
}
위로 빈칸 없이 땡겨주는 함수 입니다. inventory에 메서드로 넣어 사용하시면 되겠습니다.