Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize self-defined Minesweeper Game in C language

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article "C language how to achieve custom minesweeper game" is not understood by most people, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "C language how to achieve custom minesweeper game" article.

Realization process

For the realization of minesweeping game in C language, the game process can be divided into two parts.

Functions to realize the key functions of the game

Build a reasonable game process

Realize the key functions of the game

In order to make the game function easy to manage and type, first we create a header file, and mine.h declares the game function.

Then create a corresponding source file, mine.c, to define these functions.

For the game function, the first thing that comes to mind is to build a target specification of the mineplate, that is, a two-dimensional array.

In order to make the game more playable, the specifications of the mineplate should be customized. So in the mine.h header file, you should first use define to define the ROW (row) and column (column) identifiers for easy customization.

# define ROW 15 / / define their specifications as 15*15#define COL 15#define ROWS ROW+2#define COLS COL+2 first

Why define the last two macros?

For the realization of the core function of the mineplate, it must be able to accurately count the number of nine Gray around the coordinates after the player chooses the coordinates (if the location is not mine). So how to accurately judge the place at the boundary?

So define the ROW+2,COL+2 size of the real thunderhead array specification, and randomly place the lightning in the ROW,COL specification thunder plate to make the game result accurate.

Define initialization of mineplate and display panel

Define two two-dimensional arrays of the same specification, the former is a real minefield, using'1' and'0' to represent mine and no mine, and the latter is a display area for player operation and result display.

Void init (char arr [ROWS] [COLS], int rows, int cols, char type) / / memset batch operation is passed a pointer, that is, the array start address {memset (arr, type, rows*cols * sizeof (mine [0] [0]));}

Randomly place mines in the display area

Void setmine (char mine [ROWS] [COLS], int count) {srand ((unsigned) time (NULL)); int I = 0; int j = 0; while (count) {I = (rand ()% ROW) + 1; / change the random value 0-9 to 1-10 to realize random mine release in the operating area j = (rand ()% COL) + 1 If (mine [I] [j] = ='0') {mine [I] [j] = '1century; count--;}

Print interface

Void display (char board [ROWS] [COLS]) {/ / starting from the first row and column, the causative element int I = 0; int j = 0; printf ("\ n |"); for (I = 1; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report