'C#/실습'에 해당되는 글 45건

  1. 2019.10.31 10.31 제너릭을 이용해 사용자 지정 타입 활용하기 (where문 비활용)
  2. 2019.10.31 10.31 제너릭을 이용해 사용자 지정 타입 활용하기 (where문 활용)
  3. 2019.10.30 10.30 클래스로 LinkedList 구현하기 (메서드, 속성의 재귀호출)
  4. 2019.10.24 10.23 Stack 활용하기
  5. 2019.10.24 10.23 배열에서 숫자이동시키기
  6. 2019.10.24 10.24 벡터
  7. 2019.10.23 2048 프로그램 (저장기능x)
  8. 2019.10.17 10.17 상속 클래스를 이용한 뽑기박스

10.31 제너릭을 이용해 사용자 지정 타입 활용하기 (where문 비활용)

C#/실습 2019. 10. 31. 14:10

예제:

코드:

 

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 _10._31_step1
{
    
    class App
    {
        public App()
        {
            Inventory<Item> inventory = new Inventory<Item>(10);
            inventory.AddItem(new Item(10"장검"));
            inventory.AddItem(new Item(11"단검"));
            inventory.AddItem(new Item(12"활"));
            Console.WriteLine("초기 배열 값:");
            inventory.Print();
            inventory.RemoveItem("단검");
            Console.WriteLine("단검 지운 값:");
            inventory.Print();
            Console.WriteLine("활 찾기");
            Console.WriteLine(inventory.FindItem("활").Id);
 
            
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._31_step1
{
    class Inventory <T> //where T: Item //Item 클래스나 Item 파생 클래스만 들어올 수 있다.
    {
        private T[] items;
        public T this[int i]
        {
            get
            {
                return this.items[i];
            }
        }
        public void AddItem(T item)
        {
            for (int i=0; i<items.Length; i++)
            {
                if(items[i] == null)
                {
                    items[i] = item;
                    break;
                }
            }
        }
        public void RemoveItem(string name)
        {
            for(int i =0; i<items.Length; i++)
            {
                if(items[i] is Item)
                {
                    if((items[i] as Item).Name == name)
                    {
                        items[i] = default;
                    }
                }                
            }
        }
        public T FindItem(string name)
        {
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] is Item)
                {
                    if ((items[i] as Item).Name == name)
                    {
                        return items[i];
                    }
                    continue;
                }
                continue;
            }
            return default;
        }
        public void Print()
        {
            foreach(var data in items)
            {
                if(data != null && data is Item)
                {
                    Item item = data as Item;
                    Console.WriteLine("ID: {0}  Name: {1}"item.Id, item.Name);
                }
            }
        }
 
        public Inventory(int capacity)
        {
            this.items = new T[capacity];
        }
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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 _10._31_step1
{
    class Item
    {
        public int Id { get; private set; }
        public string Name { get; private set; }
        public Item(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

:

10.31 제너릭을 이용해 사용자 지정 타입 활용하기 (where문 활용)

C#/실습 2019. 10. 31. 13:51

예제:

코드:

 

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 _10._31_step1
{
    
    class App
    {
        public App()
        {
            Inventory<Item> inventory = new Inventory<Item>(10);
            inventory.AddItem(new Item(10"장검"));
            inventory.AddItem(new Item(11"단검"));
            inventory.AddItem(new Item(12"활"));
            Console.WriteLine("초기 배열 값:");
            inventory.Print();
            inventory.RemoveItem("단검");
            Console.WriteLine("단검 지운 값:");
            inventory.Print();
            Console.WriteLine("활 찾기");
            Console.WriteLine(inventory.FindItem("활").Id);
 
            
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._31_step1
{
    class Inventory <T> where T: Item //Item 클래스나 Item 파생 클래스만 들어올 수 있다.
    {
        private T[] items;
        public T this[int i]
        {
            get
            {
                return this.items[i];
            }
        }
        public void AddItem(T item)
        {
            for (int i=0; i<items.Length; i++)
            {
                if(items[i] == null)
                {
                    items[i] = item;
                    break;
                }
            }
        }
        public void RemoveItem(string name)
        {
            for(int i =0; i<items.Length; i++)
            {
                if(items[i] is Item)
                {
                    if((items[i] as Item).Name == name)
                    {
                        items[i] = default;
                    }
                }                
            }
        }
        public T FindItem(string name)
        {
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] is Item)
                {
                    if (((Item)items[i]).Name == name)
                    {
                        return items[i];
                    }
                    continue;
                }
                continue;
            }
            return default;
        }
        public void Print()
        {
            foreach(var data in items)
            {
                if(data != null)
                Console.WriteLine("ID: {0}  Name: {1}",data.Id, data.Name);
            }
        }
 
        public Inventory(int capacity)
        {
            this.items = new T[capacity];
        }
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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 _10._31_step1
{
    class Item
    {
        public int Id { get; private set; }
        public string Name { get; private set; }
        public Item(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

:

10.30 클래스로 LinkedList 구현하기 (메서드, 속성의 재귀호출)

C#/실습 2019. 10. 30. 16:41

예제:

코드:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10._30_step2
{
    class App
    {
        public App()
        {
            var list = new LinkedList();
            list.Add("홍길동");
            list.Add("정약용");
            list.Add("이호준");
            list.Add("홍길동");
            list.Add("정약용");
            list.Add("이호준");
            Console.WriteLine("인스턴스 개수: {0}",list.Count);
            Console.WriteLine("인스턴스 : ");
            list.Print();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10._30_step2
{
    class LinkedList
    {
        public Node first;
        public Node node;
        public int count = 0;
        public int Count
        {
            get
            {
                if (first != null)
                {
                    if (node.next != null)
                    {
                        node = node.next;
                        count++;
                        return Count;
                    }
                    else
                    {
                        count++;
                        node = first;
                        return count;
                    }
                }
                return count;
            }
        }
        public LinkedList()
        {
            Console.WriteLine("LinkedList의 생성자 호출됨");
        }
        
        public void Print()
        {
            if (first != null)
            {
                if (node.next != null)
                {
                    Console.WriteLine(node.value);
                    node = node.next;
                    Print();
                }
                else
                {
                    Console.WriteLine(node.value);
                }
            }
        }
        public void Add(string data)
        {
            Node node = new Node(data);
            if (this.first == null)
            {
                this.first = node;
                this.node = node;
            }
            else
            {
                Node lastNode = FindLastNode(this.first);
                lastNode.next = node;
            }
        }
        public Node FindLastNode(Node node)
        {
            if (node.next == null)
            {
                return node;
            }
            return FindLastNode(node.next);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10._30_step2
{
    class Node
    {
        public string value;
        public Node next;
        public Node(string data)
        {
            this.value = data;
            Console.WriteLine("새로운 노드 생성됨. (value: {0})", this.value);
        }
    }
}

 

 

여기서 주의해야 할 점은 재귀호출을 할 때이다.

재귀 호출을 한 메소드(반환타입이 있는경우)에서 return문을 사용해 메소드를 종료할 경우, 해당 재귀된 메소드가 종료되는것이지 재귀 호출한 가장 바깥의 메소드가 종료가 되는게 아니기 때문에 if문 다음에 재귀호출 후에 쓰게된 return은 결국 사용하게 된다.

반환 타입이 없는 메소드에서 return을 쓰면 가장 바깥의 메소드를 종료하지만 반환타입이 있는 경우는 for문의 break 와 같이 가장 가까운 메소드를 종료할 뿐이다.

따라서 return (재귀호출문); 을 쓰면 된다.

:

10.23 Stack 활용하기

C#/실습 2019. 10. 24. 17:39

기본적으로 Stack은 컬렉션 타입이며 Last in First Out 속성을 가지고있다.

코드:

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._23_step1
{
    class App
    {
        public App()
        {
            Stack<string> numbers;
            numbers = new Stack<string>();
            //삽입
            numbers.Push("하나");
            numbers.Push("둘");
            numbers.Push("셋");
            numbers.Push("넷");
 
            //추출
            var pop = numbers.Pop();
            Console.WriteLine(pop);
            //출력
            Console.WriteLine(numbers.Count);
            foreach(string number in numbers)
            {
                Console.WriteLine(number);
            }
            for(int i=0;i<=numbers.Count; i++)
            {
                Console.WriteLine(numbers.Pop());
            }
            while(true)
            {
                Console.WriteLine(numbers.Pop());
                if(numbers.Count == 0)
                {
                    break;
                }
            }
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
:

10.23 배열에서 숫자이동시키기

C#/실습 2019. 10. 24. 17:35

0,4 위치에 있는 숫자 2를 2,0으로 옮겼다가 다시 0,4로(지그재그) 옮긴다.

코드:

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._23_step3
{
    class App
    {
        int[,] arr =
        {
            {0,0,0,0,2},
            {0,0,0,0,0},
            {0,0,0,0,0}
        };
        int count=1;
        public App()
        {
            MoveTo20(04);
            MoveTo04(20);
        }
        public void MoveTo04(int row, int col)
        {
            if (count %3!=0&& Check(row, col + 1== true && arr[row,col+1==0 )
            {
                arr[row, col + 1= arr[row, col];
                arr[row, col] = 0;
                count++;
                Print();
                MoveTo04(row, col+1);
            }
            else if(count%3==0&& Check(row-1, col)==true && arr[row-1,col]==0)
            {
                arr[row-1, col] = arr[row, col];
                arr[row, col] = 0;
                count++;
                Print();
                MoveTo04(row-1, col);
            }
        }
        public void MoveTo20(int row, int col)
        {
            if(Check(row, col - 1)==true && arr[row,col-1== 0)
            {
                arr[row, col - 1= arr[row, col];
                arr[row, col] = 0;
                Print();
                MoveTo20(row, col - 1);
            }
            else if (Check(row, col - 1== false && Check(row + 1, col) == true && arr[row + 1, col] == 0)
            {
                arr[row + 1, col] = arr[row, col];
                arr[row, col] = 0;
                Print();
                MoveTo20(row + 1, col);
            }
            
        }
        public void Print()
        {
            Console.WriteLine("--------------------");
            for (int i=0;i<arr.GetLength(0);i++)
            {
                for(int j=0;j<arr.GetLength(1);j++)
                {
                    
                    Console.Write(arr[i, j] + "\t");
                }
                Console.WriteLine();
            }
            Console.WriteLine("--------------------");
        }
        public bool Check(int nextIndexRow, int nextIndexCol)
        {
            if(nextIndexRow>=0 && nextIndexRow<3 
               && nextIndexCol>=0 && nextIndexCol<5 
               && arr[nextIndexRow, nextIndexCol]==0)
            {
                return true;
            }
            return false;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
:

10.24 벡터

C#/실습 2019. 10. 24. 17:31

예제:

벡터A (-5,-6)

벡터B (7,3)

을 잇는 벡터C의 크기와 성분을 나타내고 

벡터C의 단위벡터를 구하여 그 성분과 크기를 출력한다.

코드:

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 _10._24_step2
{
    class App
    {
        public App()
        {
            Vector2 zeroVector = new Vector2(00);
            Vector2 VectorA = new Vector2(-5-6);
            Vector2 VectorB = new Vector2(73);
            double vectorCPower = GetDistance(VectorA, VectorB);
            Console.WriteLine(vectorCPower);
            Vector2 VectorC = GetVector(VectorB, VectorA);
            Console.WriteLine("{0},{1}", VectorC.x, VectorC.y);
            Vector2 unitVectorC = GetNormalize(VectorC);
            Console.WriteLine("단위벡터:");
            Console.WriteLine("성분:{0},{1} 크기:{2}", unitVectorC.x, unitVectorC.y,GetDistance(zeroVector,unitVectorC));
        }
        public Vector2 GetNormalize(Vector2 vector)
        {
            Vector2 zeroVector = new Vector2(00);
            double distance = GetDistance(zeroVector, vector);
            Vector2 newVector = new Vector2(vector.x / distance, vector.y / distance);
            return newVector;
        }
        public Vector2 GetVector(Vector2 A , Vector2 B)
        {
            double x = A.x - B.x;
            double y = A.y - B.y;
            Vector2 vector = new Vector2(x, y);
            return vector;
        }
        public double GetDistance(Vector2 A, Vector2 B)
        {
            double a =Math.Abs(A.x - B.x);
            double b = Math.Abs(A.y - B.y);
            double c;
            c = Math.Pow(a, 2+ Math.Pow(b, 2);
            return Math.Sqrt(c);
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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 _10._24_step2
{
    class Vector2
    {
        public double x;
        public double y;
        public Vector2(double x, double y)
        {
            this.x = x;
            this.y = y;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
:

2048 프로그램 (저장기능x)

C#/실습 2019. 10. 23. 15:02

10.22 step2.exe
0.01MB

출력 예:

초기화면
숫자를 받기전 모습
숫자를 받은 후 모습
방향키로 민 후
다시 숫자를 받는다

코드:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _10._22_step2
{
    class App
    {
        int[,] arr = new int[4, 4];
        Random rand = new Random();
        Random rand2 = new Random();
        public App()
        {
            while (true)
            {
                Console.WriteLine("아무 키를 눌러주세요.");
                Console.ReadKey();
                Console.Clear();
                Console.WriteLine("------------------------");
                Show();
                Console.WriteLine("------------------------");
                for (int i = 0; i < 16; i++)
                {
                    int row = rand.Next(0, 4);
                    int col = rand.Next(0, 4);
                    int num = rand2.Next(0, 7);

                    if (num == 0)
                    {
                        num = 2;
                    }
                    else if (num == 1|| num == 2||num==3
                        ||num==4 || num == 5 || num == 6)
                    {
                        num = 0;
                    }                    
                    if (arr[row, col] == 0)
                    {
                        arr[row, col] = num;
                    }
                }
                Console.WriteLine("아무 키를 누르면 숫자를 받아옵니다.");
                Console.ReadKey();
                Console.Clear();
                Console.WriteLine("------------------------");
                Show();
                Console.WriteLine("------------------------");
                Console.WriteLine("방향키를 눌러 미세요.");
                string key = Console.ReadKey().Key.ToString();
                InputKey(key);
                Console.WriteLine("------------------------");
                Show();
                Console.WriteLine("------------------------");

            }
        }
        public void Show()
        {
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Console.Write(arr[i, j] + "\t");
                }
                Console.WriteLine();
            }
        }
       
        public void InputKey(string input)
        {
            switch (input)
            {
                case "RightArrow":
                    {
                        for(int k = 3; k>=0; k--)//숫자사이 0 제거
                        {
                            for (int i = 3; i >= 0; i--)
                            {
                                for (int j = 3; j >= 0; j--)
                                {
                                    if (j - 1 >= 0 && arr[i, j] == 0)//o이다
                                    {
                                        arr[i, j] = arr[i, j - 1];
                                        arr[i, j - 1] = 0;
                                    }
                                }
                            }
                        }                        
                            for (int i = 3; i >= 0; i--)
                            {
                                for (int j = 3; j >= 0; j--)
                                {
                                    if (j - 1 >= 0 && arr[i, j] != 0)//숫자다
                                    {
                                        if (arr[i, j] == arr[i, j - 1])//앞에 수가 같다
                                        {
                                            arr[i, j] += arr[i, j - 1];
                                            arr[i, j - 1] = 0;
                                        }
                                    }
                                }
                            }
                        for (int k = 3; k >= 0; k--)//숫자사이 0 제거
                        {
                            for (int i = 3; i >= 0; i--)
                            {
                                for (int j = 3; j >= 0; j--)
                                {
                                    if (j - 1 >= 0 && arr[i, j] == 0)//o이다
                                    {
                                        arr[i, j] = arr[i, j - 1];
                                        arr[i, j - 1] = 0;
                                    }
                                }
                            }
                        }
                        break;
                    }
                case "LeftArrow":
                    {
                        for (int k = 0; k <= 3; k++)//숫자사이 0 제거
                        {
                            for (int i = 0; i <= 3; i++)
                            {
                                for (int j = 0; j <= 3; j++)
                                {
                                    if (j + 1 <= 3 && arr[i, j] == 0)//o이다
                                    {
                                        arr[i, j] = arr[i, j + 1];
                                        arr[i, j + 1] = 0;
                                    }
                                }
                            }
                        }
                        for (int i = 0; i <= 3; i++)
                        {
                            for (int j = 0; j <= 3; j++)
                            {
                                if (j + 1 <= 3 && arr[i, j] != 0)//숫자다
                                {
                                    if (arr[i, j] == arr[i, j + 1])//앞에 수가 같다
                                    {
                                        arr[i, j] += arr[i, j + 1];
                                        arr[i, j + 1] = 0;
                                    }
                                }
                            }
                        }
                        for (int k = 0; k <= 3; k++)//숫자사이 0 제거
                        {
                            for (int i = 0; i <= 3; i++)
                            {
                                for (int j = 0; j <= 3; j++)
                                {
                                    if (j + 1 <= 3 && arr[i, j] == 0)//o이다
                                    {
                                        arr[i, j] = arr[i, j + 1];
                                        arr[i, j +1] = 0;
                                    }
                                }
                            }
                        }
                        break;
                    }
                case "UpArrow":
                    {
                        for (int k = 0; k <= 3; k++)//숫자사이 0 제거
                        {
                            for (int i = 0; i <= 3; i++)
                            {
                                for (int j = 0; j <= 3; j++)
                                {
                                    if (j + 1 <= 3 && arr[j, i] == 0)//o이다
                                    {
                                        arr[j, i] = arr[j+1, i];
                                        arr[j+1, i] = 0;
                                    }
                                }
                            }
                        }
                        for (int i = 0; i <= 3; i++)
                        {
                            for (int j = 0; j <= 3; j++)
                            {
                                if (j + 1 <= 3 && arr[j, i] != 0)//숫자다
                                {
                                    if (arr[j, i] == arr[j+1, i])//앞에 수가 같다
                                    {
                                        arr[j, i] += arr[j+1, i];
                                        arr[j+1, i] = 0;
                                    }
                                }
                            }
                        }
                        for (int k = 0; k <= 3; k++)//숫자사이 0 제거
                        {
                            for (int i = 0; i <= 3; i++)
                            {
                                for (int j = 0; j <= 3; j++)
                                {
                                    if (j + 1 <= 3 && arr[j, i] == 0)//o이다
                                    {
                                        arr[j, i] = arr[j+1, i];
                                        arr[j+1, i] = 0;
                                    }
                                }
                            }
                        }
                        break;
                    }
                case "DownArrow":
                    {
                        for (int k = 3; k >= 0; k--)//숫자사이 0 제거
                        {
                            for (int i = 3; i >= 0; i--)
                            {
                                for (int j = 3; j >= 0; j--)
                                {
                                    if (j - 1 >= 0 && arr[j, i] == 0)//o이다
                                    {
                                        arr[j, i] = arr[j-1, i];
                                        arr[j-1,i] = 0;
                                    }
                                }
                            }
                        }
                        for (int i = 3; i >= 0; i--)
                        {
                            for (int j = 3; j >= 0; j--)
                            {
                                if (j - 1 >= 0 && arr[j, i] != 0)//숫자다
                                {
                                    if (arr[j, i] == arr[j-1, i])//앞에 수가 같다
                                    {
                                        arr[j, i] += arr[j-1, i];
                                        arr[j-1,i] = 0;
                                    }
                                }
                            }
                        }
                        for (int k = 3; k >= 0; k--)//숫자사이 0 제거
                        {
                            for (int i = 3; i >= 0; i--)
                            {
                                for (int j = 3; j >= 0; j--)
                                {
                                    if (j - 1 >= 0 && arr[j, i] == 0)//o이다
                                    {
                                        arr[j, i] = arr[j-1, i];
                                        arr[j-1,i] = 0;
                                    }
                                }
                            }
                        }
                        break;
                    }
            }

        }
    }
}


 

:

10.17 상속 클래스를 이용한 뽑기박스

C#/실습 2019. 10. 17. 23:10

예제:

Equipment 클래스에 상속된 두 아이템 타입(Weapon, Armor)을 만들고 List<Equipment>타입으로 저장한다.

뽑기박스 클래스에 상속된 두 박스(WeaponBox, ArmorBox) 를 만들어 각각의 박스가 해당되는 아이템만을 출력하게 만든다.

코드:

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.Text;
 
namespace ConsoleApp1
{
    class App
    {
        public App()
        {
            List<Equipment> equipments = new List<Equipment>();
            List<Equipment> weapons = new List<Equipment>();
            Weapon weapon1 = new Weapon("활");
            Weapon weapon2 = new Weapon("검");
            Weapon weapon3 = new Weapon("총");
 
            Armor armor1 = new Armor("가죽옷");
            Armor armor2 = new Armor("사슬옷");
            Armor armor3 = new Armor("다이아옷");
 
            equipments.Add(weapon1);
            equipments.Add(weapon2);
            equipments.Add(armor1);
            equipments.Add(armor2);
            equipments.Add(weapon1);
            equipments.Add(weapon3);
            for (int i = 0; i<equipments.Count; i++)
            {
                if(equipments[i].GetType() == typeof(Weapon))
                {
                    weapons.Add(equipments[i]);
                }//GetType을 이용해 타입을 반환하고 반환한 타입을 typeof를 이용해 그 타입 자체와 비교
            }
            Console.WriteLine("모든 아이템:");
 
            foreach (var output in equipments)
            {
                Console.WriteLine(output.GetName());
            }            
            WeaponBox weaponBox = new WeaponBox();
            foreach (var item in weapons)
            {
                weaponBox.equipments.Add(item);
            }
            weaponBox.OpenBox();
            Console.Read();
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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.Text;
 
namespace ConsoleApp1
{
    class Equipment
    {
        string name;
        public Equipment(string name)
        {
            this.name = name;
        }
        public string GetName()
        {
            return this.name;
        }
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
using System.Collections.Generic;
using System.Text;
 
namespace ConsoleApp1
{
    class Weapon: Equipment
    {        
        public Weapon(string name) : base(name)
        {
        }
         
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Collections.Generic;
using System.Text;
 
namespace ConsoleApp1
{
    class Armor : Equipment
    {
        public Armor(string name) : base(name)
        {
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Collections.Generic;
using System.Text;
 
namespace ConsoleApp1
{
    class LootBox
    {
        public List<Equipment> equipments;
        public LootBox()
        {
            this.equipments = new List<Equipment>();
        }
        public virtual void OpenBox()
        {
            foreach(var item in equipments)
            {
                Console.WriteLine($"상자를 열어서 {item.GetName()}을 획득했습니다.");
            }
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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.Text;
 
namespace ConsoleApp1
{
    class WeaponBox : LootBox
    {
        public WeaponBox()
        {
            
        }
        public override void OpenBox()
        {
            Console.WriteLine("무기상자를 열었습니다.");
            base.OpenBox();
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
 
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.Text;
 
namespace ConsoleApp1
{
    class ArmorBox :LootBox
    {
        public ArmorBox()
        {
 
        }
        public override void OpenBox()
        {
            base.OpenBox();
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

출력:

: