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

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

服务器之家 - 编程语言 - C/C++ - C语言实现扫雷游戏(初级版)

C语言实现扫雷游戏(初级版)

2021-09-28 10:49sugar high C/C++

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

本文实例为大家分享了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<stdlib.h>
#include<time.h>
 
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
 
#define EASY 10
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);
void PrintBoard(char board[ROWS][COLS], int row, int col);
 
//放雷
void SetMine(char board[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
#include"game.h"
 
//初始化棋盘
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 PrintBoard(char board[ROWS][COLS], int row, int col)
{
 int i = 1;
 int j = 1;
 printf("-------------------\n");
 for (i = 0; i <= row; 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)
{
 int count = EASY;
 while (count)
 {
 int x = rand() % 9 + 1;//1-9
 int y = rand() % 9 + 1;//1-9
 if (board[x][y] == '0')
 {
  board[x][y] = '1';
  count--;
 }
 }
}
 
//查坐标四周雷的个数
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
 //方法1:
 /*int i = 0;
 int j = 0;
 int count = 0;
 for (i = x - 1; i <= x + 1; i++)
 {
 for (j = y - 1; j <= y + 1; j++)
 {
  if (mine[i][j] == '1')
  {
  count++;
  }
 }
 }
 return count;*/
 //方法2:
 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] -
 8 * '0';
}
 
//扫雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
 int num = 0;
 while (num < row*col - EASY)
 {
 int x = 0;
 int y = 0;
 printf("请输入坐标:>");
 scanf("%d%d", &x, &y);
 //判断坐标是否合法
 if (x >= 1 && x <= row&&y >= 1 && y <= col)
 {
  if (mine[x][y] == '0')
  {
  int count = GetMineCount(mine, x, y);
  show[x][y] = count+'0';
  PrintBoard(show, row, col);
  num++;
  }
  else
  {
  printf("很遗憾,您已被炸死!\n");
  PrintBoard(mine, row, col);
  break;
  }
 }
 else
 {
  printf("坐标不合法\n");
 }
 if (num == row*col - EASY)
 {
  printf("排雷成功!\n");
  break;
 }
 }
 
}

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
53
54
55
56
57
58
#include"game.h"
 
void menu()
{
 printf("*****************************\n");
 printf("********* 1. play *********\n");
 printf("********* 0. exit *********\n");
 printf("*****************************\n");
}
 
//游戏设计部分
void game()
{
 //隐藏
 char mine[ROWS][COLS] = { 0 };
 //展示
 char show[ROWS][COLS] = { 0 };
 srand((unsigned int)time(NULL));//设置随机数生成起点
 
 //初始化棋盘
 InitBoard(mine, ROWS, COLS, '0');
 InitBoard(show, ROWS, COLS, '*');
 
 //PrintBoard(mine, ROW, COL);//无需打印
 PrintBoard(show, ROW, COL);
 
 //1.放雷
 SetMine(mine, ROW, COL);
 //PrintBoard(mine, ROW, COL);//test
 
 //2.扫雷
 FindMine(mine, show, ROW, COL);
 
}
 
int main()
{
 int input = 0;
 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;
}

输出结果:

C语言实现扫雷游戏(初级版)

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

原文链接:https://blog.csdn.net/weixin_46429649/article/details/108695273

延伸 · 阅读

精彩推荐