本文实例为大家分享了C语言实现扫雷游戏的具体代码,供大家参考,具体内容如下
简单描述:
输入要要扫的位置.
如果是雷,则游戏失败.
如果将所有不是雷的位置都扫了一遍,则游戏胜利.
主要细节
1、初始化(玩家扫雷图,地雷布局图)
展示出来的地图(玩家能看到的地图)showMap
1
2
3
4
5
6
|
for ( int row = 0; row < ROW; row++){
for ( int col = 0; col < COL; col++){
showMap[row][col] = '*' ;
}
}
|
地雷的布局地图(玩家看不到的地图)mineMap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
for ( int row = 0; row < ROW; row++){
for ( int col = 0; col < COL; col++){
mineMap[row][col] = '*' ;
}
}
int count = 0;
while (count<MINECOUNT){
int row = rand () % 9;
int col = rand () % 9;
mineMap[row][col] = 'L' ;
count++;
}
}
|
3、打印地图
1
2
3
4
5
6
7
8
9
10
|
void print( char Map[ROW][COL]){
printf ( " |0|1|2|3|4|5|6|7|8|\n" );
for ( int row = 0; row < ROW; row++){
printf ( "%d |" , row);
for ( int col = 0; col < COL; col++){
printf ( "%c " , Map[row][col]);
}
printf ( "\n" );
}
}
|
5、玩家输入要扫的位置信息(9*9格子中某一特定位置)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
void Input( char showMap[ROW][COL], char mineMap[ROW][COL], int * row, int * col){
while (1){
printf ( "请输入您要点的位置坐标:" );
scanf ( "%d %d" , row, col);
if (((*row) < 0) || ((*row) >= ROW)
|| ((*col) < 0) || ((*col) >= COL)){
printf ( "输入有误 " );
continue ;
}
if (showMap[*row][*col] == '*' ){
showMap[*row][*col] = ' ' ;
break ;
}
}
}
|
7、判断某一位置是不是地雷
1
2
3
4
5
6
7
|
//返回1表示雷;返回0表示不是雷
int isMine( char mineMap[ROW][COL], int row, int col){
if (mineMap[row][col] == 'L' ){
return 1;
}
return 0;
}
|
9、某一位置附近地雷的个数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
int MineNerborhood( char mineMap[ROW][COL], int row, int col){
if (row < 0 || row >= ROW
|| col < 0 || col >= COL){
return '0' ;
}
int count = 0;
for ( int r = row - 1; r <= row + 1; r++){
for ( int c = col - 1; c <= col + 1; c++){
if (mineMap[r][c] == 'L' ){
count++;
}
}
}
return count + '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
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
|
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define ROW 9
#define COL 9
#define MINECOUNT 10
void Input( char showMap[ROW][COL], char mineMap[ROW][COL], int * row, int * col){
while (1){
printf ( "请输入您要点的位置坐标:" );
scanf ( "%d %d" , row, col);
if (((*row) < 0) || ((*row) >= ROW)
|| ((*col) < 0) || ((*col) >= COL)){
printf ( "输入有误 " );
continue ;
}
if (showMap[*row][*col] == '*' ){
showMap[*row][*col] = ' ' ;
break ;
}
}
}
//返回1表示雷;返回0表示不是雷
int isMine( char mineMap[ROW][COL], int row, int col){
if (mineMap[row][col] == 'L' ){
return 1;
}
return 0;
}
//
void init( char showMap[ROW][COL], char mineMap[ROW][COL]){
for ( int row = 0; row < ROW; row++){
for ( int col = 0; col < COL; col++){
showMap[row][col] = '*' ;
}
}
for ( int row = 0; row < ROW; row++){
for ( int col = 0; col < COL; col++){
mineMap[row][col] = '*' ;
}
}
int count = 0;
while (count<MINECOUNT){
int row = rand () % 9;
int col = rand () % 9;
mineMap[row][col] = 'L' ;
count++;
}
}
void print( char Map[ROW][COL]){
printf ( " |0|1|2|3|4|5|6|7|8|\n" );
for ( int row = 0; row < ROW; row++){
printf ( "%d |" , row);
for ( int col = 0; col < COL; col++){
printf ( "%c " , Map[row][col]);
}
printf ( "\n" );
}
}
//(row,col)附近格子地雷的个数
int MineNerborhood( char mineMap[ROW][COL], int row, int col){
if (row < 0 || row >= ROW
|| col < 0 || col >= COL){
return '0' ;
}
int count = 0;
for ( int r = row - 1; r <= row + 1; r++){
for ( int c = col - 1; c <= col + 1; c++){
if (mineMap[r][c] == 'L' ){
count++;
}
}
}
return count + '0' ;
}
//一局游戏
void game(){
srand ( time (0));
//展示给玩家的地图
char showMap[ROW][COL];
//雷的布局图
char mineMap[ROW][COL];
//对两个地图进行初始化
init(showMap, mineMap);
//print(mineMap);
//玩家已经扫到的不是雷的格子的数目
int notMineCount = 0;
printf ( "扫雷游戏开始啦!\n" );
while (1){
//打印地图
print(showMap);
//输入地点
int row, col;
Input(showMap, mineMap, &row, &col);
//判断是否是雷区
if (isMine(mineMap, row, col) == 1){
printf ( " 大兄弟,踩雷咧!\n给您看看雷的布局\n" );
print(mineMap);
break ;
}
//判断附近雷的情况
showMap[row][col]=MineNerborhood(mineMap, row, col);
notMineCount++;
//判断是否赢了
if (notMineCount == ROW*COL - MINECOUNT){
print(mineMap);
printf ( "恭喜您赢啦!\n" );
break ;
}
}
}
int menu(){
printf ( "===================\n" );
printf ( "1.开始游戏\n" );
printf ( "0.结束游戏\n" );
printf ( "===================\n" );
printf ( "请输入您的选择:" );
int choice;
scanf ( "%d" , &choice);
return choice;
}
int main(){
while (1){
int choice = menu();
if (choice == 1){
game();
} else if (choice==0){
break ;
} else {
printf ( "输入有误,请重新输入\n" );
}
}
system ( "pause" );
return 0;
}
|
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/m0_50527451/article/details/109517806