服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C语言简单实现扫雷小游戏

C语言简单实现扫雷小游戏

2021-09-28 10:47某乔姓市民 C/C++

这篇文章主要为大家详细介绍了C语言简单实现扫雷小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言简单实现扫雷小游戏 的具体代码,供大家参考,具体内容如下

游戏规则:

以9*9棋盘为例,棋盘上随机分布着10个地雷,玩家在棋盘上进行点击,如果被点击的格子是地雷,则玩家被炸“死”,游戏结束;如果被点击的格子上没有地雷,与被点击的格子相邻的格子(被点击格子的上下左右还有斜向,共八个格子)有地雷,则在被点击的格子上显示这些地雷的总数,如果与被点击的格子相邻的八个格子都没有地雷,则棋盘自动展开,直到与展开的格子相邻的格子有地雷才停止。此时最后被展开的格子显示其相邻格子共有的地雷数。

代码采用模块化思想:

C语言简单实现扫雷小游戏

game.h 模块:

存放 头文件和函数声明

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define _CRT_SECURE_NO_WARNINGS 1
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#define EASY_COUNT 10
#define ROW 9
#define COL 9
 
#define ROWS ROW+2
#define COLS COL+2
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);
 
void SetMine(char mine[ROWS][COLS], int row, int col);
 
void FindMine(char mine[ROWS][COLS], char show[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
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
#include "game.h"
 
void menu()
{
 printf("**********************\n");
 printf("**** 1. play  ***\n");
 printf("**** 0. exit  ***\n");
 printf("**********************\n");
}
 
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
 int i = 0;
 int j = 0;
 
 for (i = 0; i < rows; i++)
 {
 for (j = 0; j < cols; j++)
 {
 board[i][j] = set;
 }
 }
}
 
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
 int i = 0;
 int j = 0;
 printf("----------------------------------\n");
 //列号的打印
 for (i = 0; i <= col; i++)
 {
 printf("%d ", i);
 }
 printf("\n");
 for (i = 1; i <= row; i++)
 {
 //每一行最前面的行号
 printf("%d ", i);
 for (j = 1; j <= col; j++)
 {
 printf("%c ", board[i][j]);
 }
 printf("\n");
 }
 printf("----------------------------------\n");
 
}
 
void SetMine(char board[ROWS][COLS], int row, int col)
{
 //1. 随机找坐标布置雷
 //布置多少个雷 - 10
 int count = EASY_COUNT;
 while (count)
 {
 //布置成功一个雷,count--
 //1. 生产随机的坐标
 int x = rand()%row+1;//1-9
 int y = rand()%col+1;//1-9
 //2. 布置
 if (board[x][y] == '0')
 {
 board[x][y] = '1';
 count--;
 }
 }
}
 
 
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
 return mine[x - 1][y] +
 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]-8*'0';
}
 
//
//扫雷游戏是怎么结束的?
//1. 炸死了
//2. 正常排查了所有不是雷的位置
//
void FindMine(char mine[ROWS][COLS],
  char show[ROWS][COLS],
  int row,
  int col)
{
 int x = 0;
 int y = 0;
 int win = 0;
 
 while (win<ROW*COL - EASY_COUNT)
 {
 printf("请输入要排查的坐标:>");
 scanf("%d%d", &x, &y);
 if (x >= 1 && x <= row && y >= 1 && y <= col)
 {
 //判断x,y坐标处是否是雷
 if (mine[x][y] == '1')
 {
 printf("很遗憾,你被炸死了\n");
 DisplayBoard(mine, row, col);
 break;
 }
 else
 {
 //如果x,y坐标不是雷,就统计周围有几个雷
 int count = GetMineCount(mine, x, y);
 show[x][y] = count + '0';
 DisplayBoard(show, ROW, COL);
 win++;
 }
 }
 else
 {
 printf("坐标非法\n");
 }
 }
 if (win == ROW*COL - EASY_COUNT)
 {
 printf("恭喜你,排雷成功\n");
 DisplayBoard(mine, row, col);
 }
}
 
void game()
{
 //真正扫雷的过程
 //创建2个数组
 //存放布置好的雷
 char mine[ROWS][COLS] = {0};//'0'
 //存放排查出来的雷的信息
 char show[ROWS][COLS] = {0};//'*'
 InitBoard(mine, ROWS, COLS, '0');
 InitBoard(show, ROWS, COLS, '*');
 DisplayBoard(show, ROW, COL);
 //布置好的雷的信息不应该轻易打印
 //DisplayBoard(mine, ROW, COL);
 //1. 布置雷
 SetMine(mine, ROW, COL);
 DisplayBoard(mine, ROW, COL);
 //2. 扫雷
 FindMine(mine, show, ROW, COL);
}

test.c 模块:

程序的入口,写main函数,用来调用本次游戏运行所需的函数

?
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
#include "game.h"
 
int main()
{
 int input = 0;
 //time_t -- 整形
 srand((unsigned int)time(NULL));//设置随机数的生成起点的
 
 do
 {
 menu();
 printf("请选择:>");
 scanf("%d", &input);
 switch (input)
 {
 case 1:
 game();//扫雷游戏
 break;
 case 0:
 printf("退出游戏\n");
 break;
 default:
 printf("选择错误!\n");
 break;
 }
 //
 } while (input);
 return 0;
}

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

原文链接:https://blog.csdn.net/qq_45658339/article/details/108690857

延伸 · 阅读

精彩推荐