当前位置:网站首页>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
边栏推荐
- Buildreoot override mechanism
- We found a huge hole in MySQL: do not judge the number of rows affected by update!!!
- Methods of downloading Foreign Periodicals
- Programming examples of stm32f1 and stm32subeide infrared receiving and decoding of NEC protocol
- 关于“2022年度网络安全教育线上培训”相关问题的复盘和说明
- Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
- [lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines
- [FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera
- Fix the problem that the AI video intelligent platform easycvr device video cannot be played
- ESP32深度睡眠电流怎样低于10uA
猜你喜欢
Paging in servlets and JSPS
【图像处理】图像直方图均衡化系统含GUI界面
8 figures | analyze Eureka's first synchronization registry
Using fuseki query when there are multiple models in TDB
运维管理系统,人性化操作体验
ctfshow-web355,356(SSRF)
Software engineering review
為什麼這麼多人轉行產品經理?產品經理發展前景如何?
图解事件坐标screenX、clientX、pageX, offsetX的区别
ESP32在电池供电时用ULP监测电池电压
随机推荐
Operation and maintenance management system, humanized operation experience
EasyNVS云管理平台功能重构:支持新增用户、修改信息等
为什么这么多人转行产品经理?产品经理发展前景如何?
ctfshow-web351(SSRF)
SQL learning notes 2
Introduction to spark (one article is enough)
Notes on probability theory
广发证券开户是安全可靠的么?怎么开广发证券账户
Postgraduate entrance examination directory link
Figure out the difference between event coordinates screenx, clientx, pagex and offsetx
盘点华为云GaussDB(for Redis)六大秒级能力
Database objects: view learning records
ctfshow-web355,356(SSRF)
We found a huge hole in MySQL: do not judge the number of rows affected by update!!!
Solve the problem that the class defined in meta-inf.services cannot be read
Esp32 - ULP coprocessor reading Hall sensor in low power mode
代码实战——从零开始搭建自己的Diffusion models/Score-based generative models
rclone配置minio及基本操作
记一次线上接口慢查询问题排查
[lingo] solve quadratic programming