当前位置:网站首页>"The C language games" mine clearance
"The C language games" mine clearance
2022-07-31 07:58:00 【Guo Guo study tour】

Here is a simple little game today 扫雷 Not much to say about the rules Not much to say, go directly to the effect map

直接一点 上代码 !!!There are three source files in total
Header files and function declarations
#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
//界面
void menu();
//初始化数组
void init_board(char borad[ROWS][COLS],int rows,int cols,char ch);
//打印棋盘
void display_board(char borad[ROWS][COLS], int rows, int cols);
//布置雷
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);函数实现
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu() {
printf("*************************\n");
printf("****** 1.play *****\n");
printf("****** 0.exit *****\n");
printf("*************************\n");
}
void init_board(char borad[ROWS][COLS], int rows, int cols, char ch) {
int i = 0;
int j = 0;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
borad[i][j] = ch;
}
}
}
void display_board(char borad[ROWS][COLS], int row, int col) {
int i = 0;
int j = 0;
for (i = 0; i <= 9; i++) {
printf("%d ", i);
}printf("\n");
for (i = 1; i < row+1; i++) {
printf("%d ", i);
for (j = 1; j < col+1; j++) {
printf("%c ", borad[i][j]);
}
printf("\n");
}
}
void set_mine(char borad[ROWS][COLS], int row, int col) {
int n = 10;
while (n) {
int x = rand() % 10;
int y = rand() % 10;
if (borad[x][y] == '0'&&x>0&&x<=row&&y>0&&y<=col) {
borad[x][y] = '1';
n--;
}
}
}
int get_mine_count(char mine[ROWS][COLS], int x, int y) {
return mine[x - 1][y - 1] +
mine[x - 1][y] +
mine[x - 1][y + 1] +
mine[x][y - 1] +
mine[x][y + 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
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 t = 0;
while (t<row*col-10) {
printf("Please enter a subscript to check>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
if (show[x][y] == '*') {
if (mine[x][y] == '1') {
printf("很遗憾你GG了,游戏结束\n");
return;
}
else if(mine[x][y] != '1'){
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
t++;
display_board(show, ROW, COL);
}
}
else { printf("This subscript has been queued"); }
}
else { printf("The coordinates entered are illegal,请重新输入\n"); }
}
if(t== row * col - 10)
printf("恭喜你排雷成功\n");
}游戏实现
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void game() {
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
init_board(mine, ROWS, COLS, '0');
init_board(show, ROWS, COLS, '*');
set_mine(mine, ROW, COL);
//display_board(mine, ROW, COL);
display_board(show, ROW, COL);
find_mine(mine,show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do {
menu();
printf("请输入你的选择>");
scanf("%d", &input);
switch (input) {
case 0:printf("退出游戏\n"); break;
case 1:printf("开始游戏\n"); game(); break;
default:printf("输入错误,请重新输入\n"); break;
}
} while (input);
return 0;
}The following is an analysis of the ideas for you
The first step is to construct the interface first 界面效果如下
After first constructing the interface, we also need to consider printing the chessboard,效果图如下
但是我们要考虑到 If you use the input subscript method when playing chess,Counting them would be more troublesome,So we will do some simple processing on this chessboard ,效果图如下
Then we need to think about placing mines,In order to ensure the randomness of the distribution of each mine,我们请出了cRandom numbers in languagerand();函数 But he also considered making his changes more mysterious,We go ahead and give it a timestamptime(NULL);函数 So let's construct it first srand(time(NULL));但是,问题来了,srand函数接收的是unsigned类型,If we want to use it better, we also need to use a type conversionsrand((unsigned int)time(NULL));
Then we can mine well,Take a random interception of the distribution of mines and take a look
红框中的1is the distribution of thunder
Then we start the player minesweeper,The first thing we need to consider is whether the subscript entered by the player is legal,The second thing to consider is whether the subscript entered by the player has been swiped,After excluding these two factors, we have to consider whether the coordinates entered by the player are mines,If it's Ray then so be itgg了,Otherwise, print out the distribution number of nearby mines to the screen
After every player minesweep,To test whether the player has successfully swept all the mines out,If all grids are successfully opened, all mines are avoided,Then the player wins
好了,Our simple little game is done,If it helps you, look forward to three consecutive
边栏推荐
猜你喜欢

【微服务】(十六)—— 分布式事务Seata

超级详细的mysql数据库安装指南

2022.07.20_Daily Question

Environment_Variable_and_SetUID

Visual Studio新功能出炉:低优先级构建

Leetcode952. Calculate maximum component size by common factor

Fund investment advisory business

双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分

Vscode: Project-tree plugin

【科普向】5G核心网架构和关键技术
随机推荐
【科普向】5G核心网架构和关键技术
2022.07.13_Daily Question
[PSQL] SQL Basic Course Reading Notes (Chapter1-4)
2022.07.20_Daily Question
Introduction and self-order of bcos
中断及pendSV
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
DAY18:Xss 靶场通关手册
linux redis6.2.6配置文件
03-SDRAM: Write operation (burst)
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
[PSQL] Complex query
2022.07.12_每日一题
regex bypass
[Interview: Concurrency 37: Multithreading: Thread Pool] Custom Thread Pool
最大似然估计和最小二乘法 含代码
2022.07.12_每日一题
Collation and sharing of related classic papers and datasets in the field of deep learning communication
《c语言》青蛙跳台阶递归问题
个人报错问题 持续总结




