C语言实现简单的扫雷游戏操作

时间:2022-05-31 08:03:24

扫雷小游戏的代码实现,供大家参考,具体内容如下

C语言实现简单的扫雷游戏操作

编译器使用的为VS2013

代码分为三个部分

1、头文件 game.h
2、源文件 game.c 用来存放游戏中所有的函数
3、源文件 test.c 用来存放游戏的主题部分

代码如下:

game.h部分

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
 
#define EASY_COUNT 5  //存放雷的个数,此处为5,可任意改变
#define ROW 5 //行
#define COL 5 //列
 
#define ROWS ROW+2
#define COLS COL+2
 
//初始化棋盘,将雷设计成字符‘1' ,注意不是数字1,没有雷设计成字符‘0',此处设计是为后续统计雷的个数做铺垫
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, int count);
//排雷
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
#include "game.h"
 
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
 int i = 0;
 for (i = 1; i <= rows - 2; i++)
 {
 int j = 0;
 for (j = 1; j <= cols - 2; j++)
 {
  board[i][j] = set;
 }
 }
}
 
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
 int i = 0;
 printf("----------扫雷游戏----------\n");
 for (i = 0; i <= col; i++)
 {
 printf("%d ", i);
 }
 printf("\n");
 for (i = 1; i <= row; i++)
 {
 int j = 0;
 printf("%d ", i);
 for (j = 1; j <= col; j++)
 {
  printf("%c ", board[i][j]);
 }
 printf("\n");
 }
 printf("----------扫雷游戏----------\n");
}
 
void SetMine(char mine[ROW][COL], int row, int col, int count)
{
 while (count)
 {
 int x = rand() % row + 1;
 int y = rand() % col + 1;
 if (mine[x][y] == '0')
 {
 
 //x,y坐标出没有雷
  mine[x][y] = '1';
  count--;
 }
 }
}
 
int GetMineCount(char mine[ROWS][COLS], int x, int y)//统计雷时,只需将该坐标周围的8个位置坐标是否有雷计算一下即可,有就加字符‘1',没有加字符‘0'等同于不加
{
 int i = 0;
 int count = 0;
 for (i = x - 1; i <= x + 1; i++)
 {
 int j = 0;
 for (j = y - 1; j <= y + 1; j++)
 {
  if (mine[i][j] == '1')
  {
  count++;
  }
 }
 }
 return count;
}
//对雷的情况进行排查
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
 int win = 0;
 while (win < row * col - EASY_COUNT)
 {
 int x;
 int y;
 printf("请输入要排查的坐标:");
 scanf("%d%d", &x, &y);
 // 1.先判断坐标是否非法,即可能出现的越界行为
 // 2.坐标合法,判断该处坐标是不是雷,如果是,则游戏结束
 if (x >= 1 && x <= row &&y >= 1 && y <= col)
 {
  if (mine[x][y] == '1')
  {
  printf("很遗憾,你被炸死了!!!\n");
  DisplayBoard(mine, row, col);
  break;
  }
  else //坐标合法时,且该坐标不是雷时,统计该坐标处雷的个数
  {
  int count = GetMineCount(mine, x, y);//设计GetMineCount函数,统计该坐标处雷的个数
  show[x][y] = count + '0';
  //将该坐标周围雷的信息打印在棋盘上
  DisplayBoard(show, row, col);
  win++;
  }
 }
 else
 {
  printf("坐标非法,请重新输入!!!\n");
 }
 }
 if (row*col - EASY_COUNT == win)
 {
 printf("恭喜你,你赢了,扫雷成功!!!\n");
 printf("雷的布局为:\n");
 DisplayBoard(show, 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "game.h"
void menu()
{
 printf("***************************\n");
 printf("*******  1.PLAY  ********\n");
 printf("*******  2.EXIT  ********\n");
 printf("***************************\n");
}
void game()
{
 //创建棋盘对应的数组
 char mine[ROWS][COLS];
 char show[ROWS][COLS];
 
 InitBoard(mine, ROWS, COLS, '0');//存放布置雷的信息
 InitBoard(show, ROWS, COLS, '*');//存放排查出雷的信息
 
 //DisplayBoard(mine, ROW, COL);
 DisplayBoard(show, ROW, COL);
 //布置雷
 SetMine(mine, ROW, COL, EASY_COUNT);
 
 //DisplayBoard(mine, ROW, COL);
 FindMine(mine, show, ROW, COL);
 
}
 
int main()
{
 int input;
 srand((unsigned int)time(NULL));
 do
 {
 
 menu();
 printf("请输入 1 开始游戏 输入 0 退出游戏> \n");
 scanf("%d", &input);
 switch (input)
 {
 case 1:
  game();//扫雷游戏的实现
  break;
 case 0:
  printf("退出游戏\n");
  break;
 default:
  printf("选择错误,请重新选择\n");
 
 }
 } while (input);
 return 0;
}

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

原文链接:https://blog.csdn.net/m0_52089596/article/details/114223251