View difference between Paste ID: Z5kkc0y1 and C26sWULh
SHOW: | | - or go back to the newest paste.
1
using System;
2
3
namespace C_sharp_Light
4
{
5
    class Program
6
    {
7
        static void Main(string[] args)
8
        {
9-
            
9+
            Console.CursorVisible = false;
10
            Console.Write("Введите имя игрока: ");
11
            Person player = new Person(Console.ReadLine());
12
            TradeRoom tradeRoom = new TradeRoom(player);
13
            tradeRoom.Work();
14
        }
15
    }
16
17
	static class RandomStatic
18
    {
19
        static private Random _rand = new Random();
20
21
        static public int GetNext(int min, int max)
22
        {
23
            return _rand.Next(min,max);
24
25
        }
26
        static public int GetNext(int max)
27
        {
28
            return _rand.Next(max);
29
        }
30
    }
31
32
    static class DisplayingMessages
33
    {
34
        static public void ShowMessageWithColor(string message, ConsoleColor color, int CursorleftPosition)
35
        {
36
            Console.CursorLeft = CursorleftPosition;
37
            ConsoleColor defaultColor = Console.ForegroundColor;
38
            Console.ForegroundColor = color;
39
            Console.WriteLine(message);
40
            Console.ForegroundColor = defaultColor;
41
        }
42
43
        static public void ShowMessageWithColor(string message, ConsoleColor color, bool delay)
44
        {
45
            ConsoleColor defaultColor = Console.ForegroundColor;
46
            Console.ForegroundColor = color;
47
            Console.WriteLine(message);
48
            Console.ForegroundColor = defaultColor;
49
            if(delay)
50
                Console.ReadKey();
51
        }
52
    }
53
}
54
55
namespace C_sharp_Light
56
{
57
    class Item
58
    {
59
        private string _name;
60
        private int _cost;
61
        public int Cost
62
        {
63
            get
64
            {
65
                return _cost;
66
            }
67
        }
68
        public string Name
69
        {
70
            get
71
            {
72
                return _name;
73
            }
74
        }
75
        public Item()
76
        {
77
            _name = new string[] { "Железный меч", "Рогатый шлем", "Посох огня", "Кожаный сапоги", "Сладкий рулет" }[RandomStatic.GetNext(0, 5)];
78
            _cost = RandomStatic.GetNext(20, 150);
79
        }
80
        private Item(string name, int baseCost)
81
        {
82
            _name = name;
83
            _cost = baseCost;
84
        }
85
        public Item Clone()
86
        {
87
            return new Item(Name, Cost);
88
        }
89
        public string Info()
90
        {
91
            return $"{Name} стоит - {Cost}";
92
        }
93
    }
94
    class Person
95
    {
96
        private int _money;
97
        public string Name { get; private set; }
98
        public int Money 
99
        {
100
            get
101
            {
102
                return _money;
103
            } 
104
        }
105
        private List<Item> _inventory = new List<Item>();
106
        public int SelectedIndexItem { get; private set; }
107
        public Person()
108
        {
109
            _money = RandomStatic.GetNext(300, 900);
110
            SelectedIndexItem = 0;
111
            Name = "Торговец";
112
            int amountItems = RandomStatic.GetNext(3, 9);
113
            for (int i = 0; i < amountItems; i++)
114
            {
115
                _inventory.Add(new Item());
116
            }
117
        }
118
        public Person(string name)
119
        {
120
            _money = RandomStatic.GetNext(300, 900);
121
            SelectedIndexItem = 0;
122
            Name = name;
123
            int amountItems = RandomStatic.GetNext(3, 9);
124
            for (int i = 0; i < amountItems; i++)
125
            {
126
                _inventory.Add(new Item());
127
            }
128
        }
129
        //
130
        public bool CanBuyIt(Item iten)
131
        {
132
            if (Money >= iten.Cost) return true;
133
            else return false;
134
        }
135
        public int GetSizeInventory()
136
        {
137
            return _inventory.Count;
138
        }
139
        public string GetInfoItemByIndex(int index)
140
        {
141
            return _inventory[index].Info();
142
        }
143
        public void ChangeActiveItem(int step)
144
        {
145
            SelectedIndexItem += step;
146
            UpdateIndex();
147
        }
148
        public Item GiveItem()
149
        {
150
            Item temp = _inventory[SelectedIndexItem];
151
            _inventory.RemoveAt(SelectedIndexItem);
152
            _money += temp.Cost;
153
            UpdateIndex();
154
            return temp.Clone();
155
        }
156
        public Item PickItem()
157
        {
158
            return _inventory[SelectedIndexItem];
159
        }
160
        public void GetItem(Item item)
161
        {
162
            _inventory.Add(item);
163
            _money -= item.Cost;
164
            UpdateIndex();
165
        }
166
        public void DrawnInfo(bool isSelected, int CursorPositionX)
167
        {
168
            if (isSelected)
169
                DisplayingMessages.ShowMessageWithColor($"Инветарь {Name} - выбран", ConsoleColor.Green, CursorPositionX);
170
            else
171
                DisplayingMessages.ShowMessageWithColor($"Инветарь {Name} - не выбран", ConsoleColor.Red, CursorPositionX);
172
            DisplayingMessages.ShowMessageWithColor($"Денег: {Money}", ConsoleColor.Yellow, CursorPositionX);
173
            for (int i = 0; i < GetSizeInventory(); i++)
174
            {
175
                if (i == SelectedIndexItem && isSelected)
176
                    DisplayingMessages.ShowMessageWithColor($"{i}: {GetInfoItemByIndex(i)}", ConsoleColor.Green, CursorPositionX);
177
                else
178
                    DisplayingMessages.ShowMessageWithColor($"{i}: {GetInfoItemByIndex(i)}", ConsoleColor.White, CursorPositionX);
179
            }
180
        }
181
        private void UpdateIndex()
182
        {
183
            if (SelectedIndexItem < 0)
184
                SelectedIndexItem = 0;
185
            else if (SelectedIndexItem > _inventory.Count - 1)
186
                SelectedIndexItem = _inventory.Count - 1;
187
        }
188
    }
189
    class TradeRoom
190
    {
191
        private Person _player;
192
        private Person _trader;
193
        bool playerIsSelected = true;
194
        public TradeRoom(Person player)
195
        {
196
            _player = player;
197
            CreateNewTrader();
198
        }
199
        public void Work()
200
        {
201
            bool isOpen = true;
202
            int xPosMenu = 0, yPosMeny = 22;
203
            while (isOpen)
204
            {
205
                Console.Clear();
206
                DrawnInfoAboutTraders();
207
                Console.SetCursorPosition(xPosMenu, yPosMeny);
208
                Console.WriteLine($"{ConsoleKey.RightArrow} или {ConsoleKey.LeftArrow} - для смены активного лица " +
209
                    $"\n{ConsoleKey.UpArrow} и {ConsoleKey.DownArrow} - для выбора предмета" +
210
                    $"\n{ConsoleKey.Enter} - Покупка/продажа" +
211
                    $"\n{ConsoleKey.Tab} - Получить нового торговца" +
212
                    $"\n{ConsoleKey.Escape} - Выход");
213
                ConsoleKeyInfo key = Console.ReadKey();
214
                switch (key.Key)
215
                {
216
                    case ConsoleKey.RightArrow: case ConsoleKey.LeftArrow:
217
                        ChangeSellingSide();
218
                        break;
219
                    case ConsoleKey.UpArrow:
220
                        ChangeActiveItem(-1);
221
                        break;
222
                    case ConsoleKey.DownArrow:
223
                        ChangeActiveItem(1);
224
                        break;
225
                    case ConsoleKey.Tab:
226
                        CreateNewTrader();
227
                        break;
228
                    case ConsoleKey.Enter:
229
                        BuyOrSell();
230
                        break;
231
                    case ConsoleKey.Escape:
232
                        isOpen = false;
233
                        break;
234
                }
235
            }
236
        }
237
        private void BuyOrSell()
238
        {
239
            if (ActiveTrader().GetSizeInventory() > 0)
240
                if (UnactiveTrader().CanBuyIt(ActiveTrader().PickItem()))
241
                    UnactiveTrader().GetItem(ActiveTrader().GiveItem());
242
                else
243
                    DisplayingMessages.ShowMessageWithColor("ОШИБКА: У неактивного участника недостаточно денег для покупки предмета у активного", ConsoleColor.Red, true);
244
            else
245
                DisplayingMessages.ShowMessageWithColor("ОШИБКА: У активного участника больше не осталось вещей.", ConsoleColor.Red, true);
246
        }
247
        private void DrawnInfoAboutTraders()
248
        {
249
            int xPosPlayer = 0;
250
            int xPosTrader = xPosPlayer + 30;
251
            _player.DrawnInfo(playerIsSelected, xPosPlayer);
252
            Console.CursorTop = 0;
253
            _trader.DrawnInfo(!playerIsSelected, xPosTrader);
254
        }
255
        private void ChangeSellingSide()
256
        {
257
            playerIsSelected = !playerIsSelected;
258
        }
259
        private void CreateNewTrader()
260
        {
261
            _trader = new Person();
262
        }
263
        private void ChangeActiveItem(int step)
264
        {
265
            ActiveTrader().ChangeActiveItem(step);
266
        }
267
        private Person ActiveTrader()
268
        {
269
            if (playerIsSelected)
270
                return _player;
271
            else
272
                return _trader;
273
        }
274
        private Person UnactiveTrader()
275
        {
276
            if (playerIsSelected)
277
                return _trader;
278
            else
279
                return _player;
280
        }
281
    }
282
}