13. 인벤토리 구현하기

C#/과제 2019. 3. 27. 22:27

 

List를 이용해 인벤토리 구현 및 아이템제작, 아이템버리기, 아이템사용 기능 구현

 

 

코드

Item.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 study0327

{

    public class Item

    {

        public int itemCode;

        public string itemName;

        public string itemExplain;

        public Item()

        {

 

        }

    }

}

Colored by Color Scripter

cs

 

UseItem.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 study0327

{

    class UseItem : Item

    {

        public UseItem(string itemName)

        {

            this.itemCode = 1;

            this.itemName = itemName;

        }

        public string ItemExplain

        {

            get

            {

                return this.itemExplain;

            }

            set

            {

                if(!(this.itemExplain == null))

                {

                    this.itemExplain = value;

                }

            }

 

        }

    }

}

 

Colored by Color Scripter

cs

EquipItem.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 study0327

{

    class EquipItem : Item

    {

        public EquipItem(string itemName)

        {

            this.itemCode = 2;

            this.itemName = itemName;

        }

        public string ItemExplain

        {

            get

            {

                return this.itemExplain;

            }

            set

            {

                if (!(this.itemExplain == null))

                {

                    this.itemExplain = value;

                }

            }

 

        }

    }

}

 

Colored by Color Scripter

cs

 

EtcItem.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 study0327

{

    class EtcItem : Item

    {

        public EtcItem(string itemName)

        {

            this.itemCode = 3;

            this.itemName = itemName;

        }

        public string ItemExplain

        {

            get

            {

                return this.itemExplain;

            }

            set

            {

                if (!(this.itemExplain == null))

                {

                    this.itemExplain = value;

                }

            }

 

        }

    }

}

 

Colored by Color Scripter

cs

AvailableTest.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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace study0327

{

    public static class AvailableTest

    {

        public static bool CanAddList(string inputItemName) //존재하는 아이템인지 체크하는 메서드

        {

            if (inputItemName == "빵" || inputItemName == "포션" || inputItemName == "마녀의 귀걸이" ||

                inputItemName == "헤르메스의 부츠" || inputItemName == "오크의 손톱")

            {

                return true;

            }

            else

            {

                return false;

            }

        }

        public static string MakeItem() //아이템을 제작가능한지 체크하고 가능하다면 이름을 리턴하는 메서드

        {

            Console.Clear();

            System.Threading.Thread.Sleep(500);

            Console.WriteLine("아이템을 제작합니다.");

            Console.WriteLine("1.빵 \t 2.포션 \t 3.마녀의 귀걸이");

            Console.WriteLine("4.헤르메스의 부츠 \t 5.오크의 손톱");

            Console.Write("6.되돌아가기 (한글로 입력하세요) : ");

            string inputItemName = Console.ReadLine();

            if (CanAddList(inputItemName) == true)

            {

                return inputItemName;

            }

            else if (inputItemName == "되돌아가기")

            {

                return "break";

            }

            else

            {

                Console.WriteLine("제작할 수 없는 아이템입니다.");

                return "x";

            }

        }

        public static bool UseToItem(string inputItemName) //아이템이 사용가능한지 체크하는 메서드

        {

            if (inputItemName == "빵" || inputItemName == "포션")

            {

                Console.Write("아이템을 사용했습니다.");

                System.Threading.Thread.Sleep(500);

                return true;

            }

            else

            {

                Console.Write("사용할 수 없는 아이템입니다.");

                System.Threading.Thread.Sleep(500);

                return false;

            }

        }

        public static bool CheckItem(List<Item> itemList, string InputItemName)

        {

            foreach (Item checkList in itemList)

            {

                if (checkList.itemName == InputItemName)

                {

                    return true;

                }

            }

            return false;

        }

    }

}

 

Colored by Color Scripter

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

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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace study0327

{

    class App

    {

        public App()

        {

            bool availability; //성공가능 여부 변수

            string inputItemName; //아이템 제작 변수

            string choiceItemName = null//인벤토리 아이템 선택 변수

            int keyToSelectMenu; //메뉴선택 키값을 변환한 정수형변수

            int count = 1//출력 카운트 변수

            int breadCount = 0//빵 카운트 변수

            int potionCount = 0//포션 카운트 변수

            int ringCount = 0//귀걸이 카운트 변수

            int bootsCount = 0//신발 카운트 변수

            int orcCount = 0//손톱 카운트 변수

            int menuCount = 1//목록 출력 변수

            ConsoleKey menuKey = ConsoleKey.D1; //메뉴선택키

            List<Item> inventory = new List<Item>(); //아이템 리스트

 

            while (menuKey != ConsoleKey.D9) //실행

            {

                menuKey = ConsoleKey.D0;

                while (menuKey == ConsoleKey.D0) //반복

                {

                    System.Threading.Thread.Sleep(500);

                    Console.Clear();

                    Console.WriteLine("※아이템은 최대 10개까지 소지할 수 있습니다※");

                    Console.Write("1.아이템제작 \t 2.인벤토리 \t 3.종료");

                    menuKey = Console.ReadKey().Key;

                    while (menuKey == ConsoleKey.D1) //제작

                    {

                        if ((inventory.Count) > 9//10칸 가득차면 제작 불가능

                        {

                            Console.WriteLine("인벤토리가 가득 찼습니다!!");

                            menuKey = ConsoleKey.D0;

                            break;

                        }

                        inputItemName = AvailableTest.MakeItem();

                        if (inputItemName == "되돌아가기"//자의적 탈출

                        {

                            menuKey = ConsoleKey.D0;

                        }

                        if (inputItemName == "빵" || inputItemName == "포션")

                        {

                            inventory.Add(new UseItem(inputItemName));

                            Console.Write("제작에 성공했습니다!");

                            menuKey = ConsoleKey.D0;

                        }

                        else if (inputItemName == "마녀의 귀걸이" || inputItemName == "헤르메스의 부츠")

                        {

                            inventory.Add(new EquipItem(inputItemName));

                            Console.Write("제작에 성공했습니다!");

                            menuKey = ConsoleKey.D0;

                        }

                        else if (inputItemName == "오크의 손톱")

                        {

                            inventory.Add(new EtcItem(inputItemName));

                            Console.Write("제작에 성공했습니다!");

                            menuKey = ConsoleKey.D0;

                        }

                        else

                        {

                            Console.WriteLine("제작할 수 없는 아이템입니다!");

                            menuKey = ConsoleKey.D0;

                        }

                    }

                    while (menuKey == ConsoleKey.D2) //인벤토리출력

                    {

                        Console.Clear();

                        if(inventory.Count<1)

                        {

                            Console.WriteLine("인벤토리가 비어있습니다.");

                            System.Threading.Thread.Sleep(500);

                            break;

                        }

                        breadCount = 0;

                        potionCount = 0;

                        ringCount = 0;

                        bootsCount = 0;

                        orcCount = 0;

                        menuCount = 1;

                        foreach (Item itemList in inventory) //아이템 갯수 확인

                        {

                            if (itemList.itemName == "빵")

                            {

                                breadCount++;

                            }

                            else if (itemList.itemName == "포션")

                            {

                                potionCount++;

                            }

                            else if (itemList.itemName == "마녀의 귀걸이")

                            {

                                ringCount++;

                            }

                            else if (itemList.itemName == "헤르메스의 부츠")

                            {

                                bootsCount++;

                            }

                            else if (itemList.itemName == "오크의 손톱")

                            {

                                orcCount++;

                            }

                        }

                        foreach (Item itemlist in inventory) //아이템 갯수 포함 출력

                        {

                            switch (itemlist.itemName)

                            {

                                case "빵":

                                    if (breadCount != -1)

                                    {

                                        Console.WriteLine("{0}. {1} x{2}", menuCount, itemlist.itemName, breadCount);

                                        breadCount = -1;

                                        menuCount++;

                                    }

                                    break;

                                case "포션":

                                    if (potionCount != -1)

                                    {

                                        Console.WriteLine("{0}. {1} x{2}", menuCount, itemlist.itemName, potionCount);

                                        potionCount = -1;

                                        menuCount++;

                                    }

                                    break;

                                case "마녀의 귀걸이":

                                    if (ringCount != -1)

                                    {

                                        Console.WriteLine("{0}. {1} x{2}", menuCount, itemlist.itemName, ringCount);

                                        ringCount = -1;

                                        menuCount++;

                                    }

                                    break;

                                case "헤르메스의 부츠":

                                    if (bootsCount != -1)

                                    {

                                        Console.WriteLine("{0}. {1} x{2}", menuCount, itemlist.itemName, bootsCount);

                                        bootsCount = -1;

                                        menuCount++;

                                    }

                                    break;

                                case "오크의 손톱":

                                    if (orcCount != -1)

                                    {

                                        Console.WriteLine("{0}. {1} x{2}", menuCount, itemlist.itemName, orcCount);

                                        orcCount = -1;

                                        menuCount++;

                                    }

                                    break;

                            }

                        }

                        Console.Write("1.아이템사용 2.아이템버리기 3.되돌아가기 : ");

                        Int32.TryParse(Console.ReadLine(), out keyToSelectMenu);

                        switch (keyToSelectMenu)

                        {

                            case 1:

                                Console.Write("사용할 아이템의 이름을 입력하세요 : ");

                                choiceItemName = Console.ReadLine();

                                availability = AvailableTest.CheckItem(inventory, choiceItemName);

                                if (availability == false)

                                {

                                    Console.WriteLine("존재하지 않는 아이템입니다.");

                                    System.Threading.Thread.Sleep(500);

                                }

                                if (availability == true)

                                {

                                    availability = AvailableTest.UseToItem(choiceItemName);

                                    if (availability == true)

                                    {

                                        count = 0;

                                        foreach (Item itemList in inventory)

                                        {

                                            if (itemList.itemName == choiceItemName)

                                            {

                                                inventory.RemoveAt(count);

                                                break;

                                            }

                                            count++;

                                        }

                                        System.Threading.Thread.Sleep(500);

                                    }

                                }

                                break;

                            case 2:

                                Console.Write("버릴 아이템의 이름을 입력하세요 : ");

                                choiceItemName = Console.ReadLine();

                                availability = AvailableTest.CheckItem(inventory, choiceItemName);

                                if (availability == true)

                                {

                                    count = 0;

                                    foreach (Item itemList in inventory)

                                    {

                                        if (itemList.itemName == choiceItemName)

                                        {

                                            inventory.RemoveAt(count);

                                            break;

                                        }

                                        count++;

                                    }

                                    Console.WriteLine("{0}을 버렸습니다.", choiceItemName);

                                    System.Threading.Thread.Sleep(500);

                                }

                                else

                                {

                                    Console.WriteLine("존재하지 않는 아이템입니다.");

                                    System.Threading.Thread.Sleep(500);

                                }

                                break;

                            case 3:

                                menuKey = ConsoleKey.D0;

                                break;

                            default:

                                Console.Write("잘못 입력하셨습니다.");

                                System.Threading.Thread.Sleep(500);

                                break;

                        }

                    }

                }

                if (menuKey == ConsoleKey.D3)

                {

                    Console.Write("\n프로그램을 종료합니다.");

                    System.Threading.Thread.Sleep(500);

                    break;

                }

            }

        }

    }

}

 

Colored by Color Scripter

cs

 

 

개선사항

           - 인벤토리 다양성

           - 아이템 설명추가

           - delegate를 통해 Sort활용

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

Inventory + Singleton  (0) 2019.04.07
Hash란?  (0) 2019.04.04
12. WoW 캐릭터 생성 과제 (2단계+α 까지 완료)  (0) 2019.03.27
11. const(상수)와 enum(열거형)  (0) 2019.03.27
10. Class의 생성흐름 읽기 (동영상)  (0) 2019.03.27
: