'C#'에 해당되는 글 39건

  1. 2019.04.01 Array를 이용하여 Inventory 만들기
  2. 2019.04.01 하는중
  3. 2019.03.29 공부할것
  4. 2019.03.29 1. 선택정렬(Selection Sort) 정리 및 구현
  5. 2019.03.28 공부할 것과 정리중인 것
  6. 2019.03.27 13. 인벤토리 구현하기
  7. 2019.03.27 공부중
  8. 2019.03.27 Baek Joon Online Judge

Array를 이용하여 Inventory 만들기

C#/수업내용 2019. 4. 1. 14:46

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 ArrayInventory
{
    class App
    {
        public App()
        {
            this.Start();
        }
        public void Start()
        {
            Inventory inventory = new Inventory();
            var inputKey = ConsoleKey.D0;
            while (inputKey != ConsoleKey.D4)
            {
                Console.WriteLine("\n1.아이템제작 2.인벤토리 3.아이템검색 4.종료하기");
                inputKey = Console.ReadKey().Key;
                string inputItemName;
                switch(inputKey)
                {
                    case ConsoleKey.D1:
                        Console.Write("제작하실 아이템 이름을 입력하세요 : ");
                        inputItemName = Console.ReadLine();
                        inventory.AddItem(new Item(inputItemName));
                        break;
                    case ConsoleKey.D2:
                        inventory.GetItemList();
                        break;
                    case ConsoleKey.D3:
                        Console.Write("검색하실 아이템 이름을 입력하세요 : ");
                        inputItemName = Console.ReadLine();
                        inventory.GetItemByName(inputItemName);
                        break;
                    case ConsoleKey.D4:
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayInventory
{
    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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ArrayInventory
{
    class Inventory
    {
        //Item형식의 값을 저장할 공간 
        private Item[] items = new Item[10];
 
        //기본 생성자 
        public Inventory()
        {
            Console.WriteLine("Inventory클래스의 생성자");
            Console.WriteLine("items.Length: {0}", items.Length);
        }
 
        //넣고 (매개변수로 Item인스턴스를 받아 배열에 넣는다)
        public void AddItem(Item item)
        {
            //배열의 방에 현재 위치에 있는지 확인후 없으면 다음방에 넣는다.
            //Item, Item, null, null,null, null,null, null,null, null
            //Item, Item, Item, Item,Item, Item,Item, Item,Item, Item
            //더이상 넣을수 없습니다.
            for(int index = 0; index < items.Length; index++)
            {
                if(this.GetItemCount() == 10)
                {
                    Console.WriteLine("인벤토리가 꽉찼습니다.");
                    break;
                }
                if(items[index] == null)
                {
                    items[index] = item;
                    Console.WriteLine("{0}의 제작이 완료되었습니다.", item.name);
                    break;
                }
            }
            
            
        }
 
        //뺀다 
        public void GetItemByName(string searchName)
        {
            //배열안에 있는 요소들중에 serachName 문자열과 비교해서 같은거 찾음
            //찾은거 반환, 못찾으면? null반환 
            //같은게 있다면? 하나만 가져오자 
            //돌도끼, 돌도끼, null, null,null, null,null, null,null, null
            //null, 돌도끼, null, null,null, null,null, null,null, null
            //반환값 : 돌도끼
            //null, 돌도끼, null, null,null, null,null, null,null, null
            //null, 돌도끼, null, 장검,null, null,null, null,null, null
            //돌도끼, 장검, null, null, null, ,null, null,null, null,null, null
            //손도끼, 돌도끼, null, null,null, null,null, null,null, null
            int index = 0;
            Item temp;
            foreach(Item checkInventory in items)
            {
                if(checkInventory.name == searchName)
                {
                    Console.WriteLine("{0}가 인벤토리에 있습니다. 버리시겠습니까?(1.버린다 2.넣어둔다)", searchName);
                    var readKey = Console.ReadKey().Key;
                    if(readKey == ConsoleKey.D1)
                    {
                        items[index] = null;
                        if(items.Length != 0)
                        {
                            for (int moveIndex = 0; moveIndex < items.Length - 1; moveIndex++)
                            {
                                if (items[moveIndex] == null)
                                {
                                    temp = items[moveIndex + 1];
                                    items[moveIndex + 1= items[moveIndex];
                                    items[moveIndex] = temp;
                                }
                            }
                            Console.WriteLine("{0}이 삭제되고 인벤토리가 정렬되었습니다.", searchName);
                        }
                        Console.WriteLine("{0}이 삭제되었습니다.", searchName);
                        break;
                    }
                }
                index++;
            }
        }
        
        //목록출력
        public void GetItemList()
        {
            foreach(Item itemList in items)
            {
                if(itemList == null)
                {
                    break;
                }
                Console.Write("{0}  ", itemList.name);
            }
        }
 
        //인벤토리에 있는 아이템 갯수 가져오기 
        public int GetItemCount()
        {
            //배열안에 있는 값이 null이 아닐경우 카운트 해서 반환함 
            int cnt = 0;
            foreach (var item in items)
            {
                if (item != null)
                {
                    cnt++;
                }
            }
            return cnt;
        }
    }
}
cs


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

제너릭 클래스, 상속을 이용하여 Inventory 만들기  (0) 2019.04.01
List를 이용하여 Inventory 만들기  (0) 2019.04.01
공부할것  (0) 2019.03.29
2. for, foreach, while  (0) 2019.03.27
1. Method  (0) 2019.03.24
:

하는중

C#/Problems 2019. 4. 1. 03:10

PlayerInventory.zip


:

공부할것

C#/수업내용 2019. 3. 29. 18:19

object

일단 느낌은 var타입 같은 느낌, method의 매개변수를 해놓으면

어떤 타입이건 전달받기 가능

모든 type은 object class를 상속받았기 때문에 가능하다.






0. Table File load

1. 월드생성

2. 오브젝트들 인스턴스화

3. Table참조하여 id에 맞는 오브젝트 인스턴스화하고 이름같은 속성 저장



아이템 타입 = enum에 열거하고 파싱


table(excel)->file(txt)->instance


txt에서 itemId를 기준으로 참조하여 나머지 값들을 인스턴스화한다.



data binding class는 table과 value가 같아야한다. (무결성)



C# : Generic, IEnumerable, IEnumerator, Collection, delegate, event...



ArrayList //안쓰는걸 권유 참조형이 boxing/unboxing되는 과정에서 성능적문제

Hashtable

Queue

Stack


Boxing -> 값형식을 object형식으로 변환하여 래핑 힙에 저장한다.

암시적으로

UnBoxing -> 개체에서 값형식이 추출된다.

명시적으로


일반화 : class/method design 형식 매개변수 개념

런타임 캐스팅, boxing 작업에 대한 비용이나 위험 발생 x

Collection Class 만드는데 이용,

Interface, class, method, event, delegate 만들 수 있음

<T>를 통하여 다양한 타입의 클래스를 다양하게 인스턴스화가능

(중복안시키고)



함수형언어, 람다식

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

제너릭 클래스, 상속을 이용하여 Inventory 만들기  (0) 2019.04.01
List를 이용하여 Inventory 만들기  (0) 2019.04.01
Array를 이용하여 Inventory 만들기  (0) 2019.04.01
2. for, foreach, while  (0) 2019.03.27
1. Method  (0) 2019.03.24
:

1. 선택정렬(Selection Sort) 정리 및 구현

C#/알고리즘 2019. 3. 29. 09:45

선택정렬(Selection Sort)


선택정렬이란 오름차순, 내림차순 원하는 순으로 정렬한다. 
제자리 정렬(in-place sorting) 중 하나이다.

오름차순으로 정렬한다는 가정하에, 현재 위치의 값보다 더 작은 값을 배열을 쭉 훑으며 찾는다. 
찾았을 경우에 현재 위치의 값과 더 작은 값을 바꾸는 정렬 알고리즘이다.

즉, 0번 방의 값을 1번방부터 끝방까지 돌아서 더 작은 값을 찾아 자리를 바꾼다. 
다음엔 1번 방의 값을 2번방부터 끝방까지 돌아서 더 작은 값을 찾고, 찾았을 경우 두 값을 바꾼다.


0번방 / 101번방 / 52번방 / 93번방 / 144번방 / 1

위와 같은 배열이 있다. 0번방 부터 4번방까지 10, 5, 9, 14, 1 순으로 저장되어있다.

오름차순으로 정렬한 결과는 1, 5, 9, 10, 14가 되어야 한다.

선택정렬을 실행하여 정렬하는 과정이다.


1. 0번방의 값 10을 왼손에 들고 4번방까지 돌면서 10보다 작은 값을 찾는다.

2. 1번방의 값 5가 10보다 작아서 오른손에는 5라는 값을 들고 5보다 작은 값을 계속 찾아간다.

3. 2번방, 3번방을 돌았으나 5보다 커서 4번방까지 왔다.

4. 4번방의 값이 1로 내 오른손에 있는 5보다 작기 때문에 오른손에 있는 걸 1로 바꾼다.

5. 4번방에 내 왼손에 있는 10을 4번방에 놓고 다시 내 방인 0번방으로 와 내 방에는 오른손에 있는 1이라는 값을 둔다.



0번방 / 11번방 / 52번방 / 93번방 / 144번방 / 10

그럼 이렇게 0번방과 4번방의 값을 서로 교환했으므로 이렇게 된다.

0번방에는 제일 작은 값을 뒀으므로 이젠 1번방부터 시작한다.


1. 1번방의 값 5를 왼손에 들고 4번방까지 돌면서 5보다 작은 값을 찾는다.

2. 2번방, 3번방, 4번방을 다 돌아봤지만 내 왼손의 5보다 작은 값은 없었다.


0번방 / 11번방 / 52번방 / 93번방 / 144번방 / 10

이제 2번방부터 시작한다.

1. 2번방의 값 9를 왼손에 들고 4번방까지 돌면서 9보다 작은 값을 찾는다.

2. 3번방, 4번방을 돌아봤지만 내 왼손의 9보다 작은 값은 없었다.


0번방 / 11번방 / 52번방 / 93번방 / 144번방 / 10

자, 이제 3번방부터 시작한다.

1. 3번방의 값 14를 왼손에 들고 4번방을 가본다.

2. 4번방의 값 10이 내 왼손의 14보다 작다.

3. 4번방의 값 10을 내 오른손에 들고, 왼손의 14를 4번방에 내려놓는다.

4. 내 방 3번방으로 돌아와 3번방에 오른손의 10을 내려놓는다.


0번방 / 11번방 / 52번방 / 93번방 / 104번방 / 14

방을 열심히 돌아다니면서 정렬한 결과 1, 5, 9, 10, 14 (오름차순)으로 정렬이 되었다.

이게 바로 선택정렬 알고리즘이다.




아래는 C#의 List를 이용하여 List에 저장된 10, 5, 7, 13, 2의 값을 오름차순으로 정렬한 결과와 코드이다.

10, 5, 7, 13, 2를 선택정렬(오름차순)을 한 결과 2, 5, 7, 10, 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Selcetion_Sort
{
    //summary
    //List에 저장된 값을 직접 정의한 Method로 선택정렬(Selection_Sort)한다.
    class App
    {
        public App() //생성자
        {
            List<int> list = this.MakeList(); //int형 List를 가리키는 변수 list에 리턴받은 itemCodeList를 저장
            PrintList(list); //정렬되지 않은 list(10, 5, 7, 13, 2)를 출력하는 메서드로 보낸다
            Console.Write("\n");
            list = SelectionSort(list); //정렬하는 메서드에 list를 보내서 정렬한다.
            PrintList(list); //정렬 결과를 출력
            Console.Write("\n");
            Console.ReadKey();
        }
 
 
        public List<int> SelectionSort(List<int> list) //리스트를 정렬하는 메서드
        {
            int indexTemp = 0//index을 임시로 저장할 temp변수, 리스트의 첫번째 값으로 초기화
            int valueTemp; //값을 임시로 저장할 temp변수
            for (int index = 0; index <= list.Count-2; index++//0번부터 끝까지 비교 정렬하기 위한 반복문
            {
                valueTemp = list[index];
                for (int count = index+1; count <= list.Count-1; count++//현재 가지고 있는 값을 다른 값들과 비교하는 반복문
                {
                    if (list[count] < valueTemp) //현재 index칸의 값이 첫번째 칸의 값보다 작으면 temp에 저장
                    {
                        indexTemp = count;
                        valueTemp = list[count];
                    }
                }
                if (indexTemp != 0//indexTemp에 비교 최소값이 없으면 자리바꿈 실행
                {
                    valueTemp = list[indexTemp];
                    list[indexTemp] = list[index];
                    list[index] = valueTemp;
                }
                indexTemp = 0;
            }
            return list;
        }
 
        public void PrintList(List<int> list) //리스트의 값을 출력하는 메서드
        {
            foreach (int printList in list) //list의 처음부터 끝까지 돌면서 int형 값을 printList에 저장
            {
                Console.Write("{0}, ", printList); //저장된 값 printList를 출력
            }
        }
 
        public List<int> MakeList() //리스트를 생성하고 값을 저장하는 메서드
        {
            List<int> itemCodeList = new List<int>();
            //정수형 List 인스턴스를 생성하고, 그를 가르키는 itemCodeList 변수생성
            itemCodeList.Add(10); //0번 Index의 Value는 10
            itemCodeList.Add(5); //1번 index의 Value는 5
            itemCodeList.Add(7); //2번 index의 Value는 7
            itemCodeList.Add(13); //3번 index의 Value는 13
            itemCodeList.Add(2)//4번 index의 Value는 2
            return itemCodeList;
        }
 
    }
}
 
cs


'C# > 알고리즘' 카테고리의 다른 글

Baek Joon Online Judge  (0) 2019.03.27
:

공부할 것과 정리중인 것

C#/Problems 2019. 3. 28. 17:56

IEnumerator
IEnumerable

object 형

Collection

ArrayList
Predicate
delegate


delegate 기능 
함수들을 통합 실행할 수 있음
delegate에 메서드를 참조하게 하여,
같은 타입의 인자를 보낼 때, 가지고 있는 메서드를 대신 호출
그래서 delegate type을 받는 메서드는 전달받은
delegate를 가지고 다른 메서드를 호출하여 그 값을 이용할 수 있다.

delegate chain
메서드를 delegate에 집어넣고 이름을 이용해 호출가능
(여러가지 메서드를 집어넣고 쓸 수 있다.
메서드의 주소를 집어넣고 delegate가 호출하는 식)
즉, delegate는 Method를 가리킬 수 있는 Type이다.

Call-Back Method
A의 메서드를 호출할 때, B메서드를 매개변수로 보내준다.
그리고 A메서드가 B메서드를 호출하는 것이 Call-Back Method

Event
인스턴스의 변화가 생길때 해당되는 값을 실행
던지는 객체, 받는 객체, 핸들러로 구성
if를 이용해 조건을 달고 조건이 해당하면 event변수에 있는
delegate주소로 가서 delegate를 호출하는 형식.
즉 event 발생시 event변수에 있는 delegate를 호출
호출된 delegate는 갖고 있는 Method를 호출

:

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
:

공부중

C#/Problems 2019. 3. 27. 22:13

Starcraft

1. 종족 선택(Class)
2. 건물 및 일꾼 4기 생성 (Class Field)
3. 명령어 유닛선택 시 유닛의 목록 출력 (Class Field output)
4. 이동, 공격, 멈춤 기능 (Method)
5. 기본적으로 일을 하는 중이어서 시간단위로 자원 증가
6. 프로토스는 Shield HP따로, Shield는 시간단위 회복
7. 테란은 Land 기능, if Land state시 일꾼 일 정지
8. 저그는 라바 3기 드론 4기
9. 라바는 드론으로 변경가능하게



Class 종족
ㄴ field 유닛, 건물


유닛, 건물 인스턴스 생성시
그의 주소를 인스턴스 변수 배열에 하나씩 집어넣는다.
배열이 NULL일 경우 그 칸에 인스턴스의 주소를 넣는다.
출력시 배열을 반복하여 출력하면 된다.
배열에서 파괴되면 Index를 주어서 파괴하면 된다.



IEnumerator
object Current { get; }
bool MoveNext();
void Reset();


그룹을 만들고 싶거나 오브젝트를 만들 때, 두 개의 방법이 있음
오브젝트의 Array를 만들거나 오브젝트의 Collection을 만들거나
배열은 만들고 사용하는데 유용하고 개체수가 정해진 오브젝트를 관리하는데 유용

컬렉션은 개체를 관리하는데 있어 유연한 방법을 제공한다
Array와는 다르게 컬렉션은 동적으로 확장 및 축소가 가능(List 같은)


Generic List vs ArrayList


Get&Set
Predicate ? (델리게이트 등장!)

delegate = 대리자로서 모든 메서드에 연결할 수 있다.
즉, A메서드의 매개변수로 메서드를 전달 할 수 있다.

아직 설명은 못하겠는데 delegate랑 ienumerable, ienumerator는 사기다

:

Baek Joon Online Judge

C#/알고리즘 2019. 3. 27. 16:21

https://www.acmicpc.net/

'C# > 알고리즘' 카테고리의 다른 글

1. 선택정렬(Selection Sort) 정리 및 구현  (0) 2019.03.29
: