当前位置:网站首页>C language implementation [minesweeping game] full version (implementation source code)
C language implementation [minesweeping game] full version (implementation source code)
2022-07-01 07:02:00 【Zhou Zhouwang】
Catalog
Rules of the game
Everyone has never played mine sweeping and has heard of it , It looks like this 
The rules of the game
You point to a number , If it is 3, That means the one closest to it 8 There are 3 A thunder . Then judge which is thunder by the intersection of adjacent or alternate numbers . If you are sure that a grid is Ray , Just right click on him ; If it wasn't ray , Just click the left button to exclude . When you find all the mines , Congratulations on your victory !
Some points to pay attention to
- We actually need two chessboards , One is a chessboard with thunder on the ground floor , This chessboard is used to record the location of Mines , Which are safe locations ; The other is a chessboard displayed to the players , The initial position of this chessboard is unknown , As players explore , Step by step , This location is mine , It's still a number to indicate how many mines are around , What is unexplored .
- If we want to play a 10x10 Mine clearance , So the actual size of the chessboard we want to design at the bottom should be 12x12 Of , One more circle , The purpose is to mark the information in the outermost circle , If not more circles , Then it's out of bounds .
- Our version involves a recursive expansion , When there is no thunder for a week, it will automatically expand recursively for you .
- It also involves methods to prevent stepping on the thunder in the first step , If you judge your misfortune, the first step is to step on the thunder , The bottom layer will automatically change the mine to an empty position at random , Prevent you from taking the first step Gameover.
- The disadvantage is that we have a black window , It's ugly , You can design more beautiful , Even networked , Non windowed .
Implementation code
We need a game.h The header file , Include the declaration of all required function interfaces
#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
// Initialize chessboard
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
// Show chessboard
void DisplayBoard(char board[ROWS][COLS], int row, int col);
// Arrange thunder
void SetMine(char board[ROWS][COLS], int row, int col);
// Check Ray's
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
// an
void OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
// Prevent stepping on mines in the first step
void Prevent_jinx(char mine[ROWS][COLS], int x, int y);
// Statistical security zone * The number of
int Countshow(char board[ROWS][COLS], int row, int col, char sign);
And then we were in game.c Definition of implementation function in
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
printf("------------------------\n");
for (i = 0; i <= 9; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("------------------------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
//1. Generate random subscripts
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
void Prevent_jinx(char mine[ROWS][COLS], int x, int y)
{
int m, n;
while (1)
{
m = rand() % ROW + 1;
n = rand() % COL + 1;
if (mine[m][n] != '1')
{
mine[m][n] = '1';
mine[x][y] = '0';
break;
}
}
}
void OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
if (x >= 1 && x <= ROW && y >= 1 && y <= COL && mine[x][y] != '1') //x,y Within the scope of law , And it's not a minefield
{
int count = GetMineCount(mine, x, y);
if (count == 0) // If count=0, Then we need to continue to expand mine finding
{
show[x][y] = '0';
int i, j;
// Traverse the surrounding eight points in turn
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (show[i][j] != '*') continue;
// If it has been marked, you do not need to enter recursion , Avoid duplicate searches
OpenMine(mine, show, i, j);
}
}
}
else //count Not for 0 Stop expanding to find thunder
{
show[x][y] = count + '0';
}
}
else return;
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
int i, j, count = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (mine[i][j] == '1')
count++;
}
}
return count;
}
int Countshow(char board[ROWS][COLS], int row, int col, char sign)
{
int count = 0, i, j;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (board[i][j] == sign)
count++;
}
}
return count;
}
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 ( Lines The first few columns ):>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (win == 0 && mine[x][y] == '1')// If you step on thunder as soon as you come up
{
Prevent_jinx(mine, x, y);
}
if (mine[x][y] == '1')
{
printf(" unfortunately , You're killed in the blast (1 It's the location of the bomb )\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
OpenMine(mine, show, x, y);
win = ROW * COL - Countshow(show, ROW, COL, '*');
if (Countshow(show, ROW, COL, '*') == EASY_COUNT)
{
printf(" congratulations , Mine clearance is successful (1 It's the location of the bomb )\n");
DisplayBoard(mine, ROW, COL);
break;
}
DisplayBoard(show, ROW, COL);
}
}
else
{
printf(" Illegal coordinates , Re input \n");
}
}
}
Finally, we test.c Implementation test in
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("********************************\n");
printf("********* 1. play ********\n");
printf("********* 0. exit ********\n");
printf("********************************\n");
}
void game()
{
char mine[ROWS][COLS] = {
0 };// Store Ray's information
char show[ROWS][COLS] = {
0 };// Store the information of the detected mine
// Initialize the chessboard
InitBoard(mine, ROWS, COLS, '0');//'0'
InitBoard(show, ROWS, COLS, '*');//'*'
// Arrange thunder
SetMine(mine, ROW, COL);
DisplayBoard(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 select :>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice , To choose !\n");
break;
}
} while (input);
return 0;
}
Effect display

Thank you for reading , See you next time
If there is a mistake Welcome to exchange
边栏推荐
- 【LINGO】求解二次规划
- 在长城证券上做基金定投安全吗?
- LeetCode+ 71 - 75
- Webapck packaging principle -- Analysis of startup process
- ESP32 ESP-IDF GPIO按键中断响应
- JSP - paging
- [the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
- buildroot override 机制
- 清除过期缓存条目后可用空间仍不足 - 请考虑增加缓存的最大空间
- 问题解决:OfficeException: failed to start and connect(一)
猜你喜欢

How to use Alibaba vector font files through CDN
![[lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines](/img/34/d2efae5b283cdc130d55f52cdff76d.png)
[lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines
![[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization](/img/49/719496e014f4766d22aba44dbed19e.png)
[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization

解决无法读取META-INF.services里面定义的类

图解事件坐标screenX、clientX、pageX, offsetX的区别

Product learning (II) - competitive product analysis

开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开

EasyNVS云管理平台功能重构:支持新增用户、修改信息等

Code practice - build your own diffusion models / score based generic models from scratch

Fix the problem that the AI video intelligent platform easycvr device video cannot be played
随机推荐
清除过期缓存条目后可用空间仍不足 - 请考虑增加缓存的最大空间
AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复
rclone配置minio及基本操作
Rclone access web interface
广发证券开户是安全可靠的么?怎么开广发证券账户
Is the account opening of GF Securities safe and reliable? How to open GF Securities Account
On whether variables are thread safe
[matlab] solve nonlinear programming
【计网】(一) 集线器、网桥、交换机、路由器等概念
图像风格迁移 CycleGAN原理
為什麼這麼多人轉行產品經理?產品經理發展前景如何?
Insufficient free space after clearing expired cache entries - consider increasing the maximum cache space
关于“2022年度网络安全教育线上培训”相关问题的复盘和说明
如何通过cdn方式使用阿里巴巴矢量图字体文件
Webapck packaging principle -- Analysis of startup process
(上)苹果有开源,但又怎样呢?
Router 6/ 以及和Router5 的区别
Terminology description in the field of software engineering
在支付宝上买基金安全吗?哪里可以买基金
比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛