本文实例为大家分享了C语言代码实现简易扫雷的具体代码,供大家参考,具体内容如下
源.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
|
#define _CRT_SECURE_NO_WARNINGS #include"Game.h" void Game() { //创建两个雷区,一个记录雷,一个展示给玩家 char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; //初始化两个雷区 Init_board(mine, ROWS, COLS, '0' ); Init_board(show, ROWS, COLS, '*' ); //打印雷区 Prin_board(show, ROW, COL); //布置地雷 PlaceMine(mine, ROWS, COLS); //开始扫雷 FoundMine(mine, show, ROW, COL); } int main() { srand ((unsigned int ) time (NULL)); //打印菜单 int input = 0; printf ( "**********************************\n" ); printf ( "******* 1.play 0.exit *******\n" ); printf ( "**********************************\n" ); do { printf ( "请选择:\n" ); scanf ( "%d" , &input); switch (input) { case 1: Game(); printf ( "再来一局请输入1,退出请按0\n" ); break ; case 0: printf ( "退出游戏\n" ); break ; default : printf ( "请输入1或0:\n" ); break ; } } while (input); return 0; } |
Game.h代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include<stdio.h> #include<time.h> #include<stdlib.h> #define ROWS 11 #define COLS 11 #define ROW ROWS - 2 #define COL COLS - 2 #define EASY 10 void Init_board( char board[ROWS][COLS], int rows, int cols, char set); void Prin_board( char board[ROWS][COLS], int row, int col); void PlaceMine( char board[ROWS][COLS], int row, int col); void FoundMine( char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col); |
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
|
#define _CRT_SECURE_NO_WARNINGS #include"Game.h" int Sum( char board[ROWS][COLS], int x, int y) { int tem = y; //备份y的值,方便循环中初始化y值 //(x-1,y-1) (x-1,y) (x-1,y+1) //(x,y-1) (x,y) (x,y+1) //(x+1,y-1) (x+1,y) (x+1,y+1) int i = 0, j = 0; int sum = 0; for (i = 0; i < 3; i++, x++) { for (j = 0, y = tem; j < 3; j++, y++) { sum += board[x - 1][y - 1]; } } sum = sum - 8 * ( int ) '0' ; return sum; } void Init_board( char board[ROWS][COLS], int rows, int cols, char set) { int i = 0, j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = set; } } } void Prin_board( char board[ROWS][COLS], int row, int col) { int i = 0, j = 0; //打印列号 for (i = 0; i < 10; i++) { printf ( "%d " , i); if (i == 0) printf ( " " ); } printf ( "\n\n" ); for (i = 1; i <= row; i++) { printf ( "%d " , i); //打印行号 for (j = 1; j <= col; j++) { printf ( "%c " , board[i][j]); } printf ( "\n" ); } } void PlaceMine( char board[ROWS][COLS], int row, int col) { int count = 20; while (count) //控制地雷个数 { int x = 0, y = 0; x = rand () % 9 + 1; y = rand () % 9 + 1; board[x][y] = '1' ; count--; } } void FoundMine( char board1[ROWS][COLS], char board2[ROWS][COLS], int row, int col) { int x = 0, y = 0; //判断输入坐标是否合法 while (1) { printf ( "请输入排雷的坐标:\n" ); scanf ( "%d%d" , &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (board1[x][y] == '1' ) { printf ( "游戏结束,你踩雷了\n" ); break ; } else { board2[x][y] = Sum(board1, x, y); Prin_board(board2, ROW, COL); } } else printf ( "坐标非法\n" ); } if (board1[x][y] != '1' ) printf ( "恭喜你完成游戏!\n" ); } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/YouMMo/article/details/112728212