-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
159 lines (134 loc) · 4.77 KB
/
Program.cs
File metadata and controls
159 lines (134 loc) · 4.77 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
using System;
using System.Collections.Generic;
namespace Structures
{
internal class Program
{
static Random rand = new Random();
static void Main(string[] args)
{
Spaceship spaceship = new Spaceship("Шустрый", 100, 25);
Player player = new Player(spaceship);
player.AddCargo(new CargoItem("Детали", 4, 16));
player.AddCargo(new CargoItem("Метал", 2, 40));
player.AddCargo(new CargoItem("Молоко", 16, 3));
player.ShowInfo();
for (int i = 0; i <= 3; i++)
{
DamageReport damageReport = CombatCalculator.CalculateDamage(player.Spaceship.Speed, rand);
damageReport.PrintReport();
if (damageReport.DamageDealt > 0)
{
player.TakeDamage(damageReport.DamageDealt);
}
if (player.Spaceship.Health <= 0)
{
Console.WriteLine("Корабль уничтожен!");
break;
}
}
player.ShowInfo();
}
}
public static class CombatCalculator
{
private const double CriticalHitChance = 0.3;
public static DamageReport CalculateDamage(float shipSpeed, Random rnd)
{
int baseDamage = 10 + (int)shipSpeed;
bool isCriticalHit = false;
if (rnd.NextDouble() <= CriticalHitChance)
{
baseDamage *= 2;
isCriticalHit = true;
}
return new DamageReport(baseDamage, isCriticalHit);
}
}
public struct Spaceship
{
public string ShipName { get; set; }
public int Health { get; set; }
public int MaxHealth { get; set; }
public float Speed { get; set; }
public Spaceship(string shipName, int maxHealth, float speed)
{
ShipName = shipName;
MaxHealth = Math.Max(maxHealth, 1);
Health = MaxHealth;
Speed = speed;
}
}
public struct CargoItem
{
private string itemName;
private float _pricePerUnit;
public int Quantity { get; set; }
public string ItemName => itemName;
public float PricePerUnit => _pricePerUnit;
public CargoItem(string name, int quantity, float pricePerUnit)
{
itemName = name;
Quantity = quantity;
_pricePerUnit = pricePerUnit;
}
}
public class Player
{
private Spaceship _spaceShip;
private List<CargoItem> _cargoItems;
public Spaceship Spaceship => _spaceShip;
public List<CargoItem> CargoItems => _cargoItems;
public Player(Spaceship spaceship)
{
_spaceShip = spaceship;
_cargoItems = new List<CargoItem>();
}
public void TakeDamage(int damage)
{
_spaceShip.Health = Math.Max(0, _spaceShip.Health - damage);
}
public void AddCargo(CargoItem newCargo)
{
int index = _cargoItems.FindIndex(item => item.ItemName == newCargo.ItemName);
if (index != -1)
{
CargoItem existingItem = _cargoItems[index];
existingItem.Quantity += newCargo.Quantity;
_cargoItems[index] = existingItem;
}
else
{
_cargoItems.Add(newCargo);
}
}
public void ShowInfo()
{
Console.WriteLine($"У корабля {_spaceShip.ShipName} здоровье: {_spaceShip.Health}/{_spaceShip.MaxHealth}");
Console.WriteLine($"На корабле {_spaceShip.ShipName} {_cargoItems.Count} видов груза:");
foreach (CargoItem item in _cargoItems)
{
Console.WriteLine($"{item.ItemName}: Кол-во: {item.Quantity}. Цена за шт: {item.PricePerUnit}");
}
}
}
public struct DamageReport
{
public int DamageDealt;
public bool IsCriticalHit;
public DamageReport(int damageDealt, bool isCriticalHit)
{
DamageDealt = damageDealt;
IsCriticalHit = isCriticalHit;
}
public void PrintReport()
{
if (IsCriticalHit)
Console.ForegroundColor = ConsoleColor.Green;
else
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Нанесено {DamageDealt} урона.");
Console.ForegroundColor = ConsoleColor.White;
}
}
}