[杂项项目]C语言实现小游戏——控制台扫雷

程序比较简单,需要输入坐标x,y进行翻牌。当时现场演示时不到50分钟边讲边写的,实现了基本的扫雷功能,只是没有计分计时排名之类的辅助功能。由于程序较简单,也没必要再加工了。另外本程序的特点是第一次输入一定不会是雷。此外,如果翻牌后周围没有雷,即为0,将会搜索周围所以为0的牌。此处用到了dfs。仅供参考。

项目版:

mine.h

1
2
3
4
5
6
7
#pragma once  
#define BOOM 10
#define SIZE 10
void init(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int a, int b);//初始化扫雷页面
void dfs(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int i, int j);//如果展开面为0,通过深搜将周围所以为0的区域展开
int check(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int x, int y);//判断胜利条件
void show(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int x, int y);//展示扫雷页面

mine.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
#include<stdio.h>  
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"mine.h"
int stemp[8][2] = { { 0,1 },{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,-1 },{ 1,-1 },{ -1,1 } };//深搜时上下左右四个方向搜索
void init(char (*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int a, int b) {
int x, y;
visit[a][b] = 0;
for (int i = 0; i < 10; i++) {
do {
x = rand() % SIZE + 1;
y = rand() % SIZE + 1;
} while (visit[x][y] == 0);
if (visit[x][y] == -1) {
visit[x][y] == 0;
MAP[x][y] = '*';
}
}
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE; j++) {
if (MAP[i][j] != '*') {
MAP[i][j] = '0';
for (int k = 0; k < 8; k++) {
if (MAP[i + stemp[k][0]][j + stemp[k][1]] == '*')
MAP[i][j]++;
}
}
}
}
}
void dfs(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int i, int j) {
visit[i][j] = 1;
for (int k = 0; k < 8; k++) {
if (MAP[i + stemp[k][0]][j + stemp[k][1]] == '0'&&visit[i + stemp[k][0]][j + stemp[k][1]] != 1)
dfs(MAP, visit, i + stemp[k][0], j + stemp[k][1]);
else if (MAP[i + stemp[k][0]][j + stemp[k][1]] != '*'&&visit[i + stemp[k][0]][j + stemp[k][1]] != 1)
visit[i + stemp[k][0]][j + stemp[k][1]] = 1;
}
}
int check(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int x, int y) {
if (MAP[x][y] == '*') {
visit[x][y] = 1;
return 1;
}
else if (MAP[x][y] == '0') {
dfs(MAP, visit, x, y);
}
else
visit[x][y] = 1;
int temp = 0;
for (int i = 1; i <= SIZE; i++)
for (int j = 1; j <= SIZE; j++) {
if (visit[i][j] == -1)
temp++;
}
if (temp == BOOM)
return 2;
return 0;
}
void show(char(*MAP)[SIZE + 2], int(*visit)[SIZE + 2], int x, int y) {
printf("-------------------------------------------------\n");
for (int i = 0; i <= SIZE + 1; i++) {
printf("|");
for (int j = 0; j <= SIZE + 1; j++) {
if (visit[i][j] == 1) {
printf(" %c ", MAP[i][j]);
printf("|");
}
else {
printf(" ");
printf("|");
}
}
printf("\n");
printf("-------------------------------------------------\n");
}
}

main.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
#include<stdio.h>  
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"mine.h"
char MAP[SIZE + 2][SIZE + 2];
int visit[SIZE + 2][SIZE + 2];
int x, y;//坐标
int main() {
srand(time(0));
memset(visit, -1, sizeof(visit));
MAP[0][0] = ' ';
for (int i = 1; i <= SIZE; i++) {
MAP[0][i] = 'A' + i - 1;
MAP[i][0] = 'a' + i - 1;
MAP[SIZE + 1][i] = 'A' + i - 1;
MAP[i][SIZE + 1] = 'a' + i - 1;
}
for (int i = 1; i <= SIZE + 1; i++) {
visit[0][i] = 1;
visit[i][0] = 1;
visit[SIZE + 1][i] = 1;
visit[i][SIZE + 1] = 1;
}
show(MAP, visit, x, y);
scanf("%d%d", &y, &x);
init(MAP,visit, x, y);
memset(visit, -1, sizeof(visit));
for (int i = 1; i <= SIZE + 1; i++) {
visit[0][i] = 1;
visit[i][0] = 1;
visit[SIZE + 1][i] = 1;
visit[i][SIZE + 1] = 1;
}
check(MAP, visit, x, y);
system("cls");
show(MAP, visit, x, y);
while (MAP[x][y] != '*') {
scanf("%d%d", &y, &x);
int res = check(MAP, visit, x, y);
system("cls");
show(MAP, visit, x, y);
if (res == 2) {
printf("Win\n");
system("pause");
return 0;
}
else if (res == 1) {
printf("Lose\n");
system("pause");
return 0;
}
}
return 0;
}

img

另附原始版:

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
#include<stdio.h>  
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define BOOM 10
#define SIZE 10
char MAP[SIZE + 2][SIZE + 2];
int visit[SIZE + 2][SIZE + 2];
int x, y;
int stemp[8][2] = { {0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,-1},{1,-1}, {-1,1} };
void init(int a, int b) {
int x, y;
visit[a][b] = 0;
for (int i = 0; i < 10; i++) {
do {
x = rand() % SIZE + 1;
y = rand() % SIZE + 1;
} while (visit[x][y] == 0);
if (visit[x][y] == -1) {
visit[x][y] == 0;
MAP[x][y] = '*';
}
}
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE; j++) {
if (MAP[i][j] != '*') {
MAP[i][j] = '0';
for (int k = 0; k < 8; k++) {
if (MAP[i + stemp[k][0]][j + stemp[k][1]] == '*')
MAP[i][j]++;
}
}
}
}
}
void dfs(int i, int j) {
visit[i][j] = 1;
for (int k = 0; k < 8; k++) {
if (MAP[i + stemp[k][0]][j + stemp[k][1]] == '0'&&visit[i + stemp[k][0]][j + stemp[k][1]] != 1)
dfs(i + stemp[k][0], j + stemp[k][1]);
else if (MAP[i + stemp[k][0]][j + stemp[k][1]] != '*'&&visit[i + stemp[k][0]][j + stemp[k][1]] != 1)
visit[i + stemp[k][0]][j + stemp[k][1]] = 1;
}
}
int check() {
if (MAP[x][y] == '*') {
visit[x][y] = 1;
return 1;
}
else if (MAP[x][y] == '0') {
dfs(x, y);
}
else
visit[x][y] = 1;
int temp = 0;
for (int i = 1; i <= SIZE; i++)
for (int j = 1; j <= SIZE; j++) {
if (visit[i][j] == -1)
temp++;
}
if (temp == BOOM)
return 2;
return 0;
}
void show() {
printf("-------------------------------------------------\n");
for (int i = 0; i <= SIZE + 1; i++) {
printf("|");
for (int j = 0; j <= SIZE + 1; j++) {
if (visit[i][j] == 1) {
printf(" %c ", MAP[i][j]);
printf("|");
}
else {
printf(" ");
printf("|");
}
}
printf("\n");
printf("-------------------------------------------------\n");
}
}
int main() {
srand(time(0));
memset(visit, -1, sizeof(visit));
MAP[0][0] = ' ';
for (int i = 1; i <= SIZE; i++) {
MAP[0][i] = 'A' + i - 1;
MAP[i][0] = 'a' + i - 1;
MAP[SIZE + 1][i] = 'A' + i - 1;
MAP[i][SIZE + 1] = 'a' + i - 1;
}
for (int i = 1; i <= SIZE + 1; i++) {
visit[0][i] = 1;
visit[i][0] = 1;
visit[SIZE + 1][i] = 1;
visit[i][SIZE + 1] = 1;
}
show();
scanf("%d%d", &x, &y);
init(x, y);
memset(visit, -1, sizeof(visit));
for (int i = 1; i <= SIZE + 1; i++) {
visit[0][i] = 1;
visit[i][0] = 1;
visit[SIZE + 1][i] = 1;
visit[i][SIZE + 1] = 1;
}
check();
system("cls");
show();
while (MAP[x][y] != '*') {
scanf("%d%d", &x, &y);
int res = check();
system("cls");
show();
if (res == 2) {
printf("Win\n");
system("pause");
return 0;
}
else if (res == 1) {
printf("Lose\n");
system("pause");
return 0;
}
}
return 0;
}

纸短情长啊文章结束了但我们的故事还在继续
坚持原创技术分享,您的支持将鼓励我继续创作!