当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

Why use huluer pie disk instead of U disk?

This is the necessary software for college students 𞓜 knowledge management

scope 数据导出mat

Ssm+mysql second-hand trading website (thesis + source code access link)

论文学习记录随笔 多标签之GLOCAL

从MLPerf谈起:如何引领AI加速器的下一波浪潮

基于TI DRV8424驱动步进电机实现调速和行程控制

Daily code 300 lines learning notes day 11

Basic electrician knowledge 100 questions

论文学习记录随笔 多标签之LSML
随机推荐
Simple implementation of database connection pool
Qt编写自定义控件-自绘电池
4GB大文件,如何实时远程传输和共享?
win10、win11中Elan触摸板滚动方向反转、启动“双指点击打开右键菜单“、“双指滚动“
Fiber Bragg grating (FBG) notes [1]: waveguide structure and Bragg wavelength derivation
idea启动查看项目端口
Boot + jsp University Community Management System (with source Download Link)
Advanced cross platform application development (III): online resource upgrade / hot update with uni app
Crossing sect · paipan + Siyuan notes = private notebook
千万不要把笔记视频乱放!
[excel] column operation, which performs specific column for data in a cell, such as text division by comma, colon, space, etc
First defined here occurs during QT compilation. Causes and Solutions
Ssm+mysql second-hand trading website (thesis + source code access link)
In depth understanding of condition source code interpretation and analysis of concurrent programming
HCM 初学 ( 四 ) - 时间
新手在挖财开通证券账户安全吗?
OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow
Chapitre d'apprentissage mongodb: Introduction à la première leçon après l'installation
vsCode函数注解/文件头部注解快捷键
穿越派·派盘 + Mountain Duck = 数据本地管理