-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameManger.cs
328 lines (291 loc) · 11.6 KB
/
GameManger.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MSNMineSweeper
{
public enum MineFieldCellStatus { Covered, Opened };
public enum CurrentPlayer { Red, Blue };
//public enum CellImageStyle { Covered, Opened };
static class GameManger
{
public static MineFiledCell[,] MineFiledCells = new MineFiledCell[16, 16]; //[Row, Column]
public static GameWindow myGameWindow;
static Dictionary<String, Image> CellImageRef = new Dictionary<string, Image>();
public static Player PlayerRed = new Player(), PlayerBlue = new Player();
public static CurrentPlayer CurrentPlayer = MSNMineSweeper.CurrentPlayer.Red;
static GameManger()
{
myGameWindow = new GameWindow();
for (int i = 0; i < 16 * 16; i++)
{
Int32 Row = i / 16, Column = i % 16;
MineFiledCells[Row, Column] = new MineFiledCell()
{
};
}
//Prepare mine cell image
foreach (String ImageName in new String[] { "0", "1", "2", "3", "4", "5", "Covered", "Flag_Blue", "Flag_Red" })
{
BitmapImage bi4 = new BitmapImage();
bi4.BeginInit();
bi4.UriSource = new Uri("img/Grid/" + ImageName + ".png", UriKind.Relative);
bi4.EndInit();
Image myImage3 = new Image();
myImage3.Source = bi4;
CellImageRef.Add(ImageName, myImage3);
}
NewGameSetup();
//Add CellImage
for (int i = 0; i < 16 * 16; i++)
{
myGameWindow.MineBoard.Children.Add(MineFiledCells[i / 16, i % 16].CellImage);
}
//Add BlankImage
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("img/Grid/Transparent.png", UriKind.Relative);
bi3.EndInit();
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
Image myImage3 = new Image();
myImage3.Source = bi3;
myImage3.SetValue(Grid.RowProperty, i);
myImage3.SetValue(Grid.ColumnProperty, j);
myGameWindow.MouseDetection.Children.Add(myImage3);
}
}
}
public static void NewGameSetup()
{
//Set mine list
Boolean[] MineList = new Boolean[16 * 16];
for (int i = 0; i < 51; i++)
{
MineList[i] = true;
}
Shuffle<Boolean>(MineList);
//Prepare cells based on the MineList
for (int i = 0; i < 16 * 16; i++)
{
Int32 Row = i / 16, Column = i % 16;
MineFiledCells[Row, Column].isMine = MineList[i];
MineFiledCells[Row, Column].isOpened = false;
MineFiledCells[Row, Column].MineAroundCount = 0;
//Reset CellImage
MineFiledCells[Row, Column].CellImage.Source = GetCellImageByKey("Covered").Source;
MineFiledCells[Row, Column].SetCellImageGrid(Row, Column);
}
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
if (MineFiledCells[i, j].isMine)
{
IncreaseMineAroundCount(i, j);
}
}
}
//Reset Player infor
PlayerRed.MineFound = PlayerBlue.MineFound = 0; ;
myGameWindow.MineGot_Red.Text = myGameWindow.MineGot_Blue.Text = "00";
myGameWindow.RemainMineNo.Content = 51;
myGameWindow.GameResult.Visibility = System.Windows.Visibility.Hidden;
myGameWindow.ChangePlayerNotice.Visibility = System.Windows.Visibility.Hidden;
CurrentPlayer = MSNMineSweeper.CurrentPlayer.Red;
myGameWindow.Arrow_Blue.Visibility = System.Windows.Visibility.Visible;
myGameWindow.Arrow_Blue.Visibility = System.Windows.Visibility.Hidden;
PlaySound("NewGame");
}
static void PlaySound(String SoundName)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer("sound/" + SoundName + ".wav");
player.Play();
}
static void IncreaseMineAroundCount(Int32 Row, Int32 Column)
{
for (int RowOffset = -1; RowOffset <= 1; RowOffset++)
{
for (int ColumnOffset = -1; ColumnOffset <= 1; ColumnOffset++)
{
Int32 Row_A = Row + RowOffset, Column_A = Column + ColumnOffset;
if (Row_A >= 0 && Row_A <= 15 && Column_A >= 0 && Column_A <= 15 && (RowOffset != 0 || ColumnOffset != 0))
{
MineFiledCells[Row_A, Column_A].MineAroundCount++;
}
}
}
}
static Image GetCellImageByKey(String key)
{
return new Image() { Source = CellImageRef[key].Source };
}
static Random _random = new Random();
static void Shuffle<T>(T[] array)
{
var random = _random;
for (int i = array.Length; i > 1; i--)
{
// Pick random element to swap.
int j = random.Next(i); // 0 <= j <= i-1
// Swap.
T tmp = array[j];
array[j] = array[i - 1];
array[i - 1] = tmp;
}
}
//Process evenet
public static void CellClick(Int32 Row, Int32 Column)
{
MineFiledCell TargetCell = MineFiledCells[Row, Column];
if (!TargetCell.isOpened)
{
if (TargetCell.MineAroundCount == 0 && !TargetCell.isMine)
{
Expand(Row, Column);
}
else
{
SetCellOpen(TargetCell);
}
if (TargetCell.isMine)
{
if ((CurrentPlayer == MSNMineSweeper.CurrentPlayer.Red && PlayerRed.MineFound >= 20)
||
(CurrentPlayer == MSNMineSweeper.CurrentPlayer.Blue && PlayerBlue.MineFound >= 20)
)
PlaySound("CloseToWin");
else
PlaySound("FoundMine");
CheckWinner();
}
else
{
PlaySound("FoundEmpty");
ChangePlayer();
}
}
}
private static void CheckWinner()
{
if (PlayerRed.MineFound == 26)
{
myGameWindow.GameResultText.Content = PlayerRed.PlayerName + " is the winner!";
PlaySound("Win");
myGameWindow.GameResult.Visibility = System.Windows.Visibility.Visible;
}
else if (PlayerBlue.MineFound == 26)
{
myGameWindow.GameResultText.Content = PlayerBlue.PlayerName + " is the winner!";
PlaySound("Win");
myGameWindow.GameResult.Visibility = System.Windows.Visibility.Visible;
}
}
private static void Expand(Int32 Row, Int32 Column)
{
MineFiledCell TargetCell = MineFiledCells[Row, Column];
//Open it first
SetCellOpen(TargetCell);
if (TargetCell.MineAroundCount == 0)
{
for (int RowOffset = -1; RowOffset <= 1; RowOffset++)
{
for (int ColumnOffset = -1; ColumnOffset <= 1; ColumnOffset++)
{
Int32 Row_A = Row + RowOffset, Column_A = Column + ColumnOffset;
if (Row_A >= 0 && Row_A <= 15
&& Column_A >= 0 && Column_A <= 15
&& (RowOffset != 0 || ColumnOffset != 0)
&& !MineFiledCells[Row_A, Column_A].isOpened && !MineFiledCells[Row_A, Column_A].isMine)
{
Expand(Row_A, Column_A);
}
}
}
}
}
private static void SetCellOpen(MineFiledCell TargetCell)
{
if (TargetCell.isMine)
{
if (CurrentPlayer == MSNMineSweeper.CurrentPlayer.Red)
{
TargetCell.CellImage.Source = CellImageRef["Flag_Red"].Source;
PlayerRed.MineFound++;
myGameWindow.MineGot_Red.Text = PlayerRed.MineFound.ToString("D2");
}
else
{
TargetCell.CellImage.Source = CellImageRef["Flag_Blue"].Source;
PlayerBlue.MineFound++;
myGameWindow.MineGot_Blue.Text = PlayerBlue.MineFound.ToString("D2");
}
myGameWindow.RemainMineNo.Content = (51 - (PlayerBlue.MineFound + PlayerRed.MineFound)).ToString();
}
else
{
TargetCell.CellImage.Source = CellImageRef[TargetCell.MineAroundCount.ToString()].Source;
}
TargetCell.isOpened = true;
}
static Timer _resetStatusTimer = new Timer(12);
private static void ChangePlayer()
{
if (CurrentPlayer == MSNMineSweeper.CurrentPlayer.Red)
{
CurrentPlayer = MSNMineSweeper.CurrentPlayer.Blue;
myGameWindow.Arrow_Blue.Visibility = System.Windows.Visibility.Visible;
myGameWindow.Arrow_Red.Visibility = System.Windows.Visibility.Hidden;
}
else
{
CurrentPlayer = MSNMineSweeper.CurrentPlayer.Red;
myGameWindow.Arrow_Blue.Visibility = System.Windows.Visibility.Hidden;
myGameWindow.Arrow_Red.Visibility = System.Windows.Visibility.Visible;
}
Task.Factory.StartNew(() =>
{
_resetStatusTimer.Stop();
_resetStatusTimer = new Timer(1500);
myGameWindow.ChangePlayerNotice.Dispatcher.Invoke(() =>
{
myGameWindow.ChangePlayerNotice.Visibility = System.Windows.Visibility.Visible;
});
_resetStatusTimer.Elapsed += HideChangePlayer;
_resetStatusTimer.Start();
});
}
private static void HideChangePlayer(object sender, ElapsedEventArgs e)
{
myGameWindow.ChangePlayerNotice.Dispatcher.Invoke(() =>
{
myGameWindow.ChangePlayerNotice.Visibility = System.Windows.Visibility.Hidden;
});
_resetStatusTimer.Stop();
}
}
class Player
{
public String PlayerName;
public Int32 MineFound = 0;
}
class MineFiledCell
{
public Boolean isMine = false;
public Boolean isOpened = false;
public Image CellImage = new Image();
public Int32 MineAroundCount = 0;
public void SetCellImageGrid(Int32 Row, Int32 Column)
{
CellImage.SetValue(Grid.RowProperty, Row);
CellImage.SetValue(Grid.ColumnProperty, Column);
}
}
}