当前位置:网站首页>[C language] simple implementation of mine sweeping game
[C language] simple implementation of mine sweeping game
2022-07-02 05:55:00 【SouLinya】
When implementing this little game , Every time we implement a function, we have to check whether there is bug, Function by function .
List of articles
1. Realize the idea
1.1 menu
void menu()
{
printf("********************\n");
printf("**1.play 0.exit**\n");
printf("********************\n");
}
int main()
{
srand((unsigned int)time(NULL));// The computer randomly arranges the location of thunder
int input = 0;
do
{
menu();
printf(" Please select :");
scanf("%d", &input);
switch (input)
{
case 1:
game();// Enter the game function
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice , Please reselect \n");
break;
}
} while (input);
return 0;
}
1.2 Initialize chessboard
Ideas :
(1) Because the number and use of prompts to prevent thunder around the chessboard 1 It means thunder confusion , So we need to prepare two chessboards . A checkerboard randomly arranges thunder , Another chessboard is used to check the number of Mines . character 1 It means ray , character 0 It doesn't mean ray .
(2) Considering that users settle on the edge of the chessboard , We also need to check the number of thunder , If it is 99 Array of , Count the surrounding 8 Location (s) , It will cause cross-border , So what we need to prepare is 1111 Two chessboards of ( Auxiliary statistics ), But what we show users is 99 The chessboard of .
(3) Prepare two arrays .
A chessboard is initialized to characters 0( Ray hasn't been arranged yet );
A chessboard is initialized to characters ( Ray hasn't been checked yet , The game hasn't started yet )
// This is the function called
// Initialize two arrays
// One is initialized to '0'( It means that thunder has not been arranged )
//InitBoard(mine, ROWS, COLS,'0');
// One is initialized to '*'( It means that the investigation has not started yet )
//InitBoard(show, ROWS, COLS,'*');
// Implement the function of initializing the chessboard
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;
}
}
}
1.3 Print chessboard
Note that we need to print ( Exhibition ) The chessboard of is 9x9 Of , A print is all 0( It hasn't been laid yet ) The chessboard of , A print is all * The chessboard of . After initializing the chessboard , Print it out first to see if it can be achieved .
// Print chessboard
//Display(mine, ROW, COL);
//Display(show, ROW, COL);
// Print chessboard
void Display(char board[ROWS][COLS], int row, int col)
{
// What we want to print is 9*9 The chessboard of
int i = 0;
int j = 0;
printf("---------- The Minesweeper game ---------\n");
// Optimize the chessboard -- Print the number of columns
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
// Print lines
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("---------- The Minesweeper game ---------\n");
}
Print result display :
1.4 Random arrangement of thunder
Pay attention to it 9*9 Lay thunder on the chessboard of . After the arrangement, you can put mine Print out the array to see if it is randomly arranged each time
// stay 9*9 Lay thunder on the chessboard of
void SetMine(char mine[ROWS][COLS], int row, int col)
{
// Arrangement 10 A thunder
int count = boom;
while (count)
{
int x = rand() % 9 + 1;//1-9
int y = rand() % 9 + 1;//1-9
if (mine[x][y] == '0')// If there is no thunder in this position , Only with this coordinate can we lay thunder
{
mine[x][y] = '1';
count--;
}
}
}
1.5 Check the thunder
To check thunder, we need to consider whether the coordinates entered by players are legal , Whether it has been settled , And we should start from mine Get the number of mines around the user input coordinates in the array , That is to obtain the number of mines , And then on show In the corresponding position of the array .( Notice that we get numbers , And the characters we need )
1.5.1 Get the number of surrounding mines
int get_Mine(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';
}
1.5.2 Convert numbers and characters
from ASCII It can be found in the code table ’0’ The value of is 48,’1’ The value of is 49, And our numbers 0 Namely 0,1 Namely 1, That is, the difference between numbers and characters 48, namely ’0’ Value . So if you want to convert numbers into characters , Just add the number ’0’, How to print in character form , To convert characters into numbers , Then subtract ’0’.
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win = 0;// The winning situation - Check out ROW*COL-boom
while (win<ROW*COL-boom)
{
printf(" Please enter the coordinates you want to check :");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);//1-9
if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
{
if (show[x][y] != '*')
{
printf(" This coordinate has been checked \n");
}
else if (mine[x][y] == '1')
{
printf(" unfortunately , You're killed in the blast \n");
Display(mine, ROW, COL);
break;
}
else
{
// Count how many mines are around this coordinate
// Notice that we get numbers 3, What we want is characters '3', Can be saved to the character array show in
int count= get_Mine(mine, x, y);
show[x][y] = count + '0';
Display(show, ROW, COL);
win++;
}
}
else
{
printf(" Illegal coordinate input , Please re-enter \n");
}
}
if (win == ROW * COL - boom)
{
printf(" congratulations ! Mine clearance is successful !\n");
Display(mine, ROW, COL);
}
}
Judge the user's successful demining
Namely discharge win==ROW*COL- The number of ray
Two 、 Code implementation
The header file game.h
// The header file
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
// Display chessboard
#define ROW 9
#define COL 9
// Auxiliary chessboard
#define ROWS ROW+1
#define COLS COL+2
// If you want to set up a few mines, set up a few mines
#define boom 70
// Function declaration
// Initialize chessboard
void InitBoard(char board[ROWS][COLS], int rows, int cols,char set);
// Print chessboard
void Display(char board[ROWS][COLS], int row, int col);
// Arrange thunder Lay out Ray's array
void SetMine(char mine[ROWS][COLS], int row, int col);
// Check the thunder
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
The realization of game logic game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
// Implement the function of initializing the chessboard
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;
}
}
}
// Print chessboard
void Display(char board[ROWS][COLS], int row, int col)
{
// What we want to print is 9*9 The chessboard of
int i = 0;
int j = 0;
printf("---------- The Minesweeper game ---------\n");
// Optimize the chessboard -- Print the number of columns
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
// Print lines
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("---------- The Minesweeper game ---------\n");
}
// stay 9*9 Lay thunder on the chessboard of
void SetMine(char mine[ROWS][COLS], int row, int col)
{
// Arrangement 10 A thunder
int count = boom;
while (count)
{
int x = rand() % 9 + 1;//1-9
int y = rand() % 9 + 1;//1-9
if (mine[x][y] == '0')// If there is no thunder in this position , Only with this coordinate can we lay thunder
{
mine[x][y] = '1';
count--;
}
}
}
int get_Mine(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';
}
// Check the thunder
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int win = 0;// The winning situation - Check out ROW*COL-boom
while (win<ROW*COL-boom)
{
printf(" Please enter the coordinates you want to check :");
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);//1-9
if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
{
if (show[x][y] != '*')
{
printf(" This coordinate has been checked \n");
}
else if (mine[x][y] == '1')
{
printf(" unfortunately , You're killed in the blast \n");
Display(mine, ROW, COL);
break;
}
else
{
// Count how many mines are around this coordinate
// Notice that we get numbers 3, What we want is characters '3', Can be saved to the character array show in
int count= get_Mine(mine, x, y);
show[x][y] = count + '0';
Display(show, ROW, COL);
win++;
}
}
else
{
printf(" Illegal coordinate input , Please re-enter \n");
}
}
if (win == ROW * COL - boom)
{
printf(" congratulations ! Mine clearance is successful !\n");
Display(mine, ROW, COL);
}
}
Functions to implement the game test.c
#include"game.h"
void menu()
{
printf("********************\n");
printf("**1.play 0.exit**\n");
printf("********************\n");
}
void game()
{
// Lay out Ray's array
char mine[ROWS][COLS] = {
0 };
// Check Ray's array
char show[ROWS][COLS] = {
0 };
// Initialize two arrays
// One is initialized to '0'( It means that thunder has not been arranged )
InitBoard(mine, ROWS, COLS,'0');
// One is initialized to '*'( It means that the investigation has not started yet )
InitBoard(show, ROWS, COLS,'*');
// Print chessboard
Display(mine, ROW, COL);
Display(show, ROW, COL);
// Arrange thunder
SetMine(mine, ROW, COL);
Display(mine, ROW, COL);
// Check the thunder
FindMine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));// The computer randomly arranges the location of thunder
int input = 0;
do
{
menu();
printf(" Please select :");
scanf("%d", &input);
switch (input)
{
case 1:
game();// Enter the game function
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice , Please reselect \n");
break;
}
} while (input);
return 0;
}
边栏推荐
- Cube magique infini "simple"
- VSCode paste image插件保存图片路径设置
- Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)
- c语言中的几个关键字
- Vite打包后的dist不能直接在浏览器打开吗
- Taskbar explicit / implicit toggle function
- Opencv LBP features
- 15 C language advanced dynamic memory management
- Test case
- 3D printer G code command: complete list and tutorial
猜你喜欢
Keepalived installation, use and quick start
Fundamentals of software testing
Vscode paste image plugin saves image path settings
软件测试 - 概念篇
脑与认知神经科学Matlab Psytoolbox认知科学实验设计——实验设计四
PHP 开发与测试 Webservice(SOAP)-Win
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
5g market trend in 2020
Vite打包后的dist不能直接在浏览器打开吗
数理统计与机器学习
随机推荐
Balsamiq wireframes free installation
页面打印插件print.js
Typora installation (no need to enter serial number)
Eco express micro engine system has supported one click deployment to cloud hosting
How vite is compatible with lower version browsers
Mathematical statistics and machine learning
PHP obtains some values in the string according to the specified characters, and reorganizes the remaining strings into a new array
ThreadLocal memory leak
PHP 开发与测试 Webservice(SOAP)-Win
3D printer G code command: complete list and tutorial
Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan
Cambrian was reduced by Paleozoic venture capital and Zhike shengxun: a total of more than 700million cash
PHP parent
51单片机——ADC讲解(A/D转换、D/A转换)
Cube magique infini "simple"
Redis Key-Value数据库 【高级】
php按照指定字符,获取字符串中的部分值,并重组剩余字符串为新的数组
软件测试答疑篇
I want to understand the swift code before I learn it. I understand it
Grbl software: basic knowledge of simple explanation