当前位置:网站首页>Minesweeper game (written in c language)
Minesweeper game (written in c language)
2022-07-31 04:51:00 【Can't be white】
The minesweeper game is divided into three sections:
* test.c (主函数)
* game.c(游戏部分代码实现)
* game.h(头文件,宏定义,函数的声明)
test.c
#include"game.h"
void menu()
{
//Print the Minesweeper game catalog
printf("########################################\n");
printf("######### 1.play #########\n");
printf("######### 0.exit #########\n");
printf("########################################\n");
}
void game()
{
//设计2个数组存放信息
char mine[ROWS][COLS] = {
0 };
char show[ROWS][COLS] = {
0 };
//初始化棋盘
init_board(mine, ROWS, COLS, '0');
init_board(show, ROWS, COLS, '*');
//打印棋盘
//display_board(mine, ROW, COL);
//display_board(show, ROW, COL);
//storage mines
set_mine(mine, ROW, COL);
display_board(show, ROW, COL);//打印棋盘
//排雷
find_mine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择>:");
scanf_s("%d", &input);
if (input == 1)
{
printf("开始游戏\n");
game();
break;
}
else if (input == 0)
{
printf("游戏结束");
break;
}
else
printf("输入错误,请从新输入\n");
} while (input);
}
##################################################################
game.c
#include"game.h"
//初始化两个数组
void init_board(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 display_board(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (j = 0; 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");
}
}
//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col)
{
//布置10个雷
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
//Count how many mines there are in a circle around the coordinates you entered
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 find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win <(col * row) - EASY_COUNT)
{
printf("请输入坐标》:");
scanf_s("%d %d",&x,&y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '0')
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
display_board(show, ROW, COL);
win++;
}
else
{
printf("爆炸了\n");
display_board(mine, ROW, COL);
break;
}
}
else
{
printf("该坐标已被占用\n");
}
}
else
{
printf("输入坐标有误,请重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
display_board(mine, ROW, COL);
}
}
#####################################################################
game.h
#include<stdio.h>
#pragma once
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10//雷的个数
extern void init_board(char mine[ROWS][COLS], int rows, int cols,char set);
extern void display_board(char board[ROWS][COLS], int row, int col);
extern void set_mine(char mine[ROWS][COLS], int row, int col);
extern void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
###############################################################
游戏演示

############################################
############################################
############################################
############################################
############################################
############################################
############################################
最后:文章有什么不对的地方或者有什么更好的写法欢迎大家在评论区指出来.
边栏推荐
- 1. 获取数据-requests.get()
- 问题1:给你1-10的列表,实现列表输出,单数在左边,双数在右边。
- Unity资源管理系列:Unity 框架如何做好资源管理
- Unity框架设计系列:Unity 如何设计网络框架
- Unity URP渲染管线摄像机核心机制剖析
- MATLAB/Simulink&&STM32CubeMX工具链完成基于模型的设计开发(MBD)(三)
- 【C语言】操作符详解
- 【云原生】DevOps(五):集成Harbor
- Exsl file preview, word file preview web page method
- Lua,ILRuntime, HybridCLR(wolong)/huatuo hot update comparative analysis
猜你喜欢

Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held

MySQL database addition, deletion, modification and query (detailed explanation of basic operation commands)
![[debug highlights] Expected input batch_size (1) to match target batch_size (0)](/img/b3/ff6ccc3cd307befad3bd07a9f4a956.png)
[debug highlights] Expected input batch_size (1) to match target batch_size (0)

MATLAB/Simulink & & STM32CubeMX tool chain completes model-based design development (MBD) (three)

DVWA安装教程(懂你的不懂·详细)

C语言表白代码?

ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)

【R语言】【3】apply,tapply,lapply,sapply,mapply与par函数相关参数

WPF WPF 】 【 the depth resolution of the template

【C语言进阶】文件操作(一)
随机推荐
The MySQL database installed configuration nanny level tutorial for 8.0.29 (for example) have hands
EasyExcel的简单读取操作
VScode+ESP32快速安装ESP-IDF插件
参考代码系列_1.各种语言的Hello World
Unity Tutorial: URP Rendering Pipeline Practical Tutorial Series [1]
ENSP,划分VLAN、静态路由,三层交换机综合配置
The Vue project connects to the MySQL database through node and implements addition, deletion, modification and query operations
[py script] batch binarization processing images
ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches
unity2d小游戏
开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution
C language confession code?
three.js make 3D photo album
MATLAB/Simulink&&STM32CubeMX工具链完成基于模型的设计开发(MBD)(三)
Interview | Cheng Li, CTO of Alibaba: Cloud + open source together form a credible foundation for the digital world
Heavyweight | The Open Atomic School Source Line activity was officially launched
Create componentized development based on ILRuntime hot update
Unity教程:URP渲染管线实战教程系列【1】
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法