当前位置:网站首页>"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详解
中软国际携手深开鸿发布(1+1) x N 战略,以数字化、智慧化改变人类生产和生活方式
[Interview: Concurrency 38: Multithreading: Thread Pool] Basic concepts of the ThreadPoolExecutor class
CNN--各层的介绍
R语言 第一部分
事务的传播机制
【Go】Go 语言切片(Slice)
2022.07.29_Daily Question
2022.07.22 _ a day
How to set the computer password?How to add "safety lock" to your computer
Matlab学习第一天(持续更新中)
Environment_Variable_and_SetUID
2022.07.24_每日一题
Vscode:Project-tree插件
中断及pendSV
初识NK-RTU980开发板
mysql的建表语句_三种常用的MySQL建表语句
@Transactional注解的失效场景
【微服务】(十六)—— 分布式事务Seata
Pygame Surface对象