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

C语言 扫雷程序设计

目录

1.main函数

2.菜单打印menu函数

3.游戏game函数

4.宏定义

5.界面初始化

6.打印界面

7.设置雷

8.统计排查坐标周围雷的个数

9.排查雷

10.总代码

test.c代码

game.h代码

game.c代码

结语:


一个简单的扫雷游戏,通过宏定义可以修改行列的大小以及雷的数量,通过输入坐标选择要排查的位置。

1.main函数

int main()
{//设置随机数的生成srand((unsigned int)time(NULL));int input = 0;do{printf("扫雷游戏\n");menu();printf("请选择(1/0):>");scanf("%d", &input);switch (input){case 1:printf("开始游戏\n");game();break;case 0:printf("退出游戏\n");default:printf("输入错误\n");}}while (input);return 0;
}

2.菜单打印menu函数

//打印菜单
void menu()
{printf("******************\n");printf("*****  1.play  ***\n");printf("*****  0.exit  ***\n");printf("******************\n");}

3.游戏game函数

void game()
{//布置好雷的信息char mine[ROWS][COLS] = {0};//排查出雷的信息char show[ROWS][COLS] = {0};//初始化数组的内容为指定内容//mine数组在没有布置雷的时候,都是‘0’board_Init(mine,ROWS,COLS,'0');//show数组在没有排查雷的时候都是‘*’board_Init(show,ROWS,COLS,'*');//打印数组//Display_board(mine, ROW, COL);//Display_board(show, ROW, COL);//设置雷Set_mine(mine, ROW, COL);//Display_board(mine, ROW, COL);Display_board(show, ROW, COL);//排查雷Fine_Mine(mine, show, ROW, COL);}

4.宏定义

//设置行和列的大小
#define ROW 9
#define COL 9
//设置雷的数量
#define COUNT 80

5.界面初始化

初始化数组的内容为指定内容

mine数组在没有布置雷的时候,都是‘0‘

show数组在没有排查雷的时候都是‘*’

//数组初始化
void board_Init(char board[ROWS][COLS], int rows, int cols,char x)
{int i = 0;int j = 0;for (i = 0; i < rows; i++){for (j = 0; j < cols; j++){board[i][j] = x;}}
}

6.打印界面

//打印数组
void Display_board(char board[ROWS][COLS], int row, int col)
{int i = 0;int j = 0;printf("-----扫雷游戏-----\n");printf("  ");for (j = 1; j <= col; j++){printf("%d ", j);}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");printf("\n");}

7.设置雷

//设置雷
void Set_mine(char board[ROWS][COLS], int row, int col)
{//定义雷的数量int count = COUNT;//行1-9//列1-9while (count){int x = rand() % row + 1;//在一行中随机生成一个数int y = rand() % col + 1;//在一列中随机生成一个数if (board[x][y] == '0'){board[x][y] = '1';count--;}}}

8.统计排查坐标周围雷的个数

//统计排查坐标周围雷的个数
int get_mine_count(char board[ROWS][COLS], int x, int y)
{return board[x - 1][y - 1] +board[x - 1][y] +board[x - 1][y + 1] +board[x][y + 1] +board[x + 1][y + 1] +board[x + 1][y] +board[x + 1][y - 1] +board[x][y - 1] - 8 * '0';
}

9.排查雷

//排查雷
void Fine_Mine(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-COUNT){printf("请输入要排查的坐标>:");scanf("%d%d", &x, &y);if (x >= 1 && x <= row && y >= 1 && y <= col){if (show[x][y] != 'x'){printf("该坐标已被排查过了,不能重复排查\n");}else{//如果是雷if (mine[x][y] == '1'){printf("很遗憾,你被炸死了\n");Display_board(mine, ROW, COL);break;}else//如果不是雷{win++;//统计mine数组中x,y坐标周围有几个雷int count = get_mine_count(mine, x, y);show[x][y] = count + '0';//转换成数字字符Display_board(show, ROW, COL);}}}elseprintf("输入坐标非法,请重新输入\n");}if (win == row*col - COUNT){printf("恭喜你,排雷成功\n");Display_board(mine, ROW, COL);}
}

10.总代码

test.c代码

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#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};//初始化数组的内容为指定内容//mine数组在没有布置雷的时候,都是‘0’board_Init(mine,ROWS,COLS,'0');//show数组在没有排查雷的时候都是‘*’board_Init(show,ROWS,COLS,'*');//打印数组//Display_board(mine, ROW, COL);//Display_board(show, ROW, COL);//设置雷Set_mine(mine, ROW, COL);//Display_board(mine, ROW, COL);Display_board(show, ROW, COL);//排查雷Fine_Mine(mine, show, ROW, COL);}
int main()
{//设置随机数的生成srand((unsigned int)time(NULL));int input = 0;do{printf("扫雷游戏\n");menu();printf("请选择(1/0):>");scanf("%d", &input);switch (input){case 1:printf("开始游戏\n");game();break;case 0:printf("退出游戏\n");default:printf("输入错误\n");}}while (input);return 0;
}

game.h代码

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
//设置行和列的大小
#define ROW 9
#define COL 9#define ROWS ROW+2
#define COLS COL+2//设置雷的数量
#define COUNT 10
//初始化数组
void board_Init(char mine[ROWS][COLS], int rows, int cols, char x);
//打印数组
void Display_board(char board[ROWS][COLS], int row, int col);
//设置雷
void Set_mine(char board[ROWS][COLS], int row, int col);
//排查雷
void Fine_Mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

game.c代码

define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"//数组初始化
void board_Init(char board[ROWS][COLS], int rows, int cols,char x)
{int i = 0;int j = 0;for (i = 0; i < rows; i++){for (j = 0; j < cols; j++){board[i][j] = x;}}
}//打印数组
void Display_board(char board[ROWS][COLS], int row, int col)
{int i = 0;int j = 0;printf("-----扫雷游戏-----\n");printf("  ");for (j = 1; j <= col; j++){printf("%d ", j);}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");printf("\n");}//设置雷
void Set_mine(char board[ROWS][COLS], int row, int col)
{//定义雷的数量int count = COUNT;//行1-9//列1-9while (count){int x = rand() % row + 1;//在一行中随机生成一个数int y = rand() % col + 1;//在一列中随机生成一个数if (board[x][y] == '0'){board[x][y] = '1';count--;}}}//统计排查坐标周围雷的个数
int get_mine_count(char board[ROWS][COLS], int x, int y)
{return board[x - 1][y - 1] +board[x - 1][y] +board[x - 1][y + 1] +board[x][y + 1] +board[x + 1][y + 1] +board[x + 1][y] +board[x + 1][y - 1] +board[x][y - 1] - 8 * '0';
}//排查雷
void Fine_Mine(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-COUNT){printf("请输入要排查的坐标>:");scanf("%d%d", &x, &y);if (x >= 1 && x <= row && y >= 1 && y <= col){if (show[x][y] != 'x'){printf("该坐标已被排查过了,不能重复排查\n");}else{//如果是雷if (mine[x][y] == '1'){printf("很遗憾,你被炸死了\n");Display_board(mine, ROW, COL);break;}else//如果不是雷{win++;//统计mine数组中x,y坐标周围有几个雷int count = get_mine_count(mine, x, y);show[x][y] = count + '0';//转换成数字字符Display_board(show, ROW, COL);}}}elseprintf("输入坐标非法,请重新输入\n");}if (win == row*col - COUNT){printf("恭喜你,排雷成功\n");Display_board(mine, ROW, COL);}
}

结语:

技术有限可能有些BUG没有发现,可以往里面添点有意思的程序,比如弄个关机程序进去,扫到雷直接关机,那样会很酷吧

http://www.lryc.cn/news/517280.html

相关文章:

  • CSS语言的文件操作
  • 前端-计算机网络篇
  • 行为分析:LSTM、3D CNN、SlowFast Networks。这三者的优缺点
  • 【HarmonyOS NEXT】鸿蒙应用使用后台任务之长时任务,解决屏幕录制音乐播放等操作不被挂起
  • STM32-WWDG/IWDG看门狗
  • 基于视觉惯性 SLAM(VSLAM)、相机和 IMU 数据的融合执行 6 自由度位姿跟踪
  • Matlab仿真径向受压圆盘光弹图像
  • 网络安全抓包
  • WebSocket 测试调试:工具与实践
  • ArmSoM RK3588/RK3576核心板,开发板网络设置
  • 【学Rust开发CAD】1 环境搭建
  • 数据结构与算法之二叉树: LeetCode 108. 将有序数组转换为二叉搜索树 (Ts版)
  • Java 多线程之@Async
  • 代码随想录day38 动态规划6
  • LabVIEW无标题的模态VI窗口的白框怎么去除?
  • iOS - 原子操作
  • Go语言的语法
  • 【MySQL 保姆级教学】用户管理和数据库权限(16)
  • 什么是 ES6 “模板语法” ?
  • [项目实战2]贪吃蛇游戏
  • 关于FPGA中添加FIR IP核(采用了GOWIN EDA)
  • 1. 使用springboot做一个音乐播放器软件项目【前期规划】
  • 【Dify】Dify自定义模型设置 | 对接DMXAPI使用打折 Openai GPT 或 Claude3.5系列模型方法详解
  • 【Rust自学】10.8. 生命周期 Pt.4:方法定义中的生命周期标注与静态生命周期
  • 121 买入股票的最佳时机
  • PID学习资料
  • 采用标准化的方式开展设计-研发中运用设计模式
  • 【Linux系列】并发与顺序执行:在 Linux 脚本中的应用与选择
  • Scala语言的数据库交互
  • 字节青训十五题-Java-数字字符串格式化