前言

这个程序本质上是使用C#重构了之前写过的《用shell脚本设计的『扫雷』游戏》,其中利用了许多C#的语言特性。相比BASH的版本,本程序极大提高了游戏的运行效率——起码在点开空格时,你是看不到临近地块被翻开的过程的。

源码

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
namespace 扫雷 {
internal class Program {
enum TileStatus {
active, hidden, marked, suspected
}
enum End {
quit, win, lose, not
}
struct Field {
public byte width;
public byte height;
public short mineCount;
public byte[,] table;
public TileStatus[,] status;
public void Fill() {
table = new byte[width, height];
status = new TileStatus[width, height];
for (byte x = 0; x < width; x++) {
for (byte y = 0; y < height; y++) {
table[x, y] = 0;
status[x, y] = TileStatus.hidden;
}
}
}
public void PutMine(byte x, byte y) {
byte[,] safeList = new byte[9, 2];
byte safeListKey = 0;
foreach (int w in new int[] {x-1, x, x+1}) {
if (w < 0 || w >= width) continue;
foreach (int h in new int[] {y-1, y, y+1}) {
if (h < 0 || h >= height) continue;
safeList[safeListKey, 0] = (byte)w;
safeList[safeListKey, 1] = (byte)h;
safeListKey++;
}
}

byte[,] mineList = new byte[mineCount, 2];
short mineListKey = 0;
while (mineListKey < mineCount) {
byte xRand = (byte)(new Random().Next(width));
byte yRand = (byte)(new Random().Next(height));
bool isRepeat = false;
for (byte i = 0; i < 9; i++) {
if (xRand == safeList[i, 0] &&
yRand == safeList[i, 1]) {
isRepeat = true;
break;
}
}
for (byte i = 0; i < mineCount; i++) {
if (xRand == mineList[i, 0] &&
yRand == mineList[i, 1]) {
isRepeat = true;
break;
}
}
if (!isRepeat) {
table[xRand, yRand] = 9;
mineList[mineListKey, 0] = xRand;
mineList[mineListKey, 1] = yRand;
mineListKey++;
}
}
}
public void PutNumber() {
Field field = this;
for (byte x = 0; x < width; x++) {
for (byte y = 0; y < height; y++) {
if (table[x, y] == 9) {
PlusOne(x, y, field);
}
}
}
static void PlusOne(byte x, byte y, Field field) {
foreach (int w in new int[] { x - 1, x, x + 1 }) {
if (w < 0 || w >= field.width) continue;
foreach (int h in new int[] { y - 1, y, y + 1 }) {
if (h < 0 || h >= field.height) continue;
if (field.table[w, h] != 9) {
field.table[w, h]++;
}
}
}
}
}
public bool Judging() {
short markNum = 0;
foreach (TileStatus tileStatus in status) {
if (tileStatus == TileStatus.hidden ||
tileStatus == TileStatus.suspected) {
return false;
}
else if (tileStatus == TileStatus.marked) {
markNum++;
}
}
if (markNum == mineCount) {
return true;
}
else {
return false;
}
}
}
struct Focus {
public byte x;
public byte y;
public Field field;
public void MoveUp() {
y = (byte)(y == 0 ? field.height - 1 : y - 1);
}
public void MoveDown() {
y = (byte)(y == field.height - 1 ? 0 : y + 1);
}
public void MoveLeft() {
x = (byte)(x == 0 ? field.width - 1 : x - 1);
}
public void MoveRight() {
x = (byte)(x == field.width - 1 ? 0 : x + 1);
}
public void Check() {
Focus focus = this;
void TileCheck(byte x, byte y) {
if (focus.field.status[x, y] != TileStatus.active) {
focus.field.status[x, y] = TileStatus.active;
DrawTile(x, y, focus.field, focus);
}
}
List<byte[]> edgeList = [[x, y]];
while (edgeList.Count > 0) {
byte[] edge = edgeList[edgeList.Count - 1];
TileCheck(edge[0], edge[1]);
edgeList.RemoveAt(edgeList.Count - 1);
if (field.table[edge[0], edge[1]] == 0) {
foreach (int w in new int[] {edge[0] - 1, edge[0], edge[0] + 1}) {
if (w < 0 || w >= field.width) continue;
foreach (int h in new int[] { edge[1] - 1, edge[1], edge[1] + 1}) {
if (h < 0 || h >= field.height) continue;
if (field.status[w, h] != TileStatus.active) {
edgeList.Add([(byte)w, (byte)h]);
}
}
}
}
}
}
public void Mark() {
if (field.status[x, y] == TileStatus.marked) {
field.status[x, y] = TileStatus.hidden;
}
else if (field.status[x, y] != TileStatus.active) {
field.status[x, y] = TileStatus.marked;
}
}
public void Suspect() {
if (field.status[x, y] == TileStatus.suspected) {
field.status[x, y] = TileStatus.hidden;
}
else if (field.status[x, y] != TileStatus.active) {
field.status[x, y] = TileStatus.suspected;
}
}
}
static void DrawTile(byte x, byte y, Field field, Focus focus) {
ConsoleColor background = Console.BackgroundColor;
ConsoleColor foreground = Console.ForegroundColor;
ConsoleColor font = (ConsoleColor)(8 - field.table[x, y]);
void AutoFocus(ConsoleColor bkgrd, ConsoleColor frgrd, string obj) {
Console.SetCursorPosition(2 * x + 1, y + 2);
if (focus.x == x && focus.y == y) {
Console.BackgroundColor = frgrd;
Console.ForegroundColor = bkgrd;
}
else {
Console.ForegroundColor = frgrd;
}
Console.Write(' ' + obj + ' ');
Console.ResetColor();
}
if (field.status[x, y] == TileStatus.active) {
if (field.table[x, y] == 9) {
AutoFocus(background, ConsoleColor.Red, "#");
}
else if (field.table[x, y] == 0) {
AutoFocus(background, foreground, " ");
}
else {
AutoFocus(background, font, field.table[x, y] + "");
}
}
else if (field.status[x, y] == TileStatus.hidden) {
AutoFocus(background, foreground, "+");
}
else if (field.status[x, y] == TileStatus.marked) {
AutoFocus(background, ConsoleColor.Green, "F");
}
else if (field.status[x, y] == TileStatus.suspected) {
AutoFocus(background, ConsoleColor.Yellow, "?");
}
}
static void DrawField(Field field, string title) {
Console.Clear();
byte fieldWidth = (byte)(field.width*2 + 3);
byte titleWidth = (byte)(title.Length);
if (fieldWidth >= titleWidth) {
Console.SetCursorPosition(fieldWidth/2 - titleWidth/2, 0);
Console.WriteLine(title);
}
else {
Console.SetCursorPosition(0, 0);
Console.WriteLine(title);
}

Console.Write("┌─");
for (byte i = 0; i < field.width; i++) {
Console.Write("──");
}
Console.WriteLine("┐");

for (byte i = 0; i < field.height; i++) {
Console.Write("│ ");
for (byte j = 0; j < field.width; j++) {
Console.Write("+ ");
}
Console.WriteLine("│");
}

Console.Write("└─");
for (byte i = 0; i < field.width; i++) {
Console.Write("──");
}
Console.WriteLine("┘");
}
static void Main(string[] args) {
string title = "MineSweeper";
Field field = new Field() {
width = 30,
height = 16,
mineCount = 99
};
field.Fill();
Focus focus = new Focus() {
field = field,
x = (byte)(field.width / 2),
y = (byte)(field.height / 2),
};
DrawField(field, title);

bool checkFirst = false;
End gameEnd = End.not;
byte oldX = focus.x, oldY = focus.y;
while (gameEnd == End.not) {
DrawTile(oldX, oldY, field, focus);
oldX = focus.x;
oldY = focus.y;
DrawTile(focus.x, focus.y, field, focus);
ConsoleKey input = Console.ReadKey(true).Key;
switch (input) {
case ConsoleKey.W:
case ConsoleKey.UpArrow:
focus.MoveUp();
break;
case ConsoleKey.A:
case ConsoleKey.LeftArrow:
focus.MoveLeft();
break;
case ConsoleKey.S:
case ConsoleKey.DownArrow:
focus.MoveDown();
break;
case ConsoleKey.D:
case ConsoleKey.RightArrow:
focus.MoveRight();
break;
case ConsoleKey.F:
focus.Mark();
break;
case ConsoleKey.E:
focus.Suspect();
break;
case ConsoleKey.Q:
gameEnd = End.quit;
break;
case ConsoleKey.Spacebar:
if (!checkFirst) {
checkFirst = true;
field.PutMine(focus.x, focus.y);
field.PutNumber();
}
focus.Check();
if (field.table[focus.x, focus.y] == 9) {
Console.Beep();
gameEnd = End.lose;
}
else if (field.Judging()) {
gameEnd = End.win;
}
break;
}
Console.SetCursorPosition(0, field.height+2);
}
}
}
}