当前位置:网站首页>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
边栏推荐
- Qt显示FFmpeg解码的图片
- Assemble relevant knowledge points of flag bit (connected)
- Openstack learning notes -nova component insight
- 关于三子棋游戏的简易实现与N子棋胜利判断方法
- Using swiper to realize seamless rotation of multiple slides
- 坡道带来的困惑
- 剑指Offer 第 2 天链表(简单)
- Sword finger offer 04 Find in 2D array
- Solve the problem that yarn cannot load files in vs Code
- Lexical trap
猜你喜欢

There is a problem with the date when MySQL imports and exports data to excel

剑指 Offer 第 1 天栈与队列(简单)

Serenvlt first met

J2EE从入门到入土01.MySQL安装

指针,它那些不得不说的题目

Sword finger offer II 029 Sorted circular linked list

leetcode:918. 环形子数组的最大和【逆向思维 + 最大子数组和】

About data storage in memory

Django framework - caching, signaling, cross site request forgery, cross domain issues, cookie session token

初始c语言的知识2.0
随机推荐
初始c语言的知识2.0
Module 5 (microblog comments)
量化交易之回测篇 - 期货CTA策略实例(TQZFutureRenkoScalpingStrategy)
Some knowledge of the initial C language
Sword finger offer day 1 stack and queue (simple)
Serevlt初识
汇编标志位相关知识点(连)
數據在內存中的存儲相關內容
Sword finger offer II 029 Sorted circular linked list
关于一个图书小系统的实现
Confusion caused by the ramp
leetcode - 384. Scramble array
Summary of leetcode linked list problem solving skills
KVM 脚本管理 —— 筑梦之路
Germany holds global food security Solidarity Conference
Of binary tree_ Huffman tree_ Huffman encoding
买基金在哪里开户安全?还请赐教
关于一道教材题的讲解
KDD 2022 | graphmae: self supervised mask map self encoder
KDD 2022 | GraphMAE:自监督掩码图自编码器