当前位置:网站首页>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;
}
边栏推荐
- First defined here occurs during QT compilation. Causes and Solutions
- How to transmit and share 4GB large files remotely in real time?
- 激活函数简述
- bat操作ftp上传下载命令
- POL8901 LVDS转MIPI DSI 支持旋转图像处理芯片
- 穿越派 你的数据云行
- 数据治理:数据治理框架(第一篇)
- 2022.6.30-----leetcode. one thousand one hundred and seventy-five
- Why use huluer pie disk instead of U disk?
- tese_Time_2h
猜你喜欢

【医学分割】u2net

基于微信小程序的青少年生理健康知识小助手(免费获取源码+项目介绍+运行介绍+运行截图+论文)

Typeorm framework

mysql 将毫秒数转为时间字符串

千万不要把笔记视频乱放!

Learn the customization and testing of fpga---ram IP from the bottom structure

Speed regulation and stroke control based on Ti drv8424 driving stepper motor

π盘,让您电脑变成个人的私有云

ssm+mysql二手交易网站(论文+源码获取链接)

Know the future of "edge computing" from the Nobel prize!
随机推荐
【QT】qt加减乘除之后,保留小数点后两位
OpenGL ES: (5) OpenGL的基本概念、OpenGL ES 在屏幕产生图片的过程、OpenGL管线(pipeline)
为了保护自己的数据,他奋斗了一天一夜
Why use huluer pie disk instead of U disk?
[ffmpeg] [reprint] image mosaic: picture in picture with wheat
College community management system based on boot+jsp (with source code download link)
表格中el-tooltip 实现换行展示
Continuous breakthrough and steady progress -- Review and Prospect of cross platform development technology of mobile terminal
Data governance: metadata management implementation (Part IV)
Leetcode top 100 questions 1 Sum of two numbers
Idea start view project port
【知识点总结】卡方分布,t分布,F分布
C语言初阶——牛客网精选好题
喊我们大学生个人云服务特供商
我从技术到产品经理的几点体会
[SRS] use of Vhost isolated stream: push / pull Stream Address
论文学习记录随笔 多标签之GLOCAL
健康照明中应用的LED照明灯
OneFlow源码解析:算子签名的自动推断
穿越派·派盘 + 思源笔记 = 私人笔记本