当前位置:网站首页>C language beginner level - realize the minesweeping game
C language beginner level - realize the minesweeping game
2022-07-01 05:46:00 【Xiao LV LC】
List of articles
C Language to achieve mine sweeping game
Introduction to the rules
I believe many people have played mine sweeping games , But Xiaobian still tells us the rules of this minesweeping game .
This is the first interface of a minesweeping game

Players find all the thunder arranged by the computer in these squares, that is, the game victory , Point to thunder and be killed .
The rules are simple , Now let's start with what we have learned C Language knowledge to achieve it .
C Language code implementation ideas
This program is divided into three parts ,game.h In the file, we write some header files and interface function declarations ,game.c In the file, we write specific interface functions ,test.c Write out the main functions, game menus and other functions in the file .
First we print a game menu
void menu()
{
printf("**********************\n");
printf("******* 1. play ******\n");
printf("******* 0. exit ******\n");
printf("**********************\n");
}
Players choose to enter the game
The following is the specific implementation framework of the game
void game()
{
char mine[ROWS][COLS] = {
0 };// Store the information of the arranged mine
char show[ROWS][COLS] = {
0 };// Store the information of the detected mine
// Initializes the number to the specified element
//mine The array is always when there is no thunder ‘0’
InitBoard(mine, ROWS, COLS,'0');
//show The array is always ‘*’
InitBoard(show, ROWS, COLS, '*');
// Arrange thunder
SetMine(mine, ROW, COL);
//
// Print chessboard
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
This is the implementation of the main function
int main()
{
int input=0;
// Set the starting point for generating random numbers
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(" Input error \n");
break;
}
} while (input);
return 0;
}
Next, we implement the specific interface function
First, we initialize the chessboard
// Initialize 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
// Print chessboard
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("---------- The Minesweeper game --------\n");
for (j = 0;j <= col;j++) // Print column number
{
printf("%d ", j);
}
printf("\n");
for (i = 1;i <= row;i++)
{
printf("%d ", i); // Print line number
for (j = 1;j <= col;j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("---------- The Minesweeper game --------\n");
}
Arrange thunder
// Arrange thunder
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--;
}
}
}
Returns the number of mines around the selected coordinates
int get_mine_count(char board[ROWS][COLS],int x,int y)
{
return (board[x - 1][y - 1] +
board[x - 1][y] +
board[x - 1][y + 1] +
board[x][y - 1] +
board[x][y + 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] - 8 * '0');
}
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;// Find the number of non thunder
while (win<row*col-EASY_COUNT)
{
printf(" Please enter the coordinates of the mine to be checked :");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf(" The coordinates have been checked , You can't check again !\n");
}
else
{
// If it's ray
if (mine[x][y] == '1')
{
printf(" unfortunately , You're killed in the blast \n");
DisplayBoard(mine, ROW, COL);
break;
}
else // If it wasn't ray
{
// Statistics mine Array of x y There are several mines around the coordinates
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0'; // Convert to numeric characters
DisplayBoard(show, ROW, COL);
}
}
}
else
printf(" The coordinates entered are illegal , Please re-enter !\n");
}
if (win == row * col - EASY_COUNT)
{
printf(" congratulations , Mine clearance is successful \n");
DisplayBoard(mine, ROW, COL);
}
}

Complete implementation code
game.h
#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 // The number of mines
void InitBoard(char board[ROWS][COLS],int rows,int cols,char set); // Initialize chessboard
avoid DisplayBoard(char board[ROWS][COLS], int row, int col);// Print chessboard
void SetMine(char board[ROWS][COLS], int row, int col);// Arrange thunder
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row,int col);// Check the thunder
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
// Initialize 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 DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("---------- The Minesweeper game --------\n");
for (j = 0;j <= col;j++) // Print column number
{
printf("%d ", j);
}
printf("\n");
for (i = 1;i <= row;i++)
{
printf("%d ", i); // Print line number
for (j = 1;j <= col;j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("---------- The Minesweeper game --------\n");
}
// Arrange thunder
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--;
}
}
}
int get_mine_count(char board[ROWS][COLS],int x,int y)
{
return (board[x - 1][y - 1] +
board[x - 1][y] +
board[x - 1][y + 1] +
board[x][y - 1] +
board[x][y + 1] +
board[x + 1][y - 1] +
board[x + 1][y] +
board[x + 1][y + 1] - 8 * '0');
}
// 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;// Find the number of non thunder
while (win<row*col-EASY_COUNT)
{
printf(" Please enter the coordinates of the mine to be checked :");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (show[x][y] != '*')
{
printf(" The coordinates have been checked , You can't check again !\n");
}
else
{
// If it's ray
if (mine[x][y] == '1')
{
printf(" unfortunately , You're killed in the blast \n");
DisplayBoard(mine, ROW, COL);
break;
}
else // If it wasn't ray
{
// Statistics mine Array of x y There are several mines around the coordinates
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0'; // Convert to numeric characters
DisplayBoard(show, ROW, COL);
}
}
}
else
printf(" The coordinates entered are illegal , Please re-enter !\n");
}
if (win == row * col - EASY_COUNT)
{
printf(" congratulations , Mine clearance is successful \n");
DisplayBoard(mine, ROW, COL);
}
}
test.c
#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 the information of the arranged mine
char show[ROWS][COLS] = {
0 };// Store the information of the detected mine
// Initializes the number to the specified element
//mine The array is always when there is no thunder ‘0’
InitBoard(mine, ROWS, COLS,'0');
//show The array is always ‘*’
InitBoard(show, ROWS, COLS, '*');
// Arrange thunder
SetMine(mine, ROW, COL);
//
// Print chessboard
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
int main()
{
int input=0;
// Set the starting point for generating random numbers
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(" Input error \n");
break;
}
} while (input);
return 0;
}
边栏推荐
- Vscode function annotation / file header annotation shortcut
- Data governance: metadata management implementation (Part IV)
- 穿越派 你的数据云行
- Crossing sect · paipan + Siyuan notes = private notebook
- Leetcode Max rectangle, Max square series 84 85. 221. 1277. 1725. (monotonic stack, dynamic programming)
- In win10 and win11, the scroll direction of Elan touch panel is reversed, and "double finger click to open the right-click menu" and "double finger scroll" are started“
- 健康照明中应用的LED照明灯
- 穿越派·派盘 + 思源笔记 = 私人笔记本
- 2/15 (awk, awk conditions, awk processing design can perform additional tasks, and use awk array +for loop to realize advanced search)
- Typeorm framework
猜你喜欢

Continue to learn MySQL

喊我们大学生个人云服务特供商

Basic electrician knowledge 100 questions

boot+jsp的高校社团管理系统(附源码下载链接)

Leetcode Max rectangle, Max square series 84 85. 221. 1277. 1725. (monotonic stack, dynamic programming)

Deeply understand the underlying implementation principle of countdownlatch in concurrent programming

Crossing sect · paipan + Siyuan notes = private notebook

Crossing pie · pie pan + Mountain duck = local data management

【考研高数 武忠祥+880版 自用】高数第二章基础阶段思维导图

JDBC common interview questions
随机推荐
mysql 将毫秒数转为时间字符串
OpenGL ES: (5) OpenGL的基本概念、OpenGL ES 在屏幕产生图片的过程、OpenGL管线(pipeline)
OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow
Some errors encountered in MySQL data migration
Is it safe for a novice to open a securities account?
C语言初阶——实现扫雷游戏
码蹄集 - MT3114 · 有趣的平衡 - 用样例通俗地讲解
Know the future of "edge computing" from the Nobel prize!
OpenGL ES: (1) OpenGL ES的由来 (转)
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
数据库连接池的简单实现
数据治理:数据治理管理(第五篇)
Deeply understand the underlying implementation principle of countdownlatch in concurrent programming
HCM 初学 ( 三 ) - 快速输入PA70、PA71 浏览员工信息PA10
为了保护自己的数据,他奋斗了一天一夜
Dear pie users, I want to confess to you!
How to transmit and share 4GB large files remotely in real time?
Leetcode Max rectangle, Max square series 84 85. 221. 1277. 1725. (monotonic stack, dynamic programming)
论文学习记录随笔 多标签之GLOCAL
如何添加葫芦儿派盘