-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
200 lines (167 loc) · 6.62 KB
/
Program.cs
File metadata and controls
200 lines (167 loc) · 6.62 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace CSharpPraktice
{
internal class Program
{
private static Random rand = new Random();
static void Main(string[] args)
{
List<Enemy> enemies = new List<Enemy>
{
new Goblin(),
new SkeletWarrior(),
new KingOfWolf()
};
int totalGold = 0;
Console.WriteLine("Выберите врага: ");
Console.WriteLine("1 - Гоблин");
Console.WriteLine("2 - Скелет-воин");
Console.WriteLine("3 - Король волков");
Console.WriteLine("4 - Все");
Console.SetCursorPosition(16, 0);
if (int.TryParse(Console.ReadLine(), out int choice))
{
Console.Clear();
if (choice == 4)
{
foreach (var enemy in enemies)
{
LootResult result = SimulateLootDrop(enemy);
result.PrintResult();
totalGold += result.Gold;
Console.WriteLine();
}
}
else if (choice >= 1 && choice <= enemies.Count)
{
LootResult result = SimulateLootDrop(enemies[choice - 1]);
result.PrintResult();
totalGold = result.Gold;
}
else
{
Console.WriteLine("Вы ввели не верные данные");
}
Console.WriteLine($"\nВсего за сессию добыто золота: {totalGold}");
}
else
{
Console.Clear();
Console.WriteLine("Введите число от 1 до 4");
}
}
static LootResult SimulateLootDrop(Enemy enemy)
{
int inventorySize = rand.Next(1, 4);
List<string> lootItems = new List<string>();
int gold = 0;
for (int i = 0; i < inventorySize; i++)
{
string loot = GetRandomLootItem(enemy.LootPool, enemy.DropChance, enemy.TotalWeight);
lootItems.Add(loot);
if (loot.Contains("Золотая монета"))
{
Match findNumber = Regex.Match(loot, @"\d+");
if (findNumber.Success)
gold += int.Parse(findNumber.Value);
}
}
return new LootResult(enemy.Name, inventorySize, lootItems, gold);
}
static string GetRandomLootItem(string[] lootPool, int[] dropChance, int totalWeight)
{
if (lootPool.Length != dropChance.Length)
throw new ArgumentException("Массивы lootPool и dropChance должны иметь одинаковую длину");
if (lootPool.Length == 0)
throw new ArgumentException("Массив lootPool не может быть пустым");
int randomNumber = rand.Next(0, totalWeight);
int currentRange = 0;
for (int i = 0; i < lootPool.Length; i++)
{
currentRange += dropChance[i];
if (randomNumber < currentRange)
return lootPool[i];
}
throw new InvalidOperationException("Не удалось выбрать предмет. Проверьте массивы lootPool и dropChance.");
}
}
public class LootResult
{
public string EnemyName { get; }
public int InventorySlots { get; }
public List<string> Items { get; }
public int Gold { get; }
public LootResult(string enemyName, int inventorySlots, List<string> items, int gold)
{
EnemyName = enemyName;
InventorySlots = inventorySlots;
Items = items;
Gold = gold;
}
public void PrintResult()
{
Console.WriteLine($"=== Победа над монстром: {EnemyName} ===");
Console.WriteLine($"Слотов лута: {InventorySlots}");
Console.WriteLine("Выпало:");
foreach (var item in Items)
{
Console.WriteLine($"- {item}");
}
Console.WriteLine($"Золота в этой добыче: {Gold}");
}
}
public abstract class Enemy
{
public string Name { get; }
public string[] LootPool { get; }
public int[] DropChance { get; }
public int TotalWeight { get; }
public Enemy(string name, string[] lootPool, int[] dropChance)
{
if (lootPool.Length != dropChance.Length)
throw new ArgumentException("Массивы lootPool и dropChance должны иметь одинаковую длину");
if (lootPool.Length == 0)
throw new ArgumentException("Массив lootPool не может быть пустым");
Name = name;
LootPool = lootPool;
DropChance = dropChance;
TotalWeight = dropChance.Sum();
}
}
public class Goblin : Enemy
{
public Goblin()
: base(
"Гоблин",
new[] { "Золотая монета (1)", "Ржавый кинжал", "Кусок хлеба", "Слабая лечебная трава", "Ничего" },
new[] { 50, 15, 30, 10, 5 }
)
{
}
}
public class SkeletWarrior : Enemy
{
public SkeletWarrior()
: base(
"Скелет-воин",
new[] { "Золотая монета (5)", "Груда костей", "Сломаный щит", "Ром", "Лук из костей", "Ничего" },
new[] { 35, 10, 30, 5, 1, 5 }
)
{
}
}
public class KingOfWolf : Enemy
{
public KingOfWolf()
: base(
"Король волков",
new[] { "Золотая монета (36)", "Зуб короля волков", "Шкура короля волков", "Ничего" },
new[] { 13, 5, 2, 80 }
)
{
}
}
}