C语言实现扫雷程序

时间:2022-11-23 19:39:33

使用C语言实现简单的扫雷程序,主要是对二维数组的运用,我们需要一个头文件,两个源文件来实现。

game.h //包含函数的声明,宏定义
test.c //包含主函数,函数调用
game.c //包含函数的定义

整体思路

1.要完成一个简单的扫雷程序,我们需要创建两个二维数组,一个保存我们随机生成的雷,另外一个向外界展示。

?
1
2
3
4
5
6
7
8
9
10
//使用宏定义定义常量,方便之后对数组的使用
#define ROW 11 //雷
#define COL 11
#define ROWS 9 //棋盘
#define COLS 9
#define THUNDER 10 //雷数
 
char mine[ROW][COL] = { 0 }; //存雷数组
char show[ROWS][COLS] = { 0 }; //展示数组
Arr_init(mine, show, ROW, COL, ROWS, COLS); //数组初始化

2.完成对数组的初始化后,我们需要对雷进行放置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Col_thu(char mine[ROW][COL], int row, int col, int thunder) //布置雷
{
 int x, y;
 int i = 0;
 while (i < thunder) //存放雷的个数等于雷的个数
 {
 x = rand() % (row-2) + 1;
 y = rand() % (col-2) + 1;
 if (mine[x][y] == '0')
 {
 mine[x][y] = '1';
 i++;
 }
 }
}

3.布置完雷后,我们需要打印所需要的棋盘

存雷棋盘

?
1
2
3
4
5
6
7
8
9
10
11
12
void Print_che1(char mine[ROW][COL], int row, int col) //打印存雷棋盘
{
 int i, j;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 {
 printf("%2c", mine[i][j]);
 }
 printf("\n");
 }
}

展示棋盘

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Print_che2(char show[ROWS][COLS], int rows, int cols) //打印展示棋盘
{
 int i, j;
 for (i = 0; i <= rows; i++) //方便我们输入坐标
 {
 printf("%2d", i);
 }
 printf("\n");
 for (i = 0; i < rows; i++)
 {
 printf("%2d", i+1);
 for (j = 0; j < cols; j++)
 {
 printf("%2c", show[i][j]);
 }
 printf("\n");
 }
}

4.打印完棋盘后,我们开始扫雷了。

在扫雷的过程中,我们需要在没有找到雷时展示输入坐标周围的雷数并进行展开,同时,为了增加游戏的可玩性,当第一次就找到雷时,我们需要将雷转移到其他位置。

统计周围雷数

?
1
2
3
4
5
6
void Num_mines(char mine[ROW][COL],char show[ROWS][COLS], int x, int y) //计算输入坐标周围的雷数
{
 int ch;
 ch = mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] - 8 * '0';
 show[x - 1][y - 1] = ch + 48; //数字对应的ASCLL与数字字符相差48
}

展开

?
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
void open_show(char mine[ROW][COL], char show[ROWS][COLS], int rows, int x, int y) //计算输入坐标及周围的雷数(展开)
{
 if (mine[x][y - 1] == '0')//中 x,y
 {
 if (x - 1 >= 0 && y - 1 >= 0 && x + 1 <= rows + 1 && y + 1 <= rows + 1) //防止越界情况(所有的坐标+1<=rows+1,-1>=0)
 {
 Num_mines(mine, show, x, y); //返回坐标周围的雷数
 }
 }
 if (mine[x - 1][y - 1] == '0')//左上角x-1,y-1
 {
 if (x - 2 >= 0 && y - 2 >= 0 && x <= rows + 1 && y <= rows + 1)
 {
 Num_mines(mine, show, x - 1, y - 1);
 }
 }
 
 if (mine[x - 1][y] == '0')//上x-1, y
 {
 if (x - 2 >= 0 && y - 1 >= 0 && x <= rows + 1 && y + 1 <= rows + 1)
 {
 Num_mines(mine, show, x - 1, y);
 }
 }
 if (mine[x - 1][y + 1] == '0')//右上角 x-1, y+1
 {
 if (x - 2 >= 0 && y >= 0 && x <= rows + 1 && y + 2 <= rows + 1)
 {
 Num_mines(mine, show, x - 1, y + 1);
 }
 }
 if (mine[x][y - 1] == '0')//左 x,y-1
 {
 if (x - 1 >= 0 && y - 2 >= 0 && x + 1 <= rows + 1 && y <= rows + 1)
 {
 Num_mines(mine, show, x, y - 1);
 }
 }
 if (mine[x][y + 1] == '0')//右 x,y+1
 {
 if (x - 1 >= 0 && y >= 0 && x + 1 <= rows + 1 && y + 2 <= rows + 1)
 {
 Num_mines(mine, show, x, y + 1);
 }
 }
 
 if (mine[x + 1][y - 1] == '0')//左下角 x+1,y-1
 {
 if (x >= 0 && y - 2 >= 0 && x + 2 <= rows + 1 && y <= rows + 1)
 {
 Num_mines(mine, show, x + 1, y - 1);
 }
 }
 
 if (mine[x + 1][y] == '0')//下 x+1,y
 {
 if (x >= 0 && y - 1 >= 0 && x + 2 <= rows + 1 && y + 1 <= rows + 1)
 {
 Num_mines(mine, show, x + 1, y);
 }
 }
 if (mine[x + 1][y + 1] == '0')//右下角 x+1,y+1
 {
 if (x >= 0 && y >= 0 && x + 2 <= rows + 1 && y + 2 <= rows + 1)
 {
 Num_mines(mine, show, x + 1, y + 1);
 }
 }
}

扫雷

?
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
char Find_thu(char mine[ROW][COL], char show[ROWS][COLS], int row, int col, int i) //找雷
{
 int x, y;
flag1:
 printf("请玩家输入坐标");
 scanf("%d %d", &x, &y);
flag:
 if ((x > 0 && x <= row - 2) && (y > 0 && y <= col - 2)) //判断输入坐标的正确性
 {
 if (mine[x][y] == '0')//没找到雷
 {
 open_show(mine, show, ROWS, x, y); //计算输入坐标及周围的雷数(展开)
 return '0';
 }
 else //找到雷
 {
 if (i == 0) //第一个就找到雷
 {
 mine[x][y] = '0';
 while (1)
 {
  int a, b;
  a = rand() % (row - 2) + 1;
  b = rand() % (col - 2) + 1;
  if (mine[a][b] == '0')
  {
  mine[a][b] = '1';
  goto flag;
  }
 }
 }
 else
 {
 show[x - 1][y - 1] = '1';
 return '1';
 }
 }
 }
 else
 {
 printf("输入错误\n");
 goto flag1;
 }
}

确定大致思路后,我们完成程序的流程部分,并放入我们所创建的文件中。

代码如下:

game.h //包含函数的声明,宏定义

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef __GAME_H__
#define __GAME_H__
 
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
 
#define ROW 11//雷
#define COL 11
#define ROWS 9 //棋盘
#define COLS 9
#define THUNDER 10 //雷数
 
void Arr_init(char mine[ROW][COL], char show[ROWS][COLS], int row, int col, int rows, int cols); //数组初始化
void Col_thu(char mine[ROW][COL], int row, int col, int thunder); //布置雷
void Print_che1(char mine[ROW][COL], int row, int col); //打印存雷棋盘
void Print_che2(char show[ROWS][COLS], int rows, int cols); //打印展示棋盘
char Find_thu(char mine[ROW][COL], char show[ROWS][COLS], int row, int col, int i); //找雷
void Num_mines(char mine[ROW][COL], char show[ROWS][COLS], int x, int y);//计算输入坐标周围的雷数
void open_show(char mine[ROW][COL], char show[ROWS][COLS], int rows, int x, int y);////计算输入坐标及周围的雷数(展开)
#endif // __GAME_H__

test.c //包含主函数,函数调用

?
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
#define _CRT_SECURE_NO_WARNINGS 1
 
#include "game.h"
 
void menu() //菜单函数
{
 printf("********************\n");
 printf("**** 1.play ****\n");
 printf("**** 0.exit ****\n");
 printf("********************\n");
}
 
void game() //游戏函数
{
 int i;
 char mine[ROW][COL] = {0};//存雷数组
 char show[ROWS][COLS] = { 0 };//展示数组
 Arr_init(mine, show, ROW, COL, ROWS, COLS);//数组初始化
 Col_thu(mine, ROW, COL, THUNDER); //布置雷
 Print_che2(show, ROWS, COLS); //打印展示棋盘
 for (i = 0; i < ROWS * COLS - THUNDER; i++)
 {
 char n;
 n = Find_thu(mine, show, ROW, COL, i); //找雷
 Print_che2(show, ROWS, COLS); //打印展示棋盘
 if (n == '0')
 {
 printf("_______________________\n");
 }
 else
 {
 printf("你踩到雷了,游戏结束\n");
 Print_che1(mine, ROW, COL); //打印存雷棋盘
 break;
 }
 }
 if (i == ROWS * COLS - THUNDER)
 printf("游戏成功\n");
}
 
void test() //游戏流程函数
{
 int input;
 srand((unsigned)time(NULL));
 do
 {
 menu();
 printf("请输入选择:");
 scanf("%d", &input);
 switch (input)
 {
 case 1:
 game();
 break;
 case 0:
 break;
 default:
 printf("输入错误,请重新输入\n");
 }
 } while (input);
}
 
int main()
{
 test();
 system("pause");
 return 0;
}

game.c //包含函数的定义

?
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
#include "game.h"
 
void Arr_init(char mine[ROW][COL], char show[ROWS][COLS], int row, int col, int rows, int cols)//数组初始化
{
 int i, j;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 {
 mine[i][j] = '0';//改变存雷数组
 }
 }
 for (i = 0; i < rows; i++)
 {
 for (j = 0; j < cols; j++)
 {
 show[i][j] = '*';//改变展示数组
 }
 }
}
 
void Col_thu(char mine[ROW][COL], int row, int col, int thunder) //布置雷
{
 int x, y;
 int i = 0;
 while (i < thunder) //存放雷的个数等于雷的个数
 {
 x = rand() % (row-2) + 1;
 y = rand() % (col-2) + 1;
 if (mine[x][y] == '0')
 {
 mine[x][y] = '1';
 i++;
 }
 }
}
 
void Print_che1(char mine[ROW][COL], int row, int col) //打印存雷棋盘
{
 int i, j;
 for (i = 0; i < row; i++)
 {
 for (j = 0; j < col; j++)
 {
 printf("%2c", mine[i][j]);
 }
 printf("\n");
 }
}
 
void Print_che2(char show[ROWS][COLS], int rows, int cols) //打印展示棋盘
{
 int i, j;
 for (i = 0; i <= rows; i++)//方便我们输入坐标
 {
 printf("%2d", i);
 }
 printf("\n");
 for (i = 0; i < rows; i++)
 {
 printf("%2d", i+1);
 for (j = 0; j < cols; j++)
 {
 printf("%2c", show[i][j]);
 }
 printf("\n");
 }
}
 
void open_show(char mine[ROW][COL], char show[ROWS][COLS], int rows, int x, int y) //计算输入坐标及周围的雷数(展开)
{
 if (mine[x][y - 1] == '0')//中 x,y
 {
 if (x - 1 >= 0 && y - 1 >= 0 && x + 1 <= rows + 1 && y + 1 <= rows + 1) //防止越界情况(所有的坐标+1<=rows+1,-1>=0)
 {
 Num_mines(mine, show, x, y); //返回坐标周围的雷数
 }
 }
 if (mine[x - 1][y - 1] == '0')//左上角x-1,y-1
 {
 if (x - 2 >= 0 && y - 2 >= 0 && x <= rows + 1 && y <= rows + 1)
 {
 Num_mines(mine, show, x - 1, y - 1);
 }
 }
 
 if (mine[x - 1][y] == '0')//上x-1, y
 {
 if (x - 2 >= 0 && y - 1 >= 0 && x <= rows + 1 && y + 1 <= rows + 1)
 {
 Num_mines(mine, show, x - 1, y);
 }
 }
 if (mine[x - 1][y + 1] == '0')//右上角 x-1, y+1
 {
 if (x - 2 >= 0 && y >= 0 && x <= rows + 1 && y + 2 <= rows + 1)
 {
 Num_mines(mine, show, x - 1, y + 1);
 }
 }
 if (mine[x][y - 1] == '0')//左 x,y-1
 {
 if (x - 1 >= 0 && y - 2 >= 0 && x + 1 <= rows + 1 && y <= rows + 1)
 {
 Num_mines(mine, show, x, y - 1);
 }
 }
 if (mine[x][y + 1] == '0')//右 x,y+1
 {
 if (x - 1 >= 0 && y >= 0 && x + 1 <= rows + 1 && y + 2 <= rows + 1)
 {
 Num_mines(mine, show, x, y + 1);
 }
 }
 
 if (mine[x + 1][y - 1] == '0')//左下角 x+1,y-1
 {
 if (x >= 0 && y - 2 >= 0 && x + 2 <= rows + 1 && y <= rows + 1)
 {
 Num_mines(mine, show, x + 1, y - 1);
 }
 }
 
 if (mine[x + 1][y] == '0')//下 x+1,y
 {
 if (x >= 0 && y - 1 >= 0 && x + 2 <= rows + 1 && y + 1 <= rows + 1)
 {
 Num_mines(mine, show, x + 1, y);
 }
 }
 
 if (mine[x + 1][y + 1] == '0')//右下角 x+1,y+1
 {
 if (x >= 0 && y >= 0 && x + 2 <= rows + 1 && y + 2 <= rows + 1)
 {
 Num_mines(mine, show, x + 1, y + 1);
 }
 }
}
char Find_thu(char mine[ROW][COL], char show[ROWS][COLS], int row, int col, int i)//找雷
{
 int x, y;
flag1:
 printf("请玩家输入坐标");
 scanf("%d %d", &x, &y);
flag:
 if ((x > 0 && x <= row - 2) && (y > 0 && y <= col - 2))//判断输入坐标的正确性
 {
 if (mine[x][y] == '0')//没找到雷
 {
 open_show(mine, show, ROWS, x, y);//计算输入坐标及周围的雷数(展开)
 return '0';
 }
 else //找到雷
 {
 if (i == 0)//第一个就找到雷
 {
 mine[x][y] = '0';
 while (1)
 {
  int a, b;
  a = rand() % (row - 2) + 1;
  b = rand() % (col - 2) + 1;
  if (mine[a][b] == '0')
  {
  mine[a][b] = '1';
  goto flag;
  }
 }
 }
 else
 {
 show[x - 1][y - 1] = '1';
 return '1';
 }
 
 }
 }
 else
 {
 printf("输入错误\n");
 goto flag1;
 }
}

到这里,我们的程序就完成了,我们看看程序的效果

C语言实现扫雷程序

C语言实现扫雷程序

以上就是一个简单的扫雷程序,多有不足之处,还望指教。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/lw13572259173/article/details/79834300