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

时间:2022-12-03 18:39:02

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

设计思路

1. 定义两个二维数组,一个存放炸弹信息,一个隐藏信息后向玩家展示。

2. 玩家每一次选择都要判断是否踩到炸弹,如果踩雷,将结束游戏,否则继续游戏。

3.玩家每一次判断后要将新棋盘展示给玩家,且将该位置附近雷的个数展示出来。

4.最后如果玩家找到所有非雷区时,玩家获胜。

首先,我们设计一个简单的主函数

方便玩家*选择进入游戏与退出游戏

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
 int input = 0;
    srand((unsigned int)time(NULL));//随机数用于随机存放炸弹
 do
 {
  menu();//打印菜单
  printf("请选择>\n");
  scanf("%d", &input);
  switch (input)
  {case 1:
   game();
   break;
  case 0:
   break;
  default:
   printf("输入错误,重新输入>\n");
    break;
  }
 } while (input);
}

菜单我们可以*设计,这里是我设计的一个简单菜单:

?
1
2
3
4
5
6
7
void menu()
{
 printf("******************************\n");
 printf("********** 1. play  **********\n");
 printf("********** 0. exit  **********\n");
 printf("******************************\n");
}

敲重点 

接下来就开始了我们最关键的部分:游戏函数设计

?
1
2
3
4
5
6
7
8
9
10
11
12
void game()
{
 char mine[ROWS][COLS] = { 0 };//存放雷的信息
 char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息
 InitBoard(mine, ROWS, COLS, '0');//初始化棋盘
 InitBoard(show, ROWS, COLS, '*');//
 DisplayBoard(show, ROWS, COLS);
 //布置雷
 SetMine(mine, ROW, COL);
  //排查雷
  FindMine(mine,show,ROW,COL);
}

这里我们定义了两个二维数组,mine和show,其中mine用于我们储存炸弹的信息,而show则用于向玩家展示游戏棋盘

一开始我们要考虑我们的棋盘大小与数组大小的关系

棋盘的大小就等于我们创建的数组大小吗? 等于吗?啊哈?

当然不等于了,想什么呢小伙子!!!

我们可是要考虑每一个位置周围的炸弹数量的,你让我最外层的怎么办!

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

于是我们定义了以下几个常数用于定义棋盘的长度宽度,还有多了一圈的数组

?
1
2
3
4
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2

接下来就是对数组进行初始化

?
1
2
3
4
5
6
7
8
9
10
11
12
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;
  }
 }
}

打印数组(展示棋盘)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void DisplayBoard(char board[ROWS][COLS], int rows, int cols)
{
 int i = 0;
 int j = 0;
 printf("----------------------------\n");
 printf( "%d ", j);
 for (i = 1; i < rows - 1; i++)
 {
  printf(" %d ", i);
 }
 printf("\n");
 for (i = 1; i < rows - 1; i++) 
 {
  printf("%d ", i);
  for (j = 1; j < cols - 1; j++)
  {
   printf(" %c ", board[i][j]);
  }
  printf("\n");
 }
 printf("----------------------------\n");
}

随机放雷

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void SetMine(char board[ROWS][COLS], int row, int col)
{
 int count = EASY_COUNT;
 while (count)
 {
  int x = rand() % row + 1;//+1可以保证每一个位置都能随机放雷
  int y = rand() % col + 1;
  if (board[x][y] == '0')//在没有放过雷的位置放雷
  {
   board[x][y] = '1';
   count--;
  }
 }
}

排雷函数

?
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
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
 return mine[x-1][y]+mine[x+1][y]+
  mine[x-1][y-1]+mine[x][y-1]+mine[x+1][y-1]+
  mine[x-1][y+1]+mine[x][y+1]+mine[x+1][y+1]-
  8*'0';
}
 
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("请输入要排查的坐标>\n");
  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';//以字符形式
    DisplayBoard(show, ROWS, COLS);
    win++;
   }
   else
   {
    printf("很遗憾,踩到雷了\n");
    DisplayBoard(mine,ROWS,COLS);//向玩家展示所有雷的位置
    break;
   
  }
  else
  {
   printf("输入不合法,请重新输入>\n");
  }
 }
 if (win == row * col - EASY_COUNT)//判断是否排雷成功
 {
  printf("恭喜您,排雷成功\n");
  DisplayBoard(mine, ROWS, COLS);
 }
}

至此我们的扫雷小游戏就制作完成了,可以进行愉快的游戏啦!

下面是三个整体代码

1.   game.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
#include<stdio.h>
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int  rows, int cols,  char set);
//显示棋盘
void DisplayBoard(char board[ROWS][COLS], int rows, int cols);
//布置雷
void SetMine(char board[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROW][COL], char show[ROW][COL], int rows, int cols);

2.   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
#define _CRT_SECURE_NO_WARNINGS
#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 DisplayBoard(char board[ROWS][COLS], int rows, int cols)
{
 int i = 0;
 int j = 0;
 printf("----------------------------\n");
 printf( "%d ", j);
 for (i = 1; i < rows - 1; i++)
 {
  printf(" %d ", i);
 }
 printf("\n");
 for (i = 1; i < rows - 1; i++) 
 {
  printf("%d ", i);
  for (j = 1; j < cols - 1; j++)
  {
   printf(" %c ", board[i][j]);
  }
  printf("\n");
 }
 printf("----------------------------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
 int count = EASY_COUNT;
 while (count)
 {
  int x = rand() % row + 1;
  int y = rand() % col + 1;
  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]+
  mine[x-1][y-1]+mine[x][y-1]+mine[x+1][y-1]+
  mine[x-1][y+1]+mine[x][y+1]+mine[x+1][y+1]-
  8*'0';
}
 
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("请输入要排查的坐标>\n");
  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';
    DisplayBoard(show, ROWS, COLS);
    win++;
   }
   else
   {
    printf("很遗憾,踩到雷了\n");
    DisplayBoard(mine,ROWS,COLS);
    break;
   
  }
  else
  {
   printf("输入不合法,请重新输入>\n");
  }
 }
 if (win == row * col - EASY_COUNT)
 {
  printf("恭喜您,排雷成功\n");
  DisplayBoard(mine, ROWS, COLS);
 }
}

3.  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
#define _CRT_SECURE_NO_WARNINGS
#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 };//存放排查出的雷的信息
 InitBoard(mine, ROWS, COLS, '0');//初始化棋盘
 InitBoard(show, ROWS, COLS, '*');//
 DisplayBoard(show, ROWS, COLS);
 //布置雷
 SetMine(mine, ROW, COL);
 DisplayBoard(mine, ROWS, COLS);
  //排查雷
  FindMine(mine,show,ROW,COL);
}
 
int main()
{
 int input = 0;
 srand((unsigned int)time(NULL));
 do
 {
  menu();
  printf("请选择>\n");
  scanf("%d", &input);
  switch (input)
  {case 1:
   game();
   break;
  case 0:
   break;
  default:
   printf("输入错误,重新输入>\n");
    break;
  }
 } while (input);
 
}

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

原文链接:https://blog.csdn.net/qq_58259923/article/details/119283232