-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
201 lines (170 loc) · 6.41 KB
/
Program.cs
File metadata and controls
201 lines (170 loc) · 6.41 KB
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
using System;
using System.Collections.Generic;
namespace TheForeachCycle
{
internal class Program
{
static void Main(string[] args)
{
Player player = new Player();
player.Inventory.Add(new Coin(5));
player.Inventory.Add(new HealthPotion(30, 5));
player.Inventory.Add(new Key());
player.Inventory.Add(new Item("Сапог"));
player.Inventory.Add(new Item("Меч"));
player.Inventory.Add(new Item("Палка", true, 6));
bool isPlayed = true;
while(isPlayed)
{
DisplayMenu();
if (int.TryParse(Console.ReadLine(), out int choice))
{
switch (choice)
{
case 1: DisplayInventory(player); break;
case 2:
DisplayInventory(player);
UseItem(player);
break;
case 3: isPlayed = false; break;
default: ShowMessage("Неизвестное действие!", ConsoleColor.Red); break;
}
}
else
ShowMessage("Ошибка ввода! Введите число.", ConsoleColor.Red);
Console.ReadKey();
}
}
public static void DisplayMenu()
{
Console.Clear();
Console.WriteLine("===МЕНЮ===\n");
Console.WriteLine("1. Показать инвентарь.");
Console.WriteLine("2. Использовать предмет.");
Console.WriteLine("3. Выход.");
Console.Write("\nВыберите действие: ");
}
public static void DisplayInventory(Player player)
{
Console.Clear();
int i = 1;
Console.WriteLine("===Инвентарь===\n");
foreach(Item item in player.Inventory)
{
Console.Write($"{i}. {item.Name}");
if (item.IsStackable)
Console.WriteLine($" (x{item.Count})");
else
Console.WriteLine();
i++;
}
}
public static void UseItem(Player player)
{
Console.Write("Введите номер предмета: ");
if (int.TryParse(Console.ReadLine(), out int choice))
{
if (choice < 1 || choice > player.Inventory.Count)
{
ShowMessage("Неверный номер предмета!", ConsoleColor.Red);
return;
}
Item selectedItem = player.Inventory[choice - 1];
if (!selectedItem.CanBeUsed)
{
selectedItem.Use();
return;
}
if (selectedItem is HealthPotion potion)
{
if (player.Health == player.MaxHealth)
Console.WriteLine($"У вас уже максимальное HP: {player.Health}/{player.MaxHealth}");
else
{
selectedItem.Use();
int neededHeal = player.MaxHealth - player.Health;
int actualHeal = Math.Min(potion.HealPower, neededHeal);
player.Health += actualHeal;
Console.WriteLine($"Ваше HP: {player.Health}/{player.MaxHealth}");
}
}
else
selectedItem.Use();
if (selectedItem.IsStackable && selectedItem.Count > 1)
selectedItem.Count--;
else
player.Inventory.RemoveAt(choice - 1);
}
else
ShowMessage("Ошибка ввода! Введите число.", ConsoleColor.Red);
}
public static void ShowMessage(string massage, ConsoleColor color = ConsoleColor.White)
{
Console.ForegroundColor = color;
Console.WriteLine("\n\n" + massage);
Console.ResetColor();
Console.ReadKey();
}
}
public class Player
{
public int Health { get; set; }
public int MaxHealth { get; }
public List<Item> Inventory { get; }
public Player(int health = 20, int maxHealth = 100)
{
Health = Math.Min(health, maxHealth);
Inventory = new List<Item>();
MaxHealth = maxHealth;
}
}
public class Item
{
public string Name { get; }
public bool IsStackable { get; }
public int Count { get; set; }
public virtual bool CanBeUsed => true;
public Item(string name, bool isStackable = false, int count = 1)
{
Name = name;
IsStackable = isStackable;
if(isStackable)
Count = count;
else
Count = 1;
}
public virtual void Use()
{
Console.WriteLine($"Вы использовали {Name}");
}
}
public class HealthPotion : Item
{
public int HealPower;
public HealthPotion(int healPower = 10, int count = 1) : base("Зелье здоровья", true, count)
{
HealPower = healPower;
}
public override void Use()
{
Console.WriteLine($"Вы использовали {Name}. Восстановлено {HealPower} HP.");
}
}
public class Coin : Item
{
public Coin(int count = 1) : base("Монета", true, count) { }
public override bool CanBeUsed => false;
public override void Use()
{
Console.WriteLine("Монету нельзя использовать, это валюта.");
}
}
public class Key : Item
{
public Key() : base("Ключ") { }
public override void Use()
{
Console.WriteLine($"Вы использовали {Name} для открытия двери.");
}
}
}