当前位置:网站首页>Easy to play minesweeping
Easy to play minesweeping
2022-07-18 00:28:00 【Wake up after dark】
Initial interface of mine sweeping game completion :
Write ideas :
First build game.h,game.c and test.c
game.h Declaration for function
game.c The realization of the game
test.c Test the logic of the game
It is similar to the idea of Sanzi chess last time .
test.c The function above :
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include"game.h" void menu() { printf("***************************\n"); printf("****** 1.play ***********\n"); printf("****** 0.exit ***********\n"); printf("***************************\n"); } void game() { } int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf(" Please enter :"); scanf_s("%d", &input); switch (input) { case 1: game(); break; case 0: printf(" Quit the game \n"); break; default: printf(" Input error , Please re-enter \n"); break; } } while (input); return 0; }Like Sanzi , need do....while sentence , It's mainly about the main function main,game function ,menu Function call .
Mine sweeping game is basically 9×9 Lattice of , At this time, you can game.h In the use #define To define :
#define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2The line that defines it is ROW The column is COL, In the later process of minesweeping game, one step is to judge whether there is thunder around the input coordinates ? There are a few thunder ? If you're not lucky , The coordinates entered are the farthest position , At this time, we have to consider the problem that the more the array does not cross the boundary . The function has to write an additional function call to judge that the input coordinates are not out of bounds . At this time, a new definition ROWS, That is to say ROW+2, There is no need to judge the problem of not crossing the boundary .
The next step is game() Functional :
9×9 Mine sweeping should be applied to two-dimensional arrays , We should define two arrays
//mine Array is used to store the information of arranged mines char mine[ROWS][COLS] = { 0 }; //'0' //show The array is used to store the information of the detected mines char show[ROWS][COLS] = { 0 }; //'*'
First step : Initialize chessboard :
First stay test.c Medium game() Input function in function :
Initboard(mine, ROWS, COLS, '0'); Initboard(show, ROWS, COLS, '*');Secondly, in game.h Function declared in :
void Initboard(char arr[ROWS][COLS], int rows, int cols, char set);Last in game.c Write a function to initialize the chessboard in :
void Initboard(char arr[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++) { arr[i][j] = set; } } }A function is used to apply 2 An array , Lei Yong '1' Express , Not ray '*' Express , Directly define a string to represent .
arr[i][j] = set;
The second step : Print chessboard
First stay test.c Medium game() Input function in function :
showboard(mine, ROW, COL); showboard(show, ROW, COL);Secondly, in game.h Function declared in :
void showboard(char arr[ROWS][COLS], int row, int col);Last in game.c Write a function to initialize the chessboard in :
void showboard(char arr[ROWS][COLS], int row, int col) { int i = 0; int j = 0; printf("------------- Mine clearance ---------------\n"); for (i = 0; i <= col; i++) { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) { printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ", arr[i][j]); } printf("\n"); } printf("------------- Mine clearance ---------------\n");To have the following situation
It's just printing 9×9 Of ‘*’, It looks a little dazzling , Coordinates are not easy to count . You can print out the first line first
for (i = 0; i <= col; i++) { printf("%d ", i); }At this time, remember to print line breaks
printf("\n");While printing, you can also print the numbers in the first column
for (i = 1; i <= row; i++) { printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ", arr[i][j]); } printf("\n"); }Enter the check coordinates once , Then enter the next coordinate , To look good , You can print on both the top and bottom
printf("------------- Mine clearance ---------------\n");
As shown in the above picture , At least it doesn't look dizzy .
The third step : Lay out Lei
First stay test.c Medium game() Input function in function :
set_mine(mine, ROW, COL);Secondly, in game.h Function declared in :
void set_mine(char mine[ROWS][COLS], int row, int col);Last in game.c Write a function to initialize the chessboard in :
void set_mine(char mine[ROWS][COLS], int row, int col) { int count = EASY_COUNT; int x = 0; int y = 0; while (count) { x = rand() % row + 1; y = rand() % col + 1; if (mine[x][y] == '0') { mine[x][y] = '1';// Arrange thunder count--; } } }The computer layout is naturally random , be used rand function ,rand The function is to give a random value .srand The argument type of the function is unsigned integer , Its purpose is to define rand Start of , Function header file stdlib.h. Time stamp function time() The return value of can be used as a parameter to increase the randomness , Function header file time.h.srand Use with timestamp , Only by defining it once can we guarantee the maximum randomness , So we define it in the main function srand.
stay game.h Add header file
#include<stdlib.h> #include<time.h>The main function main Add in
srand((unsigned int)time(NULL));If you want to set the number of thunder by yourself , Can be in game.h of use #define,
#define EASY_COUNT 10
When arranging thunder , In the main function , Should be
set_mine(mine, ROW, COL); showboard(show, ROW, COL);Lay out Lei first , Then print out the chessboard .
Step four : Check the thunder
First stay test.c Medium game() Input function in function :
findmine(mine, show, ROW, COL);Secondly, in game.h Function declared in :
void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);Last in game.c Write a function to initialize the chessboard in :
void findmine(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 - EASY_COUNT) { printf(" Please enter the coordinates to be checked :"); scanf_s("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf(" unfortunately , Step on the thunder !\n"); showboard(mine, ROW, COL); break; } else { int count = get_mine_count(mine, x, y); show[x][y] = count + '0'; showboard(show, ROW, COL); win++; } } else { printf(" The coordinates are illegal , Re input \n"); } } if (win == row * col - EASY_COUNT) { printf(" Congratulations on customs clearance \n"); showboard(mine, ROW, COL); } }Check the thunder , It's a row , Then row , be used while loop .
To check the thunder, first enter the coordinates , Judge whether the coordinates are legal
Then judge whether the input coordinates are thunder
1) Step on thunder ,game over 2) It's not ray , continue
Finally, judge who won
When judging whether it is thunder , If it's ray , We give the location of ray , So that players can know where there is thunder , There are a few thunder .
If it wasn't ray , According to the game tradition, we should give some tips , Show the coordinates around this input 9 There are several thunder , Just fill in the number to the position of the input coordinates .
This is discovery get_mine_count() This function has not been written , This function is only used to serve findmine Functional , Write this function directly above :
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'; }stay ASCLL table ,‘0’ Value ratio of ‘1’ One less . Put around 9 Ge judges whether it is queen Lei , If it's ray, add it up , Direct subtraction 8*'0' You can calculate the number of mines around the input coordinates .
To judge whether to win or lose is to see win and row * col - EASY_COUNT Is it equal , Equality means that thunder is discharged , Players pass the customs smoothly .
test.c Full code :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
void menu()
{
printf("***************************\n");
printf("****** 1.play ***********\n");
printf("****** 0.exit ***********\n");
printf("***************************\n");
}
void game()
{
// The realization of mine sweeping
//mine Array is used to store the information of arranged mines
char mine[ROWS][COLS] = { 0 }; //'0'
//show The array is used to store the information of the detected mines
char show[ROWS][COLS] = { 0 }; //'*'
// Initialize chessboard
Initboard(mine, ROWS, COLS, '0');
Initboard(show, ROWS, COLS, '*');
// Print chessboard
/*showboard(mine, ROW, COL);
showboard(show, ROW, COL);*/
// Arrange thunder
set_mine(mine, ROW, COL);
showboard(show, ROW, COL);
// Check the thunder
findmine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf(" Please enter :");
scanf_s("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Input error , Please re-enter \n");
break;
}
} while (input);
return 0;
}game.h Full code :
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
// initialization
void Initboard(char arr[ROWS][COLS], int rows, int cols, char set);
// Print chessboard
void showboard(char arr[ROWS][COLS], int row, int col);
// Arrange thunder
void set_mine(char mine[ROWS][COLS], int row, int col);
// Check the thunder
void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);game.c Full code :
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void Initboard(char arr[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++)
{
arr[i][j] = set;
}
}
}
void showboard(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("------------- Mine clearance ---------------\n");
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
printf("------------- Mine clearance ---------------\n");
}
// Arrange thunder
void set_mine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
int x = 0;
int y = 0;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';// Arrange thunder
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 findmine(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 - EASY_COUNT)
{
printf(" Please enter the coordinates to be checked :");
scanf_s("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf(" unfortunately , Step on the thunder !\n");
showboard(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
showboard(show, ROW, COL);
win++;
}
}
else
{
printf(" The coordinates are illegal , Re input \n");
}
}
if (win == row * col - EASY_COUNT)
{
printf(" Congratulations on customs clearance \n");
showboard(mine, ROW, COL);
}
}边栏推荐
- H5 start applet
- 【数学建模暑期培训】Matlab数据预处理
- Communication chat system based on 51 single chip microcomputer
- 【数学建模暑期培训】Matlab绘图命令
- mysql进阶(三)游标简易知识点汇总
- MySQL read write separation configuration practice
- Jupyter Notebook入门教程
- [learning notes] tree DP
- QPS和TPS的区别
- The security optimization settings after the installation of Zhimeng CMS effectively protect Trojans
猜你喜欢

Jupyter Notebook入门教程
![[JMeter] win10 download and install JMeter 5.5](/img/92/9654d32eada08f33db43f7c45e8be1.png)
[JMeter] win10 download and install JMeter 5.5

【直播课】腾讯课堂------基于GO语言的云原生工具二次开发实战训练营-------Prometheus Exporter开发

Suspected of being recruited by apple, playcover author deleted the library and ran away

The difference between observer mode and publish subscribe mode

【古月21讲】ROS入门系列(2)——发布者Publisher、订阅者Subscriber的编程实现+自定义话题消息编程实现

【源码】HashSet的实现原理

Macm1 chip, centos8 virtual machine, mysql8 installation, service up, login error
![[interview question] what is the difference between poll() and remove() in the queue](/img/5c/2757c1be47d22dbd61510fb2f6e292.png)
[interview question] what is the difference between poll() and remove() in the queue

NFT industry analysis of metauniverse: China's digital collection industry is expected to move towards standardization and differentiation
随机推荐
使用pyinstaller打包Mediapipe项目时遇到FileNotFoundError: The path does not exist的解决方法
Alibaba cloud - object storage OSS cost optimization
C language shift operation
[unity learning 023] object pool Pro
spark调优(六):大家好才是真的好——广播变量
H5 start applet
疑似被Apple招安,PlayCover作者删库跑路
【文档熟肉】redis数据类型
MFC控件学习:按钮
【直播课】腾讯课堂------基于GO语言的云原生工具二次开发实战训练营-------Prometheus Exporter开发
手把手教你用代码实现SSO单点登录
DEDECMS织梦更改include目录重命名后,后台属性编辑无法加载的解决方法
. Net webapi to realize interface version control and get through swagger support
2019ccpc Qinhuangdao HDU - 6736 F - Forest Program (DFS ring finding Combinatorial Mathematics)
CF1265E Beautiful Mirrors (概率dp)
Analysis of eip-2535 diamond agreement from the Saudi NFT event
The solution that background attribute editing cannot load after dedecms dream changes the include directory rename
12. Installation and use of mobx 6, enable decorator syntax
Smart breeding scheme based on Lora gateway
关于DP中完全背包的遍历次序探讨


