-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
121 lines (101 loc) · 4.3 KB
/
Program.cs
File metadata and controls
121 lines (101 loc) · 4.3 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
using System;
namespace RefAndOut
{
internal class Program
{
static Random rand = new Random();
static void Main(string[] args)
{
CharacterStats playerStats = new CharacterStats(250);
Console.WriteLine("=== Исходные характеристики ===");
playerStats.PrintStats();
Console.WriteLine("\n=== Применяем зелье силы ===");
ApplyPotion(ref playerStats, "strength", 23);
playerStats.PrintStats();
Console.WriteLine("\n=== Пробуем проклятие интеллекта ===");
if(TryApplyCurse(playerStats, "intelligence", 13, out CharacterStats cursedStats))
{
Console.WriteLine("Проклятие подействовало!");
playerStats = cursedStats;
}
else
{
Console.WriteLine("Проклятие не подействовало.");
}
playerStats.PrintStats();
Console.WriteLine("\n=== Меняем местами здоровье и интеллект ===");
SwapStats(ref playerStats, "health", "intelligence");
playerStats.PrintStats();
}
static void ApplyPotion(ref CharacterStats stats, string statName, int power)
{
ref int stat = ref FindStat(ref stats, statName);
stat += power;
Console.WriteLine($"Параметр {statName} изменен на {power}. Теперь: {stat}");
}
static bool TryApplyCurse(CharacterStats baseStats, string statName, int power, out CharacterStats cursedStats)
{
cursedStats = baseStats;
if (rand.Next(0,2) == 0 && baseStats.Health < 20)
{
return false;
}
else
{
ref int stat = ref FindStat(ref cursedStats, statName);
stat -= power;
Console.WriteLine($"Параметр {statName} изменен на {power}. Теперь: {stat}");
return true;
}
}
static void SwapStats(ref CharacterStats stats, string firstStatName, string secondStatName)
{
ref int firstStat = ref FindStat(ref stats, firstStatName);
ref int secondStat = ref FindStat(ref stats, secondStatName);
(firstStat, secondStat) = (secondStat, firstStat);
Console.WriteLine($"Параметры {firstStatName} и {secondStatName} поменялись местами. Теперь {firstStatName}: {firstStat}, {secondStatName}: {secondStat}");
}
static ref int FindStat(ref CharacterStats stats, string statName)
{
switch (statName.ToLower())
{
case "health":
return ref stats.Health;
case "mana":
return ref stats.Mana;
case "strength":
return ref stats.Strength;
case "agility":
return ref stats.Agility;
case "intelligence":
return ref stats.Intelligence;
default:
throw new ArgumentException($"Неизвестная характеристика: {statName}");
}
}
}
struct CharacterStats
{
public int Health;
public int Mana;
public int Strength;
public int Agility;
public int Intelligence;
public CharacterStats(int health = 100, int mana = 50, int strength = 10, int agility = 10, int intelligence = 10)
{
Health = health;
Mana = mana;
Strength = strength;
Agility = agility;
Intelligence = intelligence;
}
public void PrintStats()
{
Console.WriteLine($"Здоровье - {Health}");
Console.WriteLine($"Мана - {Mana}");
Console.WriteLine($"Сила - {Strength}");
Console.WriteLine($"Ловкость - {Agility}");
Console.WriteLine($"Интеллект - {Intelligence}");
}
}
}