当前位置:网站首页>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;
}
边栏推荐
- Know the future of "edge computing" from the Nobel prize!
- libpng12.so.0: cannot open shared object file: No such file or directory 亲测有效
- Deeply understand the underlying implementation principle of countdownlatch in concurrent programming
- HDU - 1024 Max Sum Plus Plus(DP)
- Educational administration management system (free source code)
- 2022 the 8th China International "Internet +" college student innovation and entrepreneurship competition industry proposition track is open for registration!
- Trust guessing numbers game
- Qt编写自定义控件-自绘电池
- tese_ Time_ 2h
- 论文学习记录随笔 多标签之GLOCAL
猜你喜欢

Educational administration management system (free source code)

Call us special providers of personal cloud services for College Students

Leetcode top 100 question 2 Add two numbers

Dear pie users, I want to confess to you!

从底层结构开始学习FPGA----RAM IP的定制与测试

Chapitre d'apprentissage mongodb: Introduction à la première leçon après l'installation

激活函数简述

我从技术到产品经理的几点体会

Deeply understand the underlying implementation principle of countdownlatch in concurrent programming

Debug details under pycharm
随机推荐
如何添加葫芦儿派盘
How to add a gourd pie plate
Why use huluer pie disk instead of U disk?
RecycleView的一些使用
Is it safe for a novice to open a securities account?
It's not that you have a bad mind, but that you haven't found the right tool
【考研高数 自用】高数第一章基础阶段思维导图
Daily code 300 lines learning notes day 11
excel高级绘图技巧100讲(一)-用甘特图来展示项目进度情况
Fiber Bragg grating (FBG) notes [1]: waveguide structure and Bragg wavelength derivation
SSGSSRCSR区别
基于TI DRV8424驱动步进电机实现调速和行程控制
这才是大学生必备软件 | 知识管理
[excel] column operation, which performs specific column for data in a cell, such as text division by comma, colon, space, etc
Summary of common components of applet
This is the necessary software for college students 𞓜 knowledge management
Oracle 序列+触发器
新手在挖财开通证券账户安全吗?
[SRS] use of Vhost isolated stream: push / pull Stream Address
Data governance: data governance management (Part V)