<인벤토리>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
: