当前位置:网站首页>Mining game (C language)
Mining game (C language)
2022-08-02 06:44:00 【Fruit Chenchen】
文章目录
Game preface preparation
1.Initialize the game layout for Minesweeper using a 2D array,Changed by changing the row and column of the array,用ROWto specify the number of rows in the array,COLto specify the number of columns in the array,Minesweeper requires two two-dimensional arrays,An array is used to store mine information,An array is used to store display interface information,Used for fear of border crossingROW+2和COL+2array to avoid out of bounds;
2.Displays the minesweeper game interface using a 2D array,In addition to displaying array information,It is also necessary to display the coordinates in the first row and first column so that the player can determine the coordinates of the mine clearance;
3.设置雷区,A fixed number of mines are randomly placed according to the difficulty of the game,使用#defineIt is defined to change the value at any time to achieve a different number of mines;
4.排雷,Whenever the coordinates selected by the player are not mine,Then calculate the number of mines in a circle of eight grids around this coordinate.
5.判断输赢,If the player steps on a thunder,The game ends immediately,If the number of unlined areas is equal to the number of mines and the player is not killed,则玩家赢.
一、建立游戏菜单
void menu()
{
printf("********************************\n");
printf("********* 扫雷游戏 ********\n");
printf("********************************\n");
printf("********* 1.play ********\n");
printf("********* 0.exit ********\n");
printf("********************************\n");
printf("********************************\n");
}
nt main()
{
int input = 0;
do
{
menu();
printf("请输入你的选择\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
二、建立棋盘
void init_broad(char broad[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++)
{
broad[i][j] = set;
}
}
}
三、初始化棋盘
void display_broad(char broad[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 ", broad[i][j]);
}
printf("\n");
}
}
四、布置雷
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = DEFAULT_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
五、Check for mines and determine whether to step on mines
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 < row * col - DEFAULT_COUNT)
{
printf("请输入要排查雷的坐标:>\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("你被炸死了\n");
display_broad(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, row, col);
show[x][y] = count + '0';
display_broad(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - DEFAULT_COUNT)
{
printf("恭喜你,排雷成功\n");
display_broad(mine, ROW, COL);
}
}
六、源码
1.test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("********************************\n");
printf("********* 扫雷游戏 ********\n");
printf("********************************\n");
printf("********* 1.play ********\n");
printf("********* 0.exit ********\n");
printf("********************************\n");
printf("********************************\n");
}
void game()
{
char mine[ROWS][COLS] = {
0 };//Design arrays to store information
char show[ROWS][COLS] = {
0 };
//初始化棋盘
init_broad(mine, ROWS, COLS, '0');
init_broad(show, ROWS, COLS, '*');
//打印棋盘
display_broad(show, ROW, COL);
//布置雷
set_mine(mine, ROW, COL);
//排雷
find_mine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入你的选择\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
2.game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_broad(char broad[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++)
{
broad[i][j] = set;
}
}
}
void display_broad(char broad[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 ", broad[i][j]);
}
printf("\n");
}
}
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = DEFAULT_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
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 < row * col - DEFAULT_COUNT)
{
printf("请输入要排查雷的坐标:>\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("你被炸死了\n");
display_broad(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, row, col);
show[x][y] = count + '0';
display_broad(show, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查\n");
}
}
else
{
printf("坐标非法,请重新输入\n");
}
}
if (win == row * col - DEFAULT_COUNT)
{
printf("恭喜你,排雷成功\n");
display_broad(mine, ROW, COL);
}
}
3.game.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define DEFAULT_COUNT 10
//初始化棋盘
void init_broad(char broad[ROWS][COLS],int rows,int cols,char set);
//打印棋盘
void display_broad(char broad[ROWS][COLS], int row, int col);
//布置雷
void set_mine(char mine[ROWS][COLS], int row, int col);
//排雷
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
边栏推荐
- 51 MCU peripherals: DS18B20
- 无代码生产新模式探索
- NPM 安装指定版本包的方法及版本号查看
- 【漫画】2021满分程序员行为对照表(最新版)
- 深入剖析成员变量和局部变量的初始化问题
- Alluxio为Presto赋能跨云的自助服务能力
- 关于 VS Code 优化启动性能的实践
- 淘系资深工程师整理的300+项学习资源清单(2021最新版)
- Automated operation and maintenance tools - ansible, overview, installation, module introduction
- [OpenCV from entry to practice] image processing technology [pixel] (the most detailed in the whole network)
猜你喜欢
Double for loop case (use js jiujiu printing multiplication table)
Point Density-Aware Voxels for LiDAR 3D Object Detection 论文笔记
MySql统计函数COUNT详解
Stress testing and performance analysis of node projects
如何优化OpenSumi终端性能?
The international top conference OSDI included Taobao system papers for the first time, and the device-cloud collaborative intelligence was recommended by the keynote speech of the conference
Important concepts of target detection - IOU, receptive field, hole convolution, mAP
国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
MySQL联合查询(多表查询)
zabbix自动发现和自动注册
随机推荐
Linux CentOS8安装Redis6
双重for循环案例(用js打印九九乘法表)
Deep learning - CNN realizes the recognition of MNIST handwritten digits
leetcode括号匹配问题——32.最长有效括号
Not annotated parameter overrides @NonNullApi parameter
人工神经网络
物联网如何改变城市运行效率
51 microcontroller peripherals article: dot-matrix LCD
保证家里和企业中的WIFI安全-附AC与AP组网实验
程序员最重要的能力是什么?
NPM 安装指定版本包的方法及版本号查看
Home NAS server (4) | MergerFS and SnapRaid data backup
Leetcode parentheses matching problem -- 32. The longest parentheses effectively
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
pytorch basic operations: classification tasks using neural networks
【OpenCV从入门到实践】图像处理技术[像素](全网最详细)
MySQL database video tutorial from beginner to proficient
科技赋能拉萨之“肺”,华为助力拉鲁湿地智慧管理守护绿水青山
leetcode每天5题-Day04
Analysis of port 9848 error at startup of Nacos client (non-version upgrade problem)