当前位置: 首页 > news >正文

C语言 ——— 在控制台实现扫雷游戏(一次展开一片,递归实现)

前言

两个数组,一个用来显示在控制台上,一个用来存放雷

两个数组的实际大小为11 * 11 ,而为了方便排查雷的个数,实际使用范围是9 * 9 


test.c 

#include"mine_sweeping.h"void game()
{// 存放雷char mine[ROWS][COLS];// 展示char show[ROWS][COLS];// 初始化棋盘InitBoard(mine, ROWS, COLS, '0');InitBoard(show, ROWS, COLS, '*');// 打印棋盘DisplayBoard(show, ROW, COL);// 布置雷SetMine(mine, ROW, COL);// 排查雷FindMine(mine, show, ROW, COL);
}int main()
{int input = 0;// 随机数生成器srand((unsigned int)time(NULL));do{menu();printf("请输入:");scanf("%d", &input);if (input == 1){game();}else if (input == 0){printf("退出游戏\n");}else{printf("输入错误,请重新输入\n");}} while (input);return 0;
}

game.h

#include<stdio.h>
#include<stdlib.h>
#include<time.h>#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
#define MINE ((ROW + COL) / 2)// 打印菜单
void menu();// 初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char str);// 打印棋盘
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);

game.c

#include"mine_sweeping.h"// 打印菜单
void menu()
{printf("*******************************************\n");printf("*****     1.play           0.next     *****\n");printf("*******************************************\n");
}// 初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{for (int i = 0; i < rows; i++){for (int j = 0; j < cols; j++){board[i][j] = set;}}
}// 打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{printf("\n");//printf("--------------mine game--------------\n");// 打印横坐标for (int i = 1; i <= row; i++){printf(" %d ", i);printf(" ");}printf("\n");for (int i = 1; i <= row; i++){for (int j = 1; j <= col; j++){// 打印每一行printf(" %c ", board[i][j]);if (j < row){printf("|");}}// 打印纵坐标printf(" %d\n", i);// 打印分割线if (i < col){for (int k = 0; k < row; k++){printf("---");if (k < row - 1){printf("|");}}printf("\n");}}printf("\n");
}// 布置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{// 雷的个数int mine = MINE;while (mine){int x = rand() % row + 1;int y = rand() % col + 1;if (board[x][y] == '0'){board[x][y] = '1';mine--;}}
}// 统计雷的个数
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');int count = 0;if (mine[x - 1][y] == '1')count++;if (mine[x - 1][y - 1] == '1')count++;if (mine[x][y - 1] == '1')count++;if (mine[x + 1][y - 1] == '1')count++;if (mine[x + 1][y] == '1')count++;if (mine[x + 1][y + 1] == '1')count++;if (mine[x][y + 1] == '1')count++;if (mine[x - 1][y + 1] == '1')count++;return count;
}// 一次展开一片
void UnfoldSlice(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{if (x >= 1 && x <= ROW && y >= 1 && y <= COL){int count = GetMineCount(mine, x, y);if (count == 0 && mine[x][y] != '*'){show[x][y] = count + '0';mine[x][y] = '*';UnfoldSlice(mine, show, x - 1, y);UnfoldSlice(mine, show, x - 1, y - 1);UnfoldSlice(mine, show, x, y - 1);UnfoldSlice(mine, show, x + 1, y - 1);UnfoldSlice(mine, show, x + 1, y);UnfoldSlice(mine, show, x + 1, y + 1);UnfoldSlice(mine, show, x, y + 1);UnfoldSlice(mine, show, x - 1, y + 1);}if (count != 0){show[x][y] = count + '0';return;}}else{return;}}int IsWin(char show[ROWS][COLS], int row, int col)
{int count = 0;for (int i = 1; i <= row; i++){for (int j = 1; j <= col; j++){if (show[i][j] == '*')count++;}}return count;}// 排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{int x = 0;int y = 0;int MineCount = MINE;while (IsWin(show, row, col) > MineCount){printf("请输入要排查雷的坐标(用空格隔开):");scanf("%d %d", &x, &y);if (x >= 1 && x <= row && y >= 1 && y <= col){if (mine[x][y] == '1'){printf("很遗憾,你被炸死了,游戏结束\n");DisplayBoard(mine, ROW, COL);break;}else{// 一次展开一片UnfoldSlice(mine, show, x, y);DisplayBoard(show, ROW, COL);}}else{printf("坐标非法,请重新输入\n");}}if (IsWin(show, row, col) == MINE){printf("恭喜你,排雷成功\n");DisplayBoard(mine, ROW, COL);}
}
http://www.lryc.cn/news/416754.html

相关文章:

  • el7升级Apache模块编译
  • Linux系统下的日志管理与ELK Stack实践
  • C++入门基础知识
  • Python爬虫技术 第28节 数据可视化
  • react中的装饰器
  • Elasticsearch:用例、架构和 6 个最佳实践
  • tcp常用网络接口 linux环境
  • 第10节课:JavaScript基础——网页交互的魔法
  • springboot+vue+mybatis汽车租赁管理+PPT+论文+讲解+售后
  • .NET C# 将文件夹压缩至 zip
  • 软考基本介绍
  • 【Vue】vue3 中使用 ResizeObserver 监听元素的尺寸宽度变化
  • 信息安全专业好吗?
  • 梧桐数据库(WuTongDB):数据库中元数据表的常见信息
  • 在 Linux 9 上安装 Oracle 19c:克服兼容性问题 (INS-08101)
  • 【踩坑】pytorch中的索引与copy_结合不会复制数据及其解决方案
  • 十六、【Python】基础教程 - 【Flask】网络编程开发
  • C#初级——List 容器
  • serial靶机教程
  • 【Linux-MISC设备】
  • 【随笔】VRRP+MSTP
  • vue 动态增删行,并form表单校验(附v2\v3)
  • 计算机网络的基本概念
  • Python 爬虫项目实战三:GitHub 用户信息抓取与分析
  • xtrabackup搭建MySQL 8.0 主从复制
  • Java程序员接单分享
  • 【HarmonyOS NEXT星河版开发学习】小型测试案例01-今日头条置顶练习
  • C语言----计算开机时间
  • 批发行业进销存-登录适配 android 横竖屏幕 源码CyberWinApp-SAAS 本地化及未来之窗行业应用跨平台架构
  • js功能(1)