当前位置:网站首页>Demining game (analysis)
Demining game (analysis)
2022-07-24 03:40:00 【Wax gourd learning java】
Catalog
One 、 Code example and analysis
2.7、 Count the number of mines around the target coordinates
Two 、 Demonstration of achievements
Preface
Mine sweeping is my information technology class in senior one , The game we often play when the teacher lets us operate freely , Suddenly I feel so missed .
Use today C Language to achieve mine sweeping game .
Before the official start, sort out what functions are needed for mine sweeping , What steps
General train of thought :
1、 Print menu
2、 Create array + Array initialization
3、 Print chessboard
4、 Arrange thunder
5、 Check the thunder + 6、 Judge victory + Print chessboard
One 、 Code example and analysis
1、game.h
The header file 、define Define constants 、 Function name
Need one 9*9 Array of To store thunder ( The array elements storing thunder cannot be changed ), We also need the same array to store the information of mine screening , But for prevent When checking thunder , Accessing the surrounding coordinates of the target coordinates at the corner position results in An array , So set it to 11*11 Array of , And display Only display the array subscript 1-9 The elements of
The code is as follows ( Example ):
#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
#define EASY_COUNT 10
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int Count_Around_mine(char mine[ROWS][COLS], int x, int y);
void Spread_Show(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
int Iswin(char show[ROWS][COLS], int row, int col);
2、test.c
test.c contain menu,game,main Three functions
The code is as follows ( Example ):
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void menu()
{
printf("**********************\n");
printf("******* 1.play *******\n");
printf("******* 0.exit *******\n");
printf("**********************\n");
}
// mine Array :0-- Non thunder 1-- Thunder
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[COLS][COLS] = { 0 };
// initialization
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
DisplayBoard(show, ROW, COL);
// Set ray
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
// Check the thunder
FindMine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
// Set the starting point for generating random numbers
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Game over \n");
break;
default:
printf(" Wrong choice , Please reselect \n");
break;
}
}
while (input);
return 0;
}2、game.c
2.1、 initialization
Pass in a parameter to initialize the content , You can initialize and get different results , That is, the function can be reused
The code is as follows ( Example ):
// initialization
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;
}
}
}2.2、 Print chessboard
Attention format
The code is as follows ( Example ):
// Print chessboard
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf(" -------- Mine clearance -------\n");
int i = 0;
int j = 0;
for (j = 0; j <= col; j++)
{
if (j == 0)
printf("|");
if (j < col)
printf("%d ", j);
else
printf("%d|\n", j);
}
for (i = 1; i <= row; i++)
{
printf("|%d ", i);
for (j = 1; j <= col; j++)
{
if (j < col)
printf("%c ", board[i][j]);
else
printf("%c|\n", board[i][j]);
}
}
printf(" -------- Mine clearance -------\n");
}2.3、 Arrange thunder
use rand function ,test.c of use srand Function to set the starting point of random number generation
The code is as follows ( Example ):
// Arrange thunder
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;// 1--9
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}2.4、 Check the thunder
The check requires multiple input of coordinates , So it's a cycle , Judge after each input 1、 Whether it has been checked ,2、 Is it ray ,3、 Whether there is thunder around , Each cycle Judge whether you win before the end
The code is as follows ( Example ):
// Check the thunder
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf(" Please enter the coordinates , Such as :1 1\n");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*') // Prevent duplicate input coordinates
{
printf(" The coordinates have been checked , Please re-enter other coordinates \n");
}
else
{
if (mine[x][y] == '0')
{
int count = Count_Around_mine(mine, x, y);
// Expand the function of a piece
//1. I'm not ray 2. There's no thunder around 3. I haven't been checked
if (count == 0)
{
Spread_Show(mine, show, x, y);
}
else
{
show[x][y] = count + '0';
}
DisplayBoard(show, ROW, COL);
}
else
{
printf(" unfortunately , You're killed in the blast \n");
DisplayBoard(mine, ROW, COL);
break;
}
}
}
else
{
printf(" Illegal coordinates , Please re-enter :>\n");
}
if (Iswin(show, ROW, COL) == EASY_COUNT)
{
printf(" congratulations , Mine clearance succeeded !\n");
break;
}
}
}2.5、 Expand functions
When the input coordinates (x,y) Satisfy :1、 Not checked ,2、 It's not ray ,3、 There's no thunder around ,x,y The element order at is ' ', You can expand to look around the coordinates 8 A coordinate , Such as x-1,y-1 Also meet the surrounding 8 There is no ray in the coordinates , You can recurse , The condition for setting the stop of recursion is , When x-1,y-1 Around 8 A coordinate When there is thunder x-1,y-1 This position element is the number of surrounding mines
The code is as follows ( Example ):
// Spread out a piece of ( When there is no thunder in the surrounding coordinates )
void Spread_Show(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int i = 0;
int j = 0;
int count = 0;
if (x >= 1 && x <= ROW && y >= 1 && y <= COL) // Prevent the array from going out of bounds when deep recursion
{
for (i = -1; i <= 1; i++) // Judge x,y Whether there is thunder around
{
for (j = -1; j <= 1; j++)
{
if (mine[x + i][y + j] == '0')
{
count = Count_Around_mine(mine, x+i, y+j);
if (count == 0)
{
if (show[x + i][y + j] == '*')// ' ' and '0' Will not enter the cycle , That is, the number has not been checked
{ // When i=x,j=y when show[x][y] Has been ordered as '0', Will not enter the cycle
show[x + i][y + j] = ' ';
Spread_Show(mine, show, x + i, y + j);
}
}
else if(count>0)// When x,y The surroundings are x+i,y+j When there is thunder around ,x+i,y+j stay show Numeric characters are given in , End recursion
{ // exclude count Become negative
show[x + i][y + j] = count + '0';
}
}
}
}
}
}2.6、 Judge victory
Traverse , If remaining * The number of is equal to the number of Mines set , Just win
The code is as follows ( Example ):
// Mine clearance victory judgment
Iswin(char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
int show_board_mine_count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show[i][j] == '*')
show_board_mine_count++;
}
}
return show_board_mine_count;
}2.7、 Count the number of mines around the target coordinates
The code is as follows ( Example ):
// Calculating the surrounding coordinates is the number of Mines
int Count_Around_mine(char mine[ROWS][COLS], int x, int y)
{
// Law 1
int i = 0;
int j = 0;
int count = 0;
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
count += mine[x + i][y + j]-'0';
}
}
return count;
Law two
//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');
}Two 、 Demonstration of achievements
1、 Blown up

2、 Spread out a piece of

3、 Mine clearance succeeded

summary
This article uses C Language to achieve mine clearance , Realized some functions , Due to time , One less Tag function Unrealized , The train of thought is : stay FindMine Function to create a function that belongs to menu The submenu of , The function can be 1. Check the thunder ,2. Mark ray ,3. Cancel mark ,0. sign out . The idea of modular design is really important , If you can use the function, don't write it in the main function
边栏推荐
- Xiaodi and Xiaohui
- What is the experience of writing concurrent tool classes (semaphore, cyclicbarrier, countdownlatch) by yourself in line 30?
- Genesis public chain: Tamp the foundation of Web 3.0 development
- 拉格朗日插值法
- Matlab Simulink hydropower and synchronous motor power generation
- A series of problems of dp+ backtracking segmentation palindrome string
- Native JS realizes the acquisition and operation of DOM
- Matlab sound signal processing frequency diagram signal filtering and playing sound
- Shengsi YiDianTong | deep learning analysis of classical convolutional neural network
- MySQL message queue list structure
猜你喜欢
![Embedded system transplantation [5] - Cross compilation tool chain](/img/2a/eadaaafe794aa9b3106441fa50ffc7.png)
Embedded system transplantation [5] - Cross compilation tool chain

Programmers may still be programmers, and coders may only be coders
![[MySQL learning] install and use multiple versions of MySQL, MySQL 8 and MySQL 5.7 at the same time, compressed version](/img/08/3765b34809cc4c723608f5d2e86ed4.png)
[MySQL learning] install and use multiple versions of MySQL, MySQL 8 and MySQL 5.7 at the same time, compressed version

IO stream sorting

Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?

Data Lake: introduction to Apache Hudi

Regular expression \b \b understand word boundary matching in simple terms

RTOS内功修炼记(十) | 深度解析RTOS内核上下文切换机制

Pit encountered in project upgrading

How to realize WiFi Internet short message authentication in the park / factory
随机推荐
正则表达式 \b \B 深入浅出理解单词边界的匹配
Scenario and value of data desensitization [summary]
Emqx v4.4.5 Publishing: new exclusive subscriptions and mqtt 5.0 publishing attribute support
什么是IMU?
Talk about the application of FIFO
C user defined type details
Y74. Chapter IV Prometheus large factory monitoring system and practice -- Introduction to promql and monitoring pod resources (V)
轮播图van-swipe的报错:cannot read a properties of null(reading width)
[super complete sorting] Cisco and Huawei order comparison memo, take it away without thanks! Anytime, anywhere
Matlab Simulink hydropower and synchronous motor power generation
Skywalking distributed system application performance monitoring tool - upper
jvm类加载过程简介说明
oh-my-zsh
4.合宙Air32F103_LCD
C language classic exercises (2) - "bubble sort"“
Summary of Zhang Yu's 30 lectures on Advanced Mathematics
leetcode hot 100(刷題篇8)(232/88/451/offer10/offer22/344/)
How to implement desktop lyrics in pyqt
What is the experience of writing concurrent tool classes (semaphore, cyclicbarrier, countdownlatch) by yourself in line 30?
IO stream sorting