-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
176 lines (154 loc) · 5.65 KB
/
Game.cs
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
using System;
using System.IO;
using System.Windows.Forms;
namespace Battle
{
public partial class Game : Form
{
Hero playerHero;
Hero compHero;
private string[] doings =
{
"Бежит",
"Защищается",
"Атакует"
};
public Game(Hero playerHero, Hero compHero)
{
this.playerHero = playerHero;
this.compHero = compHero;
InitializeComponent();
UpdateLabels();
}
private void UpdateLabels()
{
player_health.Text = $@"Player health: {playerHero.currentHealth}";
comp_health.Text = $@"Comp health: {compHero.currentHealth}";
}
private void run_Click(object sender, EventArgs e)
{
var move = new Random().Next(3);
var isSecondRun = move == 0;
if (move == 1)
{
playerHero.Run(true);
player_move.Text = $@"{playerHero.name} убегает";
comp_move.Text = $@"{compHero.name} защищается";
return;
}
if (isSecondRun)
{
compHero.Run(true);
playerHero.Run(true);
player_move.Text = $@"{playerHero.name} убегает";
comp_move.Text = $@"{compHero.name} убегает";
}
else
{
if (playerHero.currentHealth < 2)
{
MessageBox.Show("Вы не можете бежать", " ", MessageBoxButtons.OK);
return;
}
playerHero.Run(false);
player_move.Text = $@"{playerHero.name} убегает";
comp_move.Text = $@"{compHero.name} атакует";
}
UpdateLabels();
CheckEndGame();
}
private void CheckEndGame()
{
var plH = playerHero.currentHealth;
var cH = compHero.currentHealth;
if (plH <= 0 && cH <= 0)
{
MessageBox.Show("Ничья! Дуэлянты прикончили друг друга", "", MessageBoxButtons.OK);
Application.OpenForms[0].Show();
Close();
return;
}
if (plH <= 0)
{
MessageBox.Show("Вы проиграли!", " ", MessageBoxButtons.OK);
Application.OpenForms[0].Show();
Close();
return;
}
if (cH <= 0)
{
MessageBox.Show("Вы выиграли!", " ", MessageBoxButtons.OK);
Application.OpenForms[0].Show();
Close();
return;
}
}
private void defend_Click(object sender, EventArgs e)
{
var move = new Random().Next(3);
if (move == 1)
{
player_move.Text = $@"{playerHero.name} защищается";
comp_move.Text = $@"{compHero.name} защищается";
return;
}
if (move == 0)
{
compHero.Run(true);
player_move.Text = $@"{playerHero.name} защищается";
comp_move.Text = $@"{compHero.name} убегает";
return;
}
compHero.Attack(playerHero);
player_move.Text = $@"{playerHero.name} защищается";
comp_move.Text = $@"{compHero.name} атакует";
UpdateLabels();
CheckEndGame();
}
private void attack_Click(object sender, EventArgs e)
{
var move = new Random().Next(3);
if(compHero.currentHealth < 2) move = new Random().Next(1, 3);
if (move == 0)
{
compHero.Run(false);
playerHero.Attack(compHero);
player_move.Text = $@"{playerHero.name} атакует";
comp_move.Text = $@"{compHero.name} убегает";
}
if (move == 1)
{
playerHero.Attack(compHero);
player_move.Text = $@"{playerHero.name} атакует";
comp_move.Text = $@"{compHero.name} защищается";
}
if (move == 2)
{
var i = 0;
if(Math.Abs(playerHero.currentHealth - compHero.currentHealth) < 0.1) i = new Random().Next(-1, 2);
if (playerHero.currentHealth + i > compHero.currentHealth)
{
playerHero.Attack(compHero);
player_move.Text = $@"{playerHero.name} атакует";
comp_move.Text = $@"{compHero.name} защищается, так как у него меньше очков";
}
else
{
compHero.Attack(playerHero);
player_move.Text = $@"{playerHero.name} защищается так как у него меньше очков";
comp_move.Text = $@"{compHero.name} атакует";
}
}
UpdateLabels();
CheckEndGame();
}
private void saveGame_Click(object sender, EventArgs e)
{
var writer = new StreamWriter(@"../save.txt");
writer.WriteLine(playerHero.ToString());
writer.WriteLine(compHero.ToString());
writer.Close();
Close();
}
}
}