-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
114 lines (103 loc) · 4.09 KB
/
Copy pathProgram.cs
File metadata and controls
114 lines (103 loc) · 4.09 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
using System;
namespace Game
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Игра Перестрелка ");
Tank player = new Tank("Танк игрока", 5, 100, 25); // танк игрока
Tank ai = new Tank("Танк противника", 5, 100, 25); // танк компьютера
int i = 1;
string key;
int amount = 10; // величина починки
key = Console.ReadLine();
Random rnd = new Random();
while (player.GetHealth() > 0 && ai.GetHealth() > 0) // условие победы
{
Console.WriteLine($"Раунд {i}"); // вывод номера раунда
{
do // начало валидации пользовательского ввода
{
player.PrintInfo();
ai.PrintInfo();
Console.WriteLine();
Console.WriteLine("Выберите действие");
Console.WriteLine("1. Выстрелить");
Console.WriteLine("2. Починка");
Console.WriteLine();
key = Console.ReadLine();
if (key != "1" && key != "2")
{
Console.WriteLine("Неизвестное действие, введите номер действия из списка");
}
}
while (key != "1" && key != "2"); // конец пользовательского ввода
}
if (key == "1")
{
player.Shoot(ref ai);
}
else if (key == "2")
{
player.Repair(amount);
}
if (ai.GetHealth() <= 0)
{
break;
}
int aim = rnd.Next(1, 3);
if (aim == 1)
{
ai.Shoot(ref player);
Console.WriteLine("Противник стреляет");
}
else if (aim == 2)
{
ai.Repair(amount);
Console.WriteLine("Противник чинится");
}
i++;
}
if (player.GetHealth() > 0 || ai.GetHealth() <= 0)
{
Console.WriteLine("Игрок победил");
}
else if (player.GetHealth() <= 0 || ai.GetHealth() > 0)
{
Console.WriteLine("Противник победил");
}
Console.ReadKey();
}
}
class Tank // объявление класса Танк
{
int armor; // броня танка
int health; // хп танка
int damage; // урон танка
string name; //имя танка
public Tank(string n, int a, int h, int d) // конструкторы класса Танк
{
name = n;
armor = a;
health = h;
damage = d;
}
public void PrintInfo() // вывод информации о танке
{
Console.WriteLine($" {name} | Броня: {armor} Жизни: {health} Урон: {damage}");
}
public void Shoot(ref Tank enemy) // метод стрельбы
{
enemy.health = enemy.health - damage + enemy.armor;
}
public void Repair(int amount) // метод починки
{
health = health + amount;
}
public int GetHealth() // метод получения текущего здоровья для условия окончания цикла
{
return health;
}
}
}