当前位置:网站首页>"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
边栏推荐
猜你喜欢
随机推荐
MySQL installation to the last step in the write the configuration file failed?And after the installation steps
《opencv学习笔记》-- 仿射变换
开源|商品识别推荐系统
2022.07.20_Daily Question
ros小乌龟画图
进程调度的基本过程
手把手教你开发微信小程序自定义底部导航栏
基于LSTM的诗词生成
强化学习科研知识必备(数据库、期刊、会议、牛人)
van-uploader上传图片,使用base64回显无法预览的问题
van-uploader uploads images, and cannot preview the image using base64 echo
庐山谣寄卢侍御虚舟
关于“算力”,这篇文章值得一看
2022.07.26_每日一题
Linked list implementation and task scheduling
Titanic 预测问题
Shellshock
【Objective-C语言中的@property】
Shell编程之条件语句
ros little turtle drawing














