当前位置:网站首页>Small game items - mine sweeping
Small game items - mine sweeping
2022-07-21 20:28:00 【Shilipo Xiaobai】
List of articles
Gobang game
Minesweeping game implementation
List of articles
Preface
Minesweeping is a classic game, which can no longer be a classic game , I will take you to realize the simple version of the minesweeping game , Beginners are also easy to use , The content is refined .
One 、 create a file
1. Create two project files and a header file
2. In two .c Reference in file #include " Minesweeping declaration .h"
3. The home page places the trunk of the mine sweeping implementation ( Thread )
Function content of placing minesweeping inside ( regional )
The function declaration of minesweeping is placed above ( Connect two files )
Two 、 Realization
1. The basic interface of the game
1. First we need a simple menu
void menu() {
printf("***********************************\n");
printf("*********** 1.Play ************\n");
printf("*********** 0.Exit ************\n");
printf("***********************************\n");
}
2. There are options to use switch Statements will be better
int main() {
int input = 0;
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input) {
case 1:
printf(" Game begins !\n");
break;
case 0:
printf(" Quit the game !\n");
break;
default:
printf(" Input error !\n");
}
return 0;
}
3. Players may play games many times. You can add one do while loop
int main() {
int input = 0;
do {
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input) {
case 1:
printf(" Game begins !\n");
Game(); // Functions that really realize the game
break;
case 0:
printf(" Quit the game !\n");
break;
default:
printf(" Input error !\n\n");
}
} while (input); //input by 0 When the cycle stops, the game ends
return 0;
}
2. Realization Game function
stay 9 * 9 In the minesweeping game , The numbers in each grid represent the surrounding 8 The number of mines in a grid , So we need to traverse every time 3 * 3 Of the lattice , When it's on the edge , There will be cross-border visits , This is very dangerous . For the sake of insurance , We define an array as 11 * 11 The big panel on the circle , Finally, initialize the data , In this way, there will be no cross-border visits . Trade space for time , Achieve high cohesion and low coupling , Simplify complex problems .
1. initialization
1. Initialization data
Define data and include header files in the declaration
#include <stdio.h>
#define ROW 9 // The number of columns in the game
#define COL 9 // The number of lines of the game
#define ROWS ROW + 2
#define COLS COL + 2
#define EASY_COUNT 10 // The number of ray
2. The minesweeping game is divided into two parts ( Corresponding to two arrays ):
1. The game screen that players can see intuitively
2. And the thunder that players can't see .
void Game() {
// Create array
char Mine[ROWS][COLS] = {
0}; // Where mines are stored
char Show[ROWS][COLS] = {
0}; // The game players see
}
3. Initialize array
1. Declare a function in the minesweeping declaration
void InitBoard(char Board[ROWS][COLS], int rows, int cols, char set);
2. Implement the function in the interior of mine sweeping
void InitBoard(char Board[ROWS][COLS], int rows, int cols, char set) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Board[i][j] = set;
}
}
}
3. Call in the minesweeping Homepage
void Game() {
// Create array
char Mine[ROWS][COLS] = {
0}; // Where mines are stored
char Show[ROWS][COLS] = {
0}; // The game players see
// Initialize array
InitBoard(Mine, ROWS, COLS, '0'); //Mine Array when there is no thunder '0'
InitBoard(Show, ROWS, COLS, '*'); //Show When the array didn't check thunder '*'
}
2. Print minesweeping game
The top and left are coordinates , It is convenient to input the corresponding coordinates
It's easy to print games here, so I won't talk nonsense here
1. Statement
// Print the game
void DisplayBoard(char Board[ROWS][COLS], int row, int col);
2. Inside
// Print the game
void DisplayBoard(char Board[ROWS][COLS], int row, int col) {
printf("|----------- The Minesweeper game -----------|\n");
for (int i = 0; i <= col; i++) {
printf(" %d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++) {
printf(" %d ", i);
for (int j = 1; j <= col; j++) {
printf(" %c ", Board[i][j]);
}
printf("\n");
}
printf("|----------- The Minesweeper game -----------|\n");
}
3. call
void Game() {
// Initialize the contents of the array
char Mine[ROWS][COLS] = {
0}; // Where mines are stored
char Show[ROWS][COLS] = {
0}; // The game players see
//Mine Array when there is no thunder '0'
InitBoard(Mine, ROWS, COLS, '0');
//Show When the array didn't check thunder '*'
InitBoard(Show, ROWS, COLS, '*');
// Print the game
DisplayBoard(Show, ROW, COL);
}
3. Set up mines
The random generation of Mines requires calling library functions
#include <time.h>
#include <stdlib.h>
1. Home page
because srand((unsigned int) time(NULL));
You only need to set it once, so we put it at the beginning of the main function .
int main() {
// Set random values
srand((unsigned int) time(NULL));
int input = 0;
do {
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input) {
case 1:
printf(" Game begins !\n");
Game();
break;
case 0:
printf(" Quit the game !\n");
break;
default:
printf(" Input error !\n\n");
}
} while (input);
return 0;
}
2. Inside
We set mines to characters '1'
, Because it may happen that mines are repeatedly set at the same location , So add Board[x][y] == '0'
This can be avoided .
// Set ray
void SetMine(char Board[ROWS][COLS], int row, int col) {
int count = EASY_COUNT;
while (count) {
int x = rand() % row + 1;
int y = rand() % col + 1;
if (Board[x][y] == '0') {
Board[x][y] = '1';
count--;
}
}
}
3. Statement
// Set ray
void SetMine(char Board[ROWS][COLS], int row, int col);
4. Home page
void Game() {
// Initialize the contents of the array
char Mine[ROWS][COLS] = {
0}; // Where mines are stored
char Show[ROWS][COLS] = {
0}; // The game players see
//Mine Array when there is no thunder '0'
InitBoard(Mine, ROWS, COLS, '0');
//Show When the array didn't check thunder '*'
InitBoard(Show, ROWS, COLS, '*');
// Print the game
DisplayBoard(Show, ROW, COL);
// Set ray
SetMine(Mine, ROW, COL);
}
5. Mine screening
1. Find the number of thunder
Because characters are used ASCII Stored in the form of code , therefore '1' - '0' = 1
, So just put the surrounding 8 Characters add up , Then subtract 8 individual '0'
It can be realized . This reduces the creation of temporary variables , It is better to reduce the space occupation of the stack area . Of course, the loop can also be well implemented .
int GetMineCount(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';
}
2. Check the thunder
Checking thunder is the simplest ( The dumbest ) Methods , Input coordinates one by one , Lattice by lattice elimination .
1. The outer
if
: If you traverse all the non ray lattices , Instant victory
2. The inner layer of theif
: If you step on a mine and explode ( Game over ), And print out the mine location . Cannot search repeatedly . The number of mines around the non mine grid you are looking for will be displayed .
// Check the thunder
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 :>");
scanf("%d%d", &y, &x);
if (1 <= x && x <= col && 1 <= y && y <= col) {
if (Mine[x][y] == '1') {
// If you step on thunder, the game is over
DisplayBoard(Mine, ROW, COL);
printf(" You stepped on a mine !\n I'm sorry you lost !\n");
break;
} else if (Show[x][y] != '*') {
printf(" This coordinate has been checked !\n");
} else {
// Step on... If you don't
int count = GetMineCount(Mine, x, y); // There are several thunder around
Show[x][y] = count + '0';
DisplayBoard(Show, ROW, COL);
win++;
continue;
}
} else {
printf(" Wrong coordinate input !\n");
}
}
if (win == row * col - EASY_COUNT) {
DisplayBoard(Mine, ROW, COL);
printf(" Congratulations on your success !\n");
}
}
Master code
Minesweeping homepage
#include " Minesweeping declaration .h"
void menu() {
printf("***********************************\n");
printf("*********** 1.Play ************\n");
printf("*********** 0.Exit ************\n");
printf("***********************************\n");
}
void Game() {
// Initialize the contents of the array
char Mine[ROWS][COLS] = {
0}; // Where mines are stored
char Show[ROWS][COLS] = {
0}; // The game players see
//Mine Array when there is no thunder '0'
InitBoard(Mine, ROWS, COLS, '0');
//Show When the array didn't check thunder '*'
InitBoard(Show, ROWS, COLS, '*');
// Print the game
DisplayBoard(Show, ROW, COL);
// Set ray
SetMine(Mine, ROW, COL);
// Check the thunder
FindMine(Mine, Show, ROW, COL);
}
int main() {
// Set random values
srand((unsigned int) time(NULL));
int input = 0;
do {
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input) {
case 1:
printf(" Game begins !\n");
Game();
break;
case 0:
printf(" Quit the game !\n");
break;
default:
printf(" Input error !\n\n");
}
} while (input);
return 0;
}
Minesweeping inside
#include " Minesweeping declaration .h"
// Initialization interface
void InitBoard(char Board[ROWS][COLS], int rows, int cols, char set) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Board[i][j] = set;
}
}
}
// Print the game
void DisplayBoard(char Board[ROWS][COLS], int row, int col) {
printf("|----------- The Minesweeper game -----------|\n");
for (int i = 0; i <= col; i++) {
printf(" %d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++) {
printf(" %d ", i);
for (int j = 1; j <= col; j++) {
printf(" %c ", Board[i][j]);
}
printf("\n");
}
printf("|----------- The Minesweeper game -----------|\n");
}
// Set ray
void SetMine(char Board[ROWS][COLS], int row, int col) {
int count = EASY_COUNT;
while (count) {
int x = rand() % row + 1;
int y = rand() % col + 1;
if (Board[x][y] == '0') {
Board[x][y] = '1';
count--;
}
}
}
// Check the thunder
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 :>");
scanf("%d%d", &y, &x);
if (1 <= x && x <= col && 1 <= y && y <= col) {
if (Mine[x][y] == '1') {
// If you step on thunder
DisplayBoard(Mine, ROW, COL);
printf(" You stepped on a mine !\n I'm sorry you lost !\n");
break;
} else if (Show[x][y] != '*') {
printf(" This coordinate has been checked !\n");
} else {
// Step on... If you don't
int count = GetMineCount(Mine, x, y); // There are several thunder around
Show[x][y] = count + '0';
DisplayBoard(Show, ROW, COL);
win++;
continue;
}
} else {
printf(" Wrong coordinate input !\n");
}
}
if (win == row * col - EASY_COUNT) {
DisplayBoard(Mine, ROW, COL);
printf(" Congratulations on your success !\n");
}
}
// Find the number of thunder
int GetMineCount(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';
}
Minesweeping declaration
#ifndef C_Three_A_2_H
#define C_Three_A_2_H
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
#define EASY_COUNT 10
// Initialization interface
void InitBoard(char Board[ROWS][COLS], int rows, int cols, char set);
// Print the game
void DisplayBoard(char Board[ROWS][COLS], int row, int col);
// Set ray
void SetMine(char Board[ROWS][COLS], int row, int col);
// Check the thunder
void FindMine(char Mine[ROWS][COLS], char Show[ROWS][COLS], int row, int col);
// Find the number of thunder
int GetMineCount(char Mine[ROWS][COLS], int x, int y);
#endif //C_Three_A_2_H
边栏推荐
- 11.【文件的二进制写入与写出】
- 3. Build the basic model of paddlepaddle from scratch (compare with keras and pytorch)
- 如何使用正态分布变换进行配准
- Stm32f407-ov7670 (no FIFO) -onenet- upload camera pictures to onenet (EDP protocol)
- leetcode中面试题17.04.消失的数字 和 27.移除元素
- Pytorch foundation module and Practice
- 深度学习源码项目里的checkpoint
- VS+QT debug 改成release版本导出记录
- Open3d官网代码学习
- 1014 Holmes date
猜你喜欢
Amy-Tabb机器人世界手眼标定(2、实验结果)
namespace 命名空间
11.【文件的二进制写入与写出】
Examples of enumeration
Let the code solve the "linear algebra" series - polynomial summation
深度剖析 —— 数据
Pycharm common errors collection
Operating instructions for opt101 monolithic photodiode and single power supply mutual resistance amplifier
吴恩达深度学习L4W4人脸识别
Owncloud 9.0 better cross server sharing and scalability
随机推荐
In depth analysis - Data
Hetai ht32 & taojingchi tjc--t0 serial port screen learning notes
MMdetection环境搭配(cuda10.1+mmdet2.24)
Semantic segmentation, instance segmentation
Small game items - Gobang game
UNet复现及环境配置(含数据集)
Operating instructions for opt101 monolithic photodiode and single power supply mutual resistance amplifier
2020常州市程序设计小能手真题及题解
25.【判断是否是素数的方法】
C. Doremy‘s IQ
无人机的微分平坦性详细推导
5. Paddlepaddle 10 lines of code deep learning image classification (cifar)
1012数字分类
UNET reproduction and environment configuration (including dataset)
Let the code solve the "linear algebra" series - polynomial summation
1023组最小数
C. Binary String(求前缀和)
我的第一篇博客
Model definition of pytorch
深度剖析 —— 数据