-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
348 lines (323 loc) · 12.4 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace Bacterium
{
public class Game
{
public int FreeCellsAmount
{
get
{
int am = 0;
foreach (var cl in _cells)
foreach (Cell c in cl)
{
if (c.Enabled)
am++;
}
return am;
}
}
public int LightCount
{
get
{
return _lightList.Count;
}
}
public int DarkCount
{
get
{
return _darkList.Count;
}
}
private List<MyPoint> _lightList;
private List<MyPoint> _darkList;
private List<List<Cell>> _cells;
private Canvas _grid;
public bool IsLightTurn { get; set; }
public Game(Canvas grid)
{
_lightList = new List<MyPoint>();
_darkList = new List<MyPoint>();
_cells = new List<List<Cell>>();
this._grid = grid;
InitializeCells();
}
public bool IsLocked
{
get
{
int i, j;
for (int ii = 0; ii < (IsLightTurn ? _lightList.Count : _darkList.Count); ii++)
{
i = IsLightTurn ? _lightList[ii].I : _darkList[ii].I;
j = IsLightTurn ? _lightList[ii].J : _darkList[ii].J;
if (i - 1 >= 0)
{
if (_cells[i - 1][j].Enabled) return false;
if (j - 1 >= 0) if (_cells[i - 1][j - 1].Enabled) return false;
}
if (i - 2 >= 0)
{
if (_cells[i - 2][j].Enabled) return false;
if (j - 2 >= 0) if (_cells[i - 2][j - 2].Enabled) return false;
}
if (i + 1 < _cells.Count)
{
if (_cells[i + 1][j].Enabled) return false;
if (j + 1 < _cells[i + 1].Count) if (_cells[i + 1][j + 1].Enabled) return false;
}
if (i + 2 < _cells.Count)
{
if (_cells[i + 2][j].Enabled) return false;
if (j + 2 < _cells[i + 2].Count) if (_cells[i + 2][j + 2].Enabled) return false;
}
if (j + 1 < _cells[i].Count) if (_cells[i][j + 1].Enabled) return false;
if (j + 2 < _cells[i].Count) if (_cells[i][j + 2].Enabled) return false;
if (j - 1 >= 0) if (_cells[i][j - 1].Enabled) return false;
if (j - 2 >= 0) if (_cells[i][j - 2].Enabled) return false;
}
return true;
}
}
public void Save()
{
FileStream fs = new FileStream("save.bin", FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
w.Write(IsLightTurn);
w.Write((Int32)_lightList.Count);
for (int i = 0; i < _lightList.Count; i++)
{
w.Write((Int32)_lightList[i].I);
w.Write((Int32)_lightList[i].J);
}
for (int i = 0; i < _darkList.Count; i++)
{
w.Write((Int32)_darkList[i].I);
w.Write((Int32)_darkList[i].J);
}
fs.Close();
}
public void Restart()
{
this._lightList.Clear();
this._darkList.Clear();
this._grid.Children.Clear();
this._cells.Clear();
this.InitializeCells();
this.Start();
}
public void Load()
{
try
{
this._grid.Children.Clear();
_lightList.Clear();
_darkList.Clear();
_cells.Clear();
InitializeCells();
FileStream fs = new FileStream("save.bin", FileMode.Open);
BinaryReader r = new BinaryReader(fs);
int i, j, lcount;
MyPoint t;
IsLightTurn = r.ReadBoolean();
lcount = r.ReadInt32();
for (int ii = 0; ii < lcount; ii++)
{
i = r.ReadInt32(); j = r.ReadInt32();
t = new MyPoint(_cells[i][j], MyPoint.LIGHT, i, j);
this._grid.Children.Add(t.CurrentImage);
_lightList.Add(t);
}
while (r.BaseStream.Position < r.BaseStream.Length)
{
i = r.ReadInt32(); j = r.ReadInt32();
t = new MyPoint(_cells[i][j], MyPoint.DARK, i, j);
this._grid.Children.Add(t.CurrentImage);
_darkList.Add(t);
}
fs.Close();
}
catch (Exception)
{
throw;
}
}
public void InitializeCells()
{
//const int StartX = -370, StartY = -220, ColumnStepX = 92, ColumnStepY = 53, RowStepY = 108;
const int StartX = 79, StartY = 119, ColumnStepX = 46, ColumnStepY = 27, RowStepY = 54;
int RowStartY, i, j;
for (i = 0; i < 9; i++)
{
RowStartY = StartY + RowStepY * i;
List<Cell> CellList = new List<Cell>();
for (j = 0; j < 9; j++)
CellList.Add(new Cell(StartX + ColumnStepX * j, RowStartY - ColumnStepY * j, true));
_cells.Add(CellList);
}
int max = 0;
for (i = 5; i < 9; i++, max++)
for (j = 0; j <= max; j++)
_cells[i][j].Enabled = false;
max = 5;
for (i = 0; i < 4; i++, max++)
for (j = max; j < 9; j++)
_cells[i][j].Enabled = false;
}
public void Start()
{
MyPoint t;
t = new MyPoint(_cells[0][0], MyPoint.LIGHT, 0, 0);
this._grid.Children.Add(t.CurrentImage);
_lightList.Add(t);
t = new MyPoint(_cells[4][8], MyPoint.LIGHT, 4, 8);
this._grid.Children.Add(t.CurrentImage);
_lightList.Add(t);
t = new MyPoint(_cells[8][4], MyPoint.LIGHT, 8, 4);
this._grid.Children.Add(t.CurrentImage);
_lightList.Add(t);
t = new MyPoint(_cells[0][4], MyPoint.DARK, 0, 4);
this._grid.Children.Add(t.CurrentImage);
_darkList.Add(t);
t = new MyPoint(_cells[4][0], MyPoint.DARK, 4, 0);
this._grid.Children.Add(t.CurrentImage);
_darkList.Add(t);
t = new MyPoint(_cells[8][8], MyPoint.DARK, 8, 8);
this._grid.Children.Add(t.CurrentImage);
_darkList.Add(t);
this.IsLightTurn = true;
/*for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
{
t = new MyPoint(_cells[i][j], MyPoint.LIGHT, i, j);
if (t.CurrentImage != null)
this._grid.Children.Add(t.CurrentImage);
}*/
}
public MyPoint Find(int side, int x, int y)
{
MyPoint closest = null;
if (side == MyPoint.LIGHT)
for (int i = 0; i < _lightList.Count; i++)
{
if (x - _lightList[i].X >= 0 && x - _lightList[i].X <= MyPoint.WIDTH &&
y - _lightList[i].Y >= 0 && y - _lightList[i].Y <= MyPoint.HEIGHT)
{ closest = _lightList[i]; break; }
}
else if (side == MyPoint.DARK)
for (int i = 0; i < _darkList.Count; i++)
{
if (x - _darkList[i].X >= 0 && x - _darkList[i].X <= MyPoint.WIDTH &&
y - _darkList[i].Y >= 0 && y - _darkList[i].Y <= MyPoint.HEIGHT)
{ closest = _darkList[i]; break; }
}
return closest;
}
public void Clone(Cell c, int side, int i, int j)
{
MyPoint t = new MyPoint(c, side, i, j);
this._grid.Children.Add(t.CurrentImage);
if (t.Side == MyPoint.LIGHT) _lightList.Add(t);
else _darkList.Add(t);
IsLightTurn = !IsLightTurn;
Expansion(t);
// MessageBox.Show(String.Format("Game over! The winner is: {0}", _lightList.Count == 0 ? "Green team" : "Red team"));
}
private void Jump(MyPoint p, Cell c, int i, int j)
{
p.SetNewLocation(c.X, c.Y);
p.I = i;
p.J = j;
c.Enabled = false;
p.CurrentCell.Enabled = true;
p.CurrentCell = c;
IsLightTurn = !IsLightTurn;
Expansion(p);
// MessageBox.Show(String.Format("Game over! The winner is: {0}", _lightList.Count == 0 ? "Green team" : "Red team"));
}
public int ChangeList(MyPoint p, int i)
{
if (p.Side == MyPoint.DARK)
{
p.Side = MyPoint.LIGHT;
_lightList.Add(_darkList[i]);
_darkList.RemoveAt(i);
}
else
{
p.Side = MyPoint.DARK;
_darkList.Add(_lightList[i]);
_lightList.RemoveAt(i);
}
if (_lightList.Count == 0 || _darkList.Count == 0) return -1;
return 0;
}
public int Expansion(MyPoint p)
{
if (p.Side == MyPoint.LIGHT)
{
for (int i = 0; i < _darkList.Count; i++)
{
if ((_darkList[i].I == p.I - 1 && (_darkList[i].J == p.J || _darkList[i].J == p.J - 1)) ||
(_darkList[i].I == p.I && (_darkList[i].J == p.J - 1 || _darkList[i].J == p.J + 1)) ||
(_darkList[i].I == p.I + 1 && (_darkList[i].J == p.J || _darkList[i].J == p.J + 1)))
{
if (ChangeList(_darkList[i], i) < 0)
return -1;
else i--;
}
}
}
else
for (int i = 0; i < _lightList.Count; i++)
{
if ((_lightList[i].I == p.I - 1 && (_lightList[i].J == p.J || _lightList[i].J == p.J - 1)) ||
(_lightList[i].I == p.I && (_lightList[i].J == p.J - 1 || _lightList[i].J == p.J + 1)) ||
(_lightList[i].I == p.I + 1 && (_lightList[i].J == p.J || _lightList[i].J == p.J + 1)))
{
if (ChangeList(_lightList[i], i) < 0)
return -1;
else i--;
}
}
return 0;
}
public bool JumpToCell(int x, int y, MyPoint p)
{
for (int i = 0; i < _cells.Count; i++)
{
for (int j = 0; j < _cells[i].Count; j++)
{
if (x - _cells[i][j].X >= 0 && x - _cells[i][j].X <= MyPoint.WIDTH &&
y - _cells[i][j].Y >= 0 && y - _cells[i][j].Y <= MyPoint.HEIGHT)
{
if (!_cells[i][j].Enabled || (i == p.I && j == p.J)) return false;
if ((i == p.I - 2 && (j == p.J || j == p.J - 2)) ||
(i == p.I && (j == p.J - 2 || j == p.J + 2)) ||
(i == p.I + 2 && (j == p.J || j == p.J + 2)))
{ Jump(p, _cells[i][j], i, j); return true; }
if ((i == p.I - 1 && (j == p.J || j == p.J - 1)) ||
(i == p.I && (j == p.J - 1 || j == p.J + 1)) ||
(i == p.I + 1 && (j == p.J || j == p.J + 1)))
Clone(_cells[i][j], p.Side, i, j);
return false;
}
}
}
return false;
}
public static void Message()
{
MessageBox.Show("A MOST IMPORTANT MESSAGE");
}
}
}