本文实例为大家分享了C语言实现扫雷游戏的具体代码,供大家参考,具体内容如下
主函数:main.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 "game.h" void Menu() { printf ( "##########################\n" ); printf ( "##1.play 0.exit##########\n" ); printf ( "##########################\n" ); printf ( "## Please Enter select! ##\n" ); } int main() { Menu(); srand ((unsigned int ) time (NULL)); int quit = 0; while (!quit) { int select = 0; printf ( "请输入你的选择:\n" ); scanf ( "%d" , &select); switch (select) { case 1: game(); break ; case 2: quit = 1; break ; default : printf ( "你输入有误,请重新输入:\n" ); break ; } } printf ( "Bye Bye!\n" ); 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
|
#include "game.h" void game() { char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; memset (mine, '0' , sizeof (mine)); //初始化数组置为0 memset (show, '*' , sizeof (show)); //初始化数组置为* int no_y, no_x; set_mine(mine,ROWS,COLS,&no_x,&no_y); //布雷 ‘1'表示雷 int x = 0; int y = 0; int time = 100 - NUM; while ( time > 0) { system ( "cls" ); //清屏 Show(show, ROWS, COLS); //打印 棋盘 printf ( "请输入坐标:\n" ); scanf ( "%d%d" , &x, &y); if (x<1 || x>10 || y<1 || y>10) { printf ( "你输入有误,请重新输入:\n" ); continue ; } if (show[x][y] != '*' ) { printf ( "你输入有误,请重新输入:\n" ); continue ; } if (mine[x][y] == '1' ) { if ( time == 80) //如果第一次有雷,用一个没雷的与这个交换 { mine[x][y] = '0' ; mine[no_y][no_y] = '1' ; } else { printf ( "game over!\n" ); Show(mine, ROWS, COLS); break ; } } show[x][y] = get_mine_count(mine, x, y) + '0' ; Expand(mine, show, x, y); time --; } } void set_mine( char mine[ROWS][COLS], int col, int row, int *no_x, int *no_y) //声明布雷函数 { int count = NUM; //设置计数器,统计布雷的个数 while (count > 0) { int x = rand () % (col-2) + 1; int y = rand () % (col-2) + 1; if ((mine[x][y]) == '0' ) { mine[x][y] = '1' ; count--; } } for ( int i = 1; i <= 10; i++) { for ( int j = 1; i <= 10; j++) { if (mine[i][j] == '0' ) { no_x = i; no_y = j; return ; } } } } void Show( char mine[ROWS][COLS], int row, int col) //声明打印棋盘函数 { int i = 0; int j = 0; printf ( " " ); for (i = 1; i <= 10 ; i++) { printf ( "%2d |" , i); } printf ( "\n" ); for (i = 1; i <= 11; i++) { printf ( "----" ); } printf ( "\n" ); for (i = 1; i <= 10 ; i++) { printf ( "%2d |" , i); for (j = 1; j <= 10; j++) { printf ( "%2c |" , mine[i][j]); } printf ( "\n" ); for ( int i = 1; i <= 11; i++) { printf ( "----" ); } printf ( "\n" ); } } int get_mine_count( char mine[ROWS][COLS], int x, int y) //雷数统计 { return mine[x - 1][y - 1]+ mine[x][y - 1] +\ mine[x + 1][y - 1]+ mine[x + 1][y]\ + mine[x + 1][y + 1]+ mine[x][y + 1] + \ + mine[x - 1][y + 1]+ mine[x - 1][y] - 8 * '0' ; } void Expand( char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y) { if (x >= 1 && x <= 10 && y >= 1 && y <= 10) { if (get_mine_count(mine, x, y) + '0' == '0' ) //表示x,y周围没雷 { show[x][y] = '0' ; if (show[x - 1][y - 1] == '*' ) { Expand(mine, show, x - 1, y - 1); } if (show[x - 1][y] == '*' ) { Expand(mine, show, x - 1, y ); } if (show[x - 1][y + 1] == '*' ) { Expand(mine, show, x - 1, y + 1); } if (show[x ][y - 1] == '*' ) { Expand(mine, show, x , y - 1); } if (show[x][y + 1] == '*' ) { Expand(mine, show, x , y + 1); } if (show[x + 1][y - 1] == '*' ) { Expand(mine, show, x + 1, y - 1); } if (show[x + 1][y] == '*' ) { Expand(mine, show, x + 1, y ); } if (show[x + 1][y + 1] == '*' ) { Expand(mine, show, x + 1, y + 1); } } } } |
函数声明:game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#ifndef _GAME_H_ #define _GAME_H_ #include<stdio.h> #include<Windows.h> #pragma warning(disable:4996) #include<time.h> #include<string.h> #define ROWS 12 #define COLS 12 #define NUM 20 //雷数 void game(); void set_mine( char mine[ROWS][COLS], int row, int col, int *no_x, int *no_y); void Show( char mine[ROWS][COLS], int row, int col); int get_mine_count( char mine[ROWS][COLS], int x, int y); void Expand( char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y); #endif |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Stephen_curry_66/article/details/102870476