-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameField.cs
More file actions
294 lines (268 loc) · 10.8 KB
/
GameField.cs
File metadata and controls
294 lines (268 loc) · 10.8 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace pentago
{
public partial class GameField : Form
{
public Guid Id { get; private set; }
public static int SectorCount;
public const int MenuWidth = 300;
private static int FieldStep = Sector.Gap;
private static int FieldLeft = 2 * Sector.StartTop + MenuWidth; //100 - ширина меню
private static int FieldTop = 3 * Sector.StartTop;
static public Dictionary<Guid, GameField> Items = new Dictionary<Guid, GameField>();
private Guid _GameId;
public StartForm Start { get; private set; }
private bool IsBallNotSector;
private static readonly int WinScore = 5;
public Game Game
{
get { return Game.Items[_GameId]; }
set { _GameId = value.Id; }
}
public GameField()
{
InitializeComponent();
Id = Guid.NewGuid();
Items.Add(Id, this);
Start = new StartForm(this);
Start.ShowDialog();
if (Start.IsFinish)
return;
SetNames(Start);
SetScore();
ShowPlayers(Start);
lblActivePlayer.Text = Player.GetByActiveIndex(Game.ActivePlayer) + "!";
Sector sector;
foreach (var sec in Game.SectorMatrix)
{
sector = new Sector(Game, sec);
Controls.Add(sector);
sector.MouseClick += OnSectorMouseClick;
foreach (var ball in Sector.BallMatrix)
{
BallPoint bp = new BallPoint(sector, ball);
bp.Click += OnBallPointClick;
sector.Controls.Add(bp);
}
}
IsBallNotSector = true;
MinimumSize = new Size(SectorCount * FieldStep + FieldLeft, SectorCount * FieldStep + FieldTop);
}
private void OnBallPointClick(object sender, EventArgs e)
{
BallPoint bp = (BallPoint)sender;
if (bp.IsColored || !IsBallNotSector)
return;
bp.PointColor = Player.GetByActiveIndex(bp.Sector.Game.ActivePlayer).PlayerColor;
bp.DrawBallPoint();
bp.IsColored = true;
string s = bp.OpenCoord.X.ToString() + " " + bp.OpenCoord.Y.ToString();//избыточная переменная
//MessageBox.Show(s);
lblHint.Text = "Поверните сектор";
IsBallNotSector = false;
}
private void OnSectorMouseClick(object sender, MouseEventArgs e)
{
if (IsBallNotSector)
return;
Sector sv = (Sector)sender;
int active = sv.Game.ActivePlayer;
if (e.Button == MouseButtons.Left)
sv.TurnLeft();
if (e.Button == MouseButtons.Right)
sv.TurnRight();
int result = Game.CheckForScore();
CangeScore();
ShowScore();
if (result >= WinScore)
SomeoneWins(result);
if (Game.CheckForWhiteBalls())
GameOver();
IsBallNotSector = true;
sv.Game.ActivePlayer = active == sv.Game.PlayerCount ? 1 : active + 1;
active = sv.Game.ActivePlayer;
lblActivePlayer.Text = Player.GetByActiveIndex(active) + "!";
lblHint.Text = "Выберите шар";
string s = sv.Game.Players[0].MaxHLine.ToString() + " " + sv.Game.Players[0].MaxVLine.ToString() + " " + sv.Game.Players[0].MaxDLLine.ToString() + " " + sv.Game.Players[0].MaxDRLine.ToString();
//MessageBox.Show(s);
}
private void SomeoneWins(int result)
{
string s = "Следующие игроки победили со счетом " + result.ToString() + " очков:";
List<Player> CurrentWinners = Game.Winners(result);
foreach (var winner in CurrentWinners)
s += "\n" + winner.ToString();
MessageBox.Show(s);
Application.Exit();
}
private void GameOver()
{
MessageBox.Show("Конец игры");
Application.Exit();
}
private void ShowScore()
{
switch (Game.PlayerCount)
{
case 8:
lblScore8.Text = Player.GetByActiveIndex(8).Score.ToString();
goto case 7;
case 7:
lblScore7.Text = Player.GetByActiveIndex(7).Score.ToString();
goto case 6;
case 6:
lblScore6.Text = Player.GetByActiveIndex(6).Score.ToString();
goto case 5;
case 5:
lblScore5.Text = Player.GetByActiveIndex(5).Score.ToString();
goto case 4;
case 4:
lblScore4.Text = Player.GetByActiveIndex(4).Score.ToString();
goto case 3;
case 3:
lblScore3.Text = Player.GetByActiveIndex(3).Score.ToString();
goto default;
default:
lblScore2.Text = Player.GetByActiveIndex(2).Score.ToString();
lblScore1.Text = Player.GetByActiveIndex(1).Score.ToString();
break;
}
}
private void CangeScore()
{
switch (Game.PlayerCount)
{
case 8:
Player.GetByActiveIndex(8).Score = Player.GetByActiveIndex(8).MaxLine;
goto case 7;
case 7:
Player.GetByActiveIndex(7).Score = Player.GetByActiveIndex(7).MaxLine;
goto case 6;
case 6:
Player.GetByActiveIndex(6).Score = Player.GetByActiveIndex(6).MaxLine;
goto case 5;
case 5:
Player.GetByActiveIndex(5).Score = Player.GetByActiveIndex(5).MaxLine;
goto case 4;
case 4:
Player.GetByActiveIndex(4).Score = Player.GetByActiveIndex(4).MaxLine;
goto case 3;
case 3:
Player.GetByActiveIndex(3).Score = Player.GetByActiveIndex(3).MaxLine;
goto default;
default:
Player.GetByActiveIndex(2).Score = Player.GetByActiveIndex(2).MaxLine;
Player.GetByActiveIndex(1).Score = Player.GetByActiveIndex(1).MaxLine;
break;
}
}
private void SetScore()
{
switch (Game.PlayerCount)
{
case 8:
lblScore8.Text = Player.GetByActiveIndex(8).Score.ToString();
goto case 7;
case 7:
lblScore7.Text = Player.GetByActiveIndex(7).Score.ToString();
goto case 6;
case 6:
lblScore6.Text = Player.GetByActiveIndex(6).Score.ToString();
goto case 5;
case 5:
lblScore5.Text = Player.GetByActiveIndex(5).Score.ToString();
goto case 4;
case 4:
lblScore4.Text = Player.GetByActiveIndex(4).Score.ToString();
goto case 3;
case 3:
lblScore3.Text = Player.GetByActiveIndex(3).Score.ToString();
goto default;
default:
lblScore1.Text = Player.GetByActiveIndex(1).Score.ToString();
lblScore2.Text = Player.GetByActiveIndex(2).Score.ToString();
break;
}
}
private void SetNames(StartForm start)
{
lblName1.Text = start.Player1Name;
lblName2.Text = start.Player2Name;
lblName3.Text = start.Player3Name;
lblName4.Text = start.Player4Name;
lblName5.Text = start.Player5Name;
lblName6.Text = start.Player6Name;
lblName7.Text = start.Player7Name;
lblName8.Text = start.Player8Name;
}
private void ShowPlayers(StartForm start)
{
switch (start.PlayerCount)
{
case 3:
pnlPlayers3.Visible = true;
pnlPlayers4.Visible = false;
pnlPlayers5.Visible = false;
pnlPlayers6.Visible = false;
pnlPlayers7.Visible = false;
pnlPlayers8.Visible = false;
break;
case 4:
pnlPlayers3.Visible = true;
pnlPlayers4.Visible = true;
pnlPlayers5.Visible = false;
pnlPlayers6.Visible = false;
pnlPlayers7.Visible = false;
pnlPlayers8.Visible = false;
break;
case 5:
pnlPlayers3.Visible = true;
pnlPlayers4.Visible = true;
pnlPlayers5.Visible = true;
pnlPlayers6.Visible = false;
pnlPlayers7.Visible = false;
pnlPlayers8.Visible = false;
break;
case 6:
pnlPlayers3.Visible = true;
pnlPlayers4.Visible = true;
pnlPlayers5.Visible = true;
pnlPlayers6.Visible = true;
pnlPlayers7.Visible = false;
pnlPlayers8.Visible = false;
break;
case 7:
pnlPlayers3.Visible = true;
pnlPlayers4.Visible = true;
pnlPlayers5.Visible = true;
pnlPlayers6.Visible = true;
pnlPlayers7.Visible = true;
pnlPlayers8.Visible = false;
break;
case 8:
pnlPlayers3.Visible = true;
pnlPlayers4.Visible = true;
pnlPlayers5.Visible = true;
pnlPlayers6.Visible = true;
pnlPlayers7.Visible = true;
pnlPlayers8.Visible = true;
break;
default: //2
pnlPlayers3.Visible = false;
pnlPlayers4.Visible = false;
pnlPlayers5.Visible = false;
pnlPlayers6.Visible = false;
pnlPlayers7.Visible = false;
pnlPlayers8.Visible = false;
break;
}
}
}
}