本文实例为大家分享了C语言实现简易版扫雷的具体代码,供大家参考,具体内容如下
声明
本次扫雷小游戏用多文件来编写。首先,要自定义的一个头文件 mine.h,里面有扫雷程序里面所需要的头文件、常量和所有函数的声明。其次,创建主函数 test.c,用自己喜欢的方式构建一个游戏的框架,最后,创建自定义函数 mine.c,编写各项功能。
设计思路
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
|
int main() { int quit = 0; do { int select = 0; Menu(); scanf ( "%d" , &select); switch (select){ case 1: Game(); printf ( "Game Over ... Again?\n" ); break ; case 2: quit = 1; printf ( "Bye ... \n" ); break ; default : printf ( "Error ... Please Select Again!\n" ); break ; } } while (!quit); system ( "pause" ); return 0; } |
2、根据主函数的需要,先写菜单函数 Menu(),这个函数很简单,只需要 printf 就好,菜单的样子也是自主设计。
1
2
3
4
5
6
7
8
9
|
static void Menu() { printf ( "===========================\n" ); printf ( "| 扫雷 |\n" ); printf ( "| 1.Play |\n" ); printf ( "| 2.Exit |\n" ); printf ( "===========================\n" ); printf ( " Please Select:" ); } |
3、编写Game()函数,在这个函数里我们将要实现扫雷游戏的全部内容。也是一样的,在这个函数里先写一个大的框架,构建需要的函数,然后再根据我们的需求在 mine.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
|
void Game() { srand ((unsigned long ) time (NULL)); char board[ROW][COL]; char mine[ROW][COL]; InitBoard(board, ROW, COL, ' ' ); //' ' InitBoard(mine, ROW, COL, '0' ); //'0' SetMine(mine, ROW, COL); //ShowBoard(board, ROW, COL); //ShowBoard(mine, ROW, COL); int count = (ROW - 2)*(COL - 2) - NUM; do { system ( "cls" ); int x = 0; int y = 0; ShowBoard(board, ROW, COL); printf ( "请输入你要扫的位置-> " ); scanf ( "%d %d" , &x, &y); if (x < 1 || x > 10 || y < 1 || y > 10){ //[1,10] printf ( "你输入的位置是错误的,请重新输入...\n" ); Sleep(1000); continue ; } if (board[x][y] != ' ' ){ printf ( "你输入的位置已经被扫过,请重新输入...\n" ); Sleep(1000); continue ; } if (mine[x][y] == '0' ){ count--; char num = GetNum(mine, x, y); board[x][y] = num; } else { printf ( "你输入的位置(%d, %d),有雷!\n" , x, y); break ; } } while (count > 0); char *result = NULL; if (count > 0){ result = "** 扫雷结束, 你被炸死了! **\n" ; } else { result = "**扫雷结束,恭喜你,成功啦! **\n" ; } printf ( "-------------------------------\n" ); printf ( "%s" , result); printf ( "-------------------------------\n" ); ShowBoard(mine, ROW, COL); } |
4、创建雷盘,还需要记录扫描雷盘。扫雷的面板是通过定义的宏来自定义大小。需要注意的是在算某个坐标的周围有多少个地雷的时候需要扫描他周围8个格子,所以给二维数组多定义一圈,让它们初始化跟内圈一样为‘0',这样扫描的时候就不算那些格子了。
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
|
void InitBoard( char board[][COL], int row, int col, char elem) { int i = 0; for (; i < row; i++){ int j = 0; for (; j < col; j++){ board[i][j] = elem; } } } void ShowBoard( char board[][COL], int row, int col) { int i = 1; printf ( " " ); for (; i <= col - 2; i++){ printf ( " %-2d|" , i); } printf ( "\n" ); for (i = 1; i <= col - 2; i++){ printf ( "%3s" , "----" ); } printf ( "---\n" ); for (i = 1; i <= row - 2; i++){ printf ( "%2d|" , i); int j = 1; for (; j <= col - 2; j++){ printf ( " %-2c|" , board[i][j]); } printf ( "\n" ); for (j = 1; j <= col - 2; j++){ printf ( "%3s" , "----" ); } printf ( "---\n" ); } } char GetNum( char mine[][COL], int x, int y) { return 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] - 7 * '0' ; } |
5、最后我们要一个函数来随机摆放地雷的位置,那么就有了SetMine函数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
void SetMine( char mine[][COL], int row, int col) { int n = NUM; while (n){ int x = rand () % (row - 2) + 1; int y = rand () % (col - 2) + 1; if (mine[x][y] == '1' ){ continue ; } mine[x][y] = '1' ; n--; } } |
这里说一下,因为随机所以需要 rand()库函数,但是调用rand之前,要在 Game()中写一句
1
|
srand ((unsigned long ) time (NULL)); |
源代码
mine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#ifndef _MINE_H_ #define _MINE_H_ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <windows.h> #pragma warning(disable:4996) #define ROW 10 #define COL 10 #define NUM 10 void Game(); void InitBoard( char board[][COL], int row, int col, char elem); void SetMine( char mine[][COL], int row, int col); void ShowBoard( char board[][COL], int row, int col); char GetNum( char mine[][COL], int x, int y); #endif |
mine.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
|
#include "mine.h" void InitBoard( char board[][COL], int row, int col, char elem) { int i = 0; for (; i < row; i++){ int j = 0; for (; j < col; j++){ board[i][j] = elem; } } } void SetMine( char mine[][COL], int row, int col) { int n = NUM; while (n){ int x = rand () % (row - 2) + 1; int y = rand () % (col - 2) + 1; if (mine[x][y] == '1' ){ continue ; } mine[x][y] = '1' ; n--; } } void ShowBoard( char board[][COL], int row, int col) { int i = 1; printf ( " " ); for (; i <= col - 2; i++){ printf ( " %-2d|" , i); } printf ( "\n" ); for (i = 1; i <= col - 2; i++){ printf ( "%3s" , "----" ); } printf ( "---\n" ); for (i = 1; i <= row - 2; i++){ printf ( "%2d|" , i); int j = 1; for (; j <= col - 2; j++){ printf ( " %-2c|" , board[i][j]); } printf ( "\n" ); for (j = 1; j <= col - 2; j++){ printf ( "%3s" , "----" ); } printf ( "---\n" ); } } char GetNum( char mine[][COL], int x, int y) { return 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] - 7 * '0' ; } void Game() { srand ((unsigned long ) time (NULL)); char board[ROW][COL]; char mine[ROW][COL]; InitBoard(board, ROW, COL, ' ' ); //' ' InitBoard(mine, ROW, COL, '0' ); //'0' SetMine(mine, ROW, COL); //ShowBoard(board, ROW, COL); //ShowBoard(mine, ROW, COL); int count = (ROW - 2)*(COL - 2) - NUM; do { system ( "cls" ); int x = 0; int y = 0; ShowBoard(board, ROW, COL); printf ( "请输入你要扫的位置-> " ); scanf ( "%d %d" , &x, &y); if (x < 1 || x > 10 || y < 1 || y > 10){ //[1,10] printf ( "你输入的位置是错误的,请重新输入...\n" ); Sleep(1000); continue ; } if (board[x][y] != ' ' ){ printf ( "你输入的位置已经被扫过,请重新输入...\n" ); Sleep(1000); continue ; } if (mine[x][y] == '0' ){ count--; char num = GetNum(mine, x, y); board[x][y] = num; } else { printf ( "你输入的位置(%d, %d),有雷!\n" , x, y); break ; } } while (count > 0); char *result = NULL; if (count > 0){ result = "** 扫雷结束, 你被炸死了! **\n" ; } else { result = "**扫雷结束,恭喜你,成功啦! **\n" ; } printf ( "-------------------------------\n" ); printf ( "%s" , result); printf ( "-------------------------------\n" ); ShowBoard(mine, ROW, COL); } |
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
|
#include "mine.h" static void Menu() { printf ( "===========================\n" ); printf ( "| 扫雷 |\n" ); printf ( "| 1.Play |\n" ); printf ( "| 2.Exit |\n" ); printf ( "===========================\n" ); printf ( " Please Select:" ); } int main() { int quit = 0; do { int select = 0; Menu(); scanf ( "%d" , &select); switch (select){ case 1: Game(); printf ( "Game Over ... Again?\n" ); break ; case 2: quit = 1; printf ( "Bye ... \n" ); break ; default : printf ( "Error ... Please Select Again!\n" ); break ; } } while (!quit); system ( "pause" ); return 0; } |
总结
总的来说,在编写代码的时候还是得有一个清晰的思路,搭建框架,再细分其中细节,需要什么就去编写什么。代码很简单,但很经典,希望对你有所帮助。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_46653783/article/details/107367374