forked from learner1999/c-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
扫雷.cpp
205 lines (171 loc) · 3.61 KB
/
扫雷.cpp
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
/* ▇ */
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# define SIZE_OF_MINE 13 //雷区的边长
# define NUM_OF_MINE 20 //地雷的个数
int mine[SIZE_OF_MINE + 1][SIZE_OF_MINE + 1] = { 0 }; //纪录雷区每个点为空(0)、数字(1~8)、雷(>=9)
int mine_copy[SIZE_OF_MINE + 1][SIZE_OF_MINE + 1] = { 0 }; //纪录雷区每个点是否已经显示出来,隐藏0,显示1
int mine_note[NUM_OF_MINE][2]; //纪录地雷的坐标
void init_game(void); //初始化游戏
void rand_mine(int num_of_mine); //随机生成地雷
void print_mine(void); //打印“雷区”
void is_zero(int x, int y); //判断用户输入的点是否为“0点”,如果是,则递归将附近的“0点显示出来”
int main(void)
{
int x, y, i, j;
int cout = 0;
system("mode con cols=30 lines=20");
printf("正在生成雷区,请稍后……\n");
init_game();
while (1)
{
printf("请输入要选择的点的坐标,左上角为1,1:");
scanf("%d%d", &x, &y);
if (mine[x][y] < 9 && mine[x][y] > 0)
{
mine_copy[x][y] = 1;
system("cls");
print_mine();
}
else if (mine[x][y] >= 9)
{
for (cout = 0; cout < NUM_OF_MINE; cout++)
{
mine_copy[mine_note[cout][0]][mine_note[cout][1]] = 1;
}
system("cls");
print_mine();
printf("GAME OVER!\n");
exit(-1);
}
else
{
is_zero(x, y);
system("cls");
print_mine();
}
for (i = 1; i <= SIZE_OF_MINE; i++)
{
for (j = 1; j <= SIZE_OF_MINE; j++)
{
if (0 == mine_copy[i][j] && mine[i][j] < 9)
goto out_loop;
}
}
out_loop:
if (i > SIZE_OF_MINE)
{
printf("YOU ARE WIN!\n");
getchar();
exit(-1);
}
}
return 0;
}
void init_game(void)
{
/* 随机生成地雷位置 */
rand_mine(NUM_OF_MINE);
system("cls");
/* 打印“雷区” */
print_mine();
return;
}
void print_mine(void)
{
int i, j;
for (i = 1; i <= SIZE_OF_MINE; i++)
{
for (j = 1; j <= SIZE_OF_MINE; j++)
{
if (1 == mine_copy[i][j] && mine[i][j] < 9)
{
printf(" %d", mine[i][j]);
}
else if (1 == mine_copy[i][j] && mine[i][j] >= 9)
{
printf(" *");
}
else
{
printf("▇");
}
}
printf("\n");
}
return;
}
void rand_mine(int num_of_mine)
{
int i, j;
int cout = 0;
/* 随机生成地雷的坐标 */
srand((int)time(NULL) * 189);
while (num_of_mine--)
{
while (1)
{
i = 1 + SIZE_OF_MINE * rand() / RAND_MAX;
j = 1 + SIZE_OF_MINE * rand() / RAND_MAX;
if (9 != mine[i][j]) //9代表该点为地雷
{
mine[i][j] = 9;
break;
}
}
/* 将地雷的坐标纪录下来 */
mine_note[cout][0] = i;
mine_note[cout][1] = j;
cout++;
}
/* 计算出每个点的数值,地雷周围的点自增1 */
for (i = 1; i <= SIZE_OF_MINE; i++)
{
for (j = 1; j <= SIZE_OF_MINE; j++)
{
if (mine[i][j] >= 9)
{
if (i - 1 >= 1 && j - 1 >= 1)
mine[i - 1][j - 1]++;
if (i - 1 >= 1)
mine[i - 1][j]++;
if (i - 1 >= 1 && j + 1 <= SIZE_OF_MINE)
mine[i - 1][j + 1]++;
if (j - 1 >= 1)
mine[i][j - 1]++;
if (j + 1 <= SIZE_OF_MINE)
mine[i][j + 1]++;
if (i + 1 <= SIZE_OF_MINE && j - 1 >= 1)
mine[i + 1][j - 1]++;
if (i + 1 <= SIZE_OF_MINE)
mine[i + 1][j]++;
if (i + 1 <= SIZE_OF_MINE && j + 1 <= SIZE_OF_MINE)
mine[i + 1][j + 1]++;
}
}
}
return;
}
void is_zero(int x, int y)
{
if (x < 1 || x > SIZE_OF_MINE || y < 1 || y > SIZE_OF_MINE)
{
return;
}
else if (0 == mine[x][y] && 1 == mine_copy[x][y])
{
return;
}
else if (0 != mine[x][y])
{
mine_copy[x][y] = 1;
return;
}
mine_copy[x][y] = 1;
is_zero(x - 1, y);
is_zero(x, y - 1);
is_zero(x, y + 1);
is_zero(x + 1, y);
return;
}