gpt4 book ai didi

C语言实现扫雷游戏(可展开)

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章C语言实现扫雷游戏(可展开)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

# 1、游戏的思路

先理清游戏大概需要实现的功能,菜单功能的实现、棋盘初始化、打印棋盘、布置雷等。运用两个数组,一个放入布置雷的信息,另一个放入排查雷的信息。选一个坐标扫雷,坐标有雷则游戏结束,没有就计算选中坐标的周围8个格子中雷的总数放入选中的坐标中,若选中的坐标周围8个格子中都没有雷则自动展开。考虑到棋盘边框的情况,实际数组要比打印出的棋盘多两行两列。下面是宏定义和函数声明:

ROW、COL为打印行、列,ROWS、COLS为实际的数组行列 EASY_COUNT为雷的个数,可根据需要调整行列和雷的个数 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#define ROW 9
#define COL 9
 
#define ROWS ROW+2
#define COLS COL+2
 
#define EASY_COUNT 10
 
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
 
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 board[ROWS][COLS], int row, int col);
void FindMine( char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void ExcludeMine( char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);

2、游戏测试

游戏实现的大致思路体现和菜单的实现,代码如下:

?
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
#define _CRT_SECURE_NO_WARNINGS 1
 
#include"game.h"
 
void menu()
{
  printf ( "##############################\n" );
  printf ( "###### 1. play 0.exit ######\n" );
  printf ( "##############################\n" );
}
 
//布置雷 - 字符组存储 - 雷用1表示,非雷用0表示 - 最外层一圈放0
//排查雷 - 为避免歧义,再用一个字符组存储排查出来的雷的信息 - 未排除的用#表示
//最外层加一圈字符,只在中间设置雷,并打印展示棋盘中间位置,因此实际存放数组要比打印的棋盘多两行两列
void game()
{
  //雷的信息存储
  //1.布置好的雷的信息
  char mine[ROWS][COLS] = { 0 };
  //2.排查出的雷的信息
  char show[ROWS][COLS] = { 0 };
  //初始化
  InitBoard(mine, ROWS, COLS, '0' );
  InitBoard(show, ROWS, COLS, '#' );
  //打印棋盘
  //DisplayBoard(mine, ROW, COL);//测试使用
  DisplayBoard(show, ROW, COL);
  //布置雷
  SetMine(mine, ROW, COL);
  //DisplayBoard(mine, ROW, COL);//测试使用
  //扫雷
  FindMine(mine, show, ROW, COL);
}
 
void test()
{
  int input = 0;
  srand ((unsigned int ) time (NULL));
  do
  {
  menu();
  printf ( "请选择:>" );
  scanf ( "%d" , &input);
  switch (input)
  {
  case 1:
  game();
  printf ( "将返回主菜单\n" );
  Sleep(5 * 1000);
  break ;
  case 0:
  printf ( "退出游戏\n" );
  break ;
  default :
  printf ( "选择错误,请重新选择\n" );
  break ;
  }
  } while (input);
}
 
int main()
{
  test();
  return 0;
}

3、游戏流程

存放函数的源文件需要引用头文件 。

?
1
2
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

1.初始化棋盘 。

?
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;
  }
  }
}

2.棋盘打印 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void DisplayBoard( char board[ROWS][COLS], int row, int col)
{
  int i = 0;
  int j = 0;
  //打印列号
  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" );
  }
}

3.布置雷 。

?
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-9
  int y = rand () % col + 1;
  if (board[x][y] == '0' )
  {
  board[x][y] = '1' ;
  count--;
  }
  }
}

4.查找雷和胜负判断 。

?
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
int CheckShow( char show[ROWS][COLS], int row, int col)
{
  int win = 0;
  int i = 0;
  int j = 0;
  for (i = 1; i <= row; i++)
  {
  for (j = 1; j <= col; j++)
  {
  if (show[i][j] == '#' )
  win++;
  }
  }
  return win;
}
 
void FindMine( char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
  int x = 0;
  int y = 0;
  int win = 0;
  //9*9 - 10 = 71
  while (1)
  {
  printf ( "请输入排查雷的坐标:>" );
  scanf ( "%d%d" , &x, &y);
  if (x >= 1 && x <= row && y >= 1 && y <= col)
  {
  //坐标合法
  //1.踩雷
  if (mine[x][y] == '1' )
  {
  printf ( "很遗憾,你被炸死了\n" );
  DisplayBoard(mine, row, col);
  break ;
  }
  else //不是雷
  {
  //计算x,y坐标周围有几个雷
  ExcludeMine(mine, show, x, y);
  DisplayBoard(show, row, col);
  win = CheckShow(show, row, col);
  if (win == EASY_COUNT)
   break ;
  }
  }
  else
  {
  printf ( "坐标非法,请重新输入!\n" );
  }
  }
  if (win == EASY_COUNT)
  {
  printf ( "恭喜你,排雷成功\n" );
  DisplayBoard(mine, row, col);
  }
}

5.扫雷的展开和提醒 。

?
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
//'1' - '0' = 1
int get_mine_count( 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' ;
}
 
void ExcludeMine( char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
  int count = get_mine_count(mine, x, y);
  if (count != 0)
  {
  show[x][y] = count + '0' ;
  }
  else
  {
  show[x][y] = ' ' ;
  if (show[x - 1][y] == '#' )
  ExcludeMine(mine, show, x - 1, y);
  if (show[x - 1][y - 1] == '#' )
  ExcludeMine(mine, show, x - 1, y - 1);
  if (show[x][y - 1] == '#' )
  ExcludeMine(mine, show, x, y - 1);
  if (show[x + 1][y - 1] == '#' )
  ExcludeMine(mine, show, x + 1, y - 1);
  if (show[x + 1][y] == '#' )
  ExcludeMine(mine, show, x + 1, y);
  if (show[x + 1][y + 1] == '#' )
  ExcludeMine(mine, show, x + 1, y + 1);
  if (show[x][y + 1] == '#' )
  ExcludeMine(mine, show, x, y + 1);
  if (show[x - 1][y + 1] == '#' )
  ExcludeMine(mine, show, x - 1, y + 1);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

原文链接:https://blog.csdn.net/Flora_26/article/details/114266700 。

最后此篇关于C语言实现扫雷游戏(可展开)的文章就讲到这里了,如果你想了解更多关于C语言实现扫雷游戏(可展开)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com