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 use C language to realize Minesweeper Mini Game

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use C language to achieve minesweeper Mini Game". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought slowly and deeply. Let's study and learn how to use C language to achieve minesweeper Mini Game!

The details are as follows

Rules of the classic minesweeping game:

When the player clicks on the game area, there are several mines in the eight locations around that area (if it is a corner, it is the number of mines in the surrounding three positions, and the edge position is the same), the number is displayed. If the place is Ray, then end the game directly.

Realize the basic idea with C language:

Suppose we want to realize the minesweeping of a 9 / 9 chessboard, in order to calculate the number of mines at the edge or corner, we can turn the minefield into a 12 / 12 chessboard and all the outermost chessboards into 0, so that no matter where the mine is, we can calculate the sum of the number of mines in the 8 positions around it.

1. Set the game menu and let the user input data to choose whether to play the game or not.

2. First, create two two-dimensional arrays, one to store minefield information, and the other to show the chessboard to the user. (here, you can directly create two arrays of 12 to 12, and finally show only the part 9 to 9 to the user)

3. Initialize the two chessboards respectively

4. Begin to randomly generate mines on the chessboard where the minefield information is stored.

5 、. Show the chessboard to the user.

6. Let the user enter the coordinates and judge. If it is thunder, the game ends directly. If not, the number of surrounding mines is calculated and the data is displayed on the chessboard.

# define _ CRT_SECURE_NO_WARNINGS#include # define row 9 / / Macro definition data convenience is not limited to playing 9x9 minesweeping # define col 9#define rows row + 2#define cols col+ 2 / / creating game menu.void menu () {printf ("* *\ n"); printf ("* 1. Start the game *\ n "); printf (" * 2. Quit the game *\ n "); printf (" * *\ n ");} / / initialize the interface void Init (char arr [] [cols], int a, int bdyerchar c) {for (int I = 0; I < a * * ife +) {for (int j = 0; j < b) For +) {arr [I] [j] = c;} / print chessboard (minefield display) void print (char arr [rows] [cols]) {for (int I = 0; I < rows; iTunes +) {for (int j = 0; j < cols; jacks +) {printf ("% c", arr [I] [j]);} printf ("\ n") }} / / set minefield void leiqu (char arr [rows] [cols]) {srand ((unsigned int) time (0)); int count = 10; while (count) {int x = rand ()% 9 + 1; int y = rand ()% 9 + 1; if (arr [x] [y] = ='0') {arr [x] [y] ='1; count-- } / / print chessboard (user display) void showboard (char arr [rows] [cols]) {for (int a = 0; a)

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