当前位置:网站首页>Simple realization of mine sweeping
Simple realization of mine sweeping
2022-06-25 13:20:00 【LIn_ jt】
Simple implementation of mine sweeping
The mine sweeping characteristics :
- The first click must not be ray
- Easy to implement
First , We are still like a three piece chess game , Create two source files and a header file :

Then there is our main function part :
Because it's the test part , Write a test function to place our code :
Next is test Implementation of function :

Here menu The function is the print menu function , That is to remind the player what to choose , From here into our switch case Statement to determine what statement should be executed next
Input 1 After entering the game :( The minesweeping game is temporarily replaced by printing ), In this case, we have written the basic logic , Now let's focus on the implementation of mine sweeping game
take printf Change statement to game() Function to implement , Now we're going to play Minesweeper , What about the minesweeping game , Now take a written program to see

Now it's a chessboard , And we have to store information , therefore , We need to create a two-dimensional array to implement , But now we want to put the characters 1 It's our ray , What to do , therefore , Let's create two arrays , One for storing thunder , An array used to store demining information , But when we mine , Mine clearance needs to be carried out in eight directions , But what about the coordinates of the four corners ? towards 8 Judging in one direction is likely to cross the line . therefore , We don't create 9,9 Array of , And then create a 11,11 Array of , But we still use 9*9 Array of , So we define four identifier constants .
Now that it has been defined in the header file , Otherwise, let's just put all the contents of the header file .
therefore , Now come to our game Internal function :
The first is our initialization function :>
take mine and show Initialize the array to the elements we want , Initialize the array of layout mines to 0, Initialize the displayed array to ’*‘, So we can achieve the effect we want ,InitBoard Function definition part :
Now that the chessboard has been initialized , Let's print it out and see how it works , Write a DisplayBoard Function to implement , Please look at the chart below. 
first for The loop prints out the column number of each column , It's printed in the next cycle . Now let's look at the printing effect 

Now that it has been printed , So now we're going to set up ray , Arrange thunder , We use one SetMine Function to implement , Please look at the chart below.

Because we're going to place ten mines , use count = 10 To count , And generate random coordinates to place the mine
Ray has put it all away , It's time to start minesweeping now , Next, let's start our minesweeping part , Write a SweepMine Function to implement 
It's on the inner floor here CountMine function , It is used to calculate the number of mines around the coordinates of the point of the discharged mine , We do that :>

So the subroutine can run , Let's take a look at the effect :>
Here are the improvements to the program :>
In case it's ray for the first time , And we're easy to debug , We set the number of Lei as the identifier constant , The following is the added code :>

The number of mines is defined as the identifier constant
Then we create a safe minesweeping function 
In this way, you can avoid being the first to thunder , Now let's look at the effect :


Let's release the source code :
test.c File function 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()
{
char mine[ROWS][COLS] = {
0 };
char show[ROWS][COLS] = {
0 };
InitBoard(mine, ROWS, COLS, '0');// Here we pass in the element we want to initialize , So you can simply initialize... With a function .
InitBoard(show, ROWS, COLS, '*');
//DisplayBoard(show, ROW, COL);
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
SafeMine(mine, show, ROW, COL);
SweepMine(mine, show, ROW, COL);
}
void test()
{
srand((unsigned int)time(NULL));
// The game is executed at least once , So with do while loop
int input = 0;
do
{
menu();// Here menu Function to create a menu function for yourself , Prompt the player
printf(" Please select a number :>");// This is a choice , Then you have to enter a number ?
scanf("%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);
}
int main()
{
test();
return 0;
}
Here is game.c The contents of the document
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[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++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= col; i++)
{
printf("%d ", i); // Print column number , So that players can see
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i); // Print line number , So that players can see
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASYCOUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
int CountMine(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y] +
mine[x + 1][y] +
mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x - 1][y + 1] +
mine[x][y + 1] +
mine[x + 1][y + 1] - 8 * '0';
}
void SafeMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int newx = 0;
int newy = 0;
printf(" Please enter the coordinates of the investigation :>");
scanf("%d%d", &x, &y);
while (1)
{
if (x >= 1 && x <= row && y >= 1 && y <= col && mine[x][y] == '0')
{
int ret = CountMine(mine, x, y);
show[x][y] = ret + '0';
DisplayBoard(show, ROW, COL);
break;
}
newx = rand() % row + 1;
newy = rand() % col + 1;
if (x >= 1 && x <= row && y >= 1 && y <= col && mine[x][y] == '1')
{
if (mine[newx][newy] == '0')
{
mine[newx][newy] = '1';
mine[x][y] = '0';
int ret = CountMine(mine, x, y);
show[x][y] = ret + '0';
DisplayBoard(show, ROW, COL);
DisplayBoard(mine, ROW, COL);
break;
}
}
}
}
void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = row * col - EASYCOUNT - 1;
while (count)
{
printf(" Please enter the coordinates of the investigation :>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf(" unfortunately , You're killed in the blast \n");
break;
}
if (mine[x][y] == '0')
{
int ret = CountMine(mine, x, y);
show[x][y] = ret + '0';
DisplayBoard(show, ROW, COL);
count--;
}
}
else
{
printf(" Incorrect input , Please re-enter \n");
}
}
if (count == 0)
{
printf(" congratulations , Mine clearance is successful \n");
DisplayBoard(mine, ROW, COL);
}
}
game.h The code in :
#pragma once
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASYCOUNT 10
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS][COLS], int row, int col);
void SafeMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
The end of this paper
边栏推荐
- Capabilities required by architects
- The starting point for learning programming.
- 剑指 Offer II 029. 排序的循环链表
- Alibaba stability fault emergency handling process
- QT mouse tracking
- KDD 2022 | GraphMAE:自监督掩码图自编码器
- 始终保持疫情防控不放松 营造安全稳定的社会环境
- Golang keyboard input statement scanln scanf code example
- leetcode - 384. 打乱数组
- 德国举行全球粮食安全团结会议
猜你喜欢

WIN10环境下配置pytorch

Confusion caused by the ramp
![[data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box](/img/81/f8280f16efa314d736c3a869c32ef0.jpg)
[data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box

数据在内存中的存储相关内容

Online service emergency research methodology

À propos du stockage des données en mémoire

Sword finger offer II 032 Effective anagrams

KDD 2022 | GraphMAE:自监督掩码图自编码器

学习编程的起点。
![[turn] starting from the end, analyze in detail how to fill in the college entrance examination volunteer](/img/77/715454c8203d722e246ed70e1fe0d8.png)
[turn] starting from the end, analyze in detail how to fill in the college entrance examination volunteer
随机推荐
量化交易之回测篇 - 期货CTA策略策略(TQZFutureRenkoWaveStrategy)
Openstack learning notes -nova component insight
Django框架——缓存、信号、跨站请求伪造、 跨域问题、cookie-session-token
Three lines of code to simply modify the project code of the jar package
KVM 脚本管理 —— 筑梦之路
leetcode - 384. Scramble array
[pit avoidance refers to "difficult"] antd cascader implements new customized functions
Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream
Django framework - caching, signaling, cross site request forgery, cross domain issues, cookie session token
康威定律,作为架构师还不会灵活运用?
Sword finger offer II 032 Effective anagrams
Sword finger offer II 029 Sorted circular linked list
买基金在哪里开户安全?还请赐教
MySQL learning notes
Always maintain epidemic prevention and control and create a safe and stable social environment
Sword finger offer day 3 string (simple)
OpenStack学习笔记(二)
德国举行全球粮食安全团结会议
. NET in China - What's New in . NET
And console Log say goodbye