当前位置:网站首页>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);
###############################################################
游戏演示

############################################
############################################
############################################
############################################
############################################
############################################
############################################
最后:文章有什么不对的地方或者有什么更好的写法欢迎大家在评论区指出来.
边栏推荐
- MySQL数据库备份
- C语言表白代码?
- Blockbuster | foundation for platinum, gold, silver gave nameboards donors
- unity2d小游戏
- prompt.ml/15中<svg>标签使用解释
- [py script] batch binarization processing images
- Unity shader forge和自带的shader graph,有哪些优缺点?
- Reinforcement learning: from entry to pit to shit
- MySQL数据库安装配置保姆级教程(以8.0.29为例)有手就行
- 重磅 | 基金会为白金、黄金、白银捐赠人授牌
猜你喜欢

微信小程序使用云函数更新和添加云数据库嵌套数组元素

PWN ROP

Unity资源管理系列:Unity 框架如何做好资源管理

Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us

Unity Fighter

从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)

已解决:不小心卸载pip后(手动安装pip的两种方式)

VScode+ESP32快速安装ESP-IDF插件

30 Years of Open Source Community | 2022 Open Atom Global Open Source Summit 30 Years of Open Source Community Special Event Held Successfully

Minesweeper game - C language
随机推荐
The 15th day of the special assault version of the sword offer
Basic knowledge of mysql (2)
聚变云原生,赋能新里程 | 2022开放原子全球开源峰会云原生分论坛圆满召开
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)
已解决(最新版selenium框架元素定位报错)NameError: name ‘By‘ is not defined
Musk talks to the "virtual version" of Musk, how far is the brain-computer interaction technology from us
Create componentized development based on ILRuntime hot update
MySQL忘记密码怎么办
unity2d game
Reinforcement learning: from entry to pit to shit
MySQL开窗函数
STM32HAL library modifies Hal_Delay to us-level delay
Can't load /home/Iot/.rnd into RNG
MATLAB/Simulink & & STM32CubeMX tool chain completes model-based design development (MBD) (three)
HCIP第十天_BGP路由汇总实验
两个地址池r2负责管地址池r1负责管dhcp中继
sql语句之多表查询
Fusion Cloud Native, Empowering New Milestones | 2022 Open Atom Global Open Source Summit Cloud Native Sub-Forum Successfully Held
Open Source Smart Future | 2022 OpenAtom Global Open Source Summit OpenAtom openEuler sub-forum was successfully held
MySQL数据库必会的增删查改操作(CRUD)