In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to implement a simple minesweeping game in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The details are as follows
Before we formally talk about the content, let's talk about the rules of the minesweeping game.
The rules of the game are very simple, click on the box, if it is a landmine, the game fails, find all the landmine games to win
. You need to try your luck at first, just click on an area and you can officially start.
3. According to the existing situation, it is determined that there must be a location of thunder.
4. Further calculation, because the "1" on the right side of "2" already has subordinate thunder, so there is no lightning directly below the "2" on the right, so it can be judged that there are two mines on the left, satisfying the two "2".
5. The red circle indicates that "1" already has a definite thunder. at this time, you only need to place the mouse over "1", hold down the right button, and click the left button, and all the other squares around "1" will be clicked open. This tip is very helpful to speed up.
6. There must be a mine next to the "1" in the upper left corner, which may be in the two blocks surrounded by the red circle, which is not yet certain. But no matter which of the two is subordinate to "3", so there are already three mines around "3", and the square in the lower left corner of "3" can be sure that there is no lightning.
7. There must be a mine where the red circle is framed, because there must be three mines around "3". Whichever of the two is subordinate to the "2" next to "3", so that "2" satisfies the condition of two thunders. so the block in the lower left corner of "2" is mine-free.
8. In this way, the two blocks under "3" can be determined to be thunderous.
9. Marking out these thunderous places opens up a new world.
10. Then according to the existing conditions to determine the place where there is lightning, point out other places. At this time, we found that the problem left over in the upper left corner had been solved.
11. Repeat the above methods, constantly deduce, and finally complete mine clearance.
All right, the rules are over, now let's talk about how to achieve it:
First of all, you have to create three folders, which are the implementation of the specific functions needed by the game.c game, game.h (header files to be used) test.c (logic of the game, menus, and sequential function calls).
First take a look at test.c:
The general logic of the game is to lay mines, print out the chessboard, and then the players mine-sweep themselves. This part of the code is not difficult to understand.
# define _ CRT_SECURE_NO_WARNINGS 1#include "game.h" void game () {char mine [ROWS] [COLS] = {0}; / storing mine information 0 no mine 1 mine char show [ROWS] [COLS] = {0}; / showing users and storing mine information InitBoard (mine, ROWS, COLS,'0'); InitBoard (show, ROWS, COLS,'*'); / / laying mine SetMine (mine, ROW, COL) / / print chessboard / / DisplayBoard (mine, ROW, COL); DisplayBoard (show, ROW, COL); / / Mine FindMine (mine,show, ROW, COL);} / / Game menu interface void menu () {printf ("*\ n"); printf ("* 1. Start *\ n "); printf (" * 0. Exit * *\ n "); printf (" *\ n ");} / choose whether or not to play void test () {int input = 0; srand ((unsigned int) time (NULL)); do {menu (); printf (" Please select->; "); scanf_s ("% d ", & input); switch (input) {case 1: / / minesweeping game (); break Case 0: printf ("% d\ n", input); printf ("about to quit the game\ n"); break; default: printf ("input error, please re-enter:\ n"); break;}} while (input);} int main (void) {test (); return 0;}
Then there is game.h:
It should not be difficult to understand that I have written all the functions and code snippets into the code.
# pragma once#include#include#include # define ROW 9gamble / printed chessboard size # define COL 9Universe / printed chessboard size # define ROWS ROW+2// actual chessboard size, to prevent crossing the line # define COLS COL+2// actual chessboard size In order to prevent # define EASY_COUNT 10 shock / simple mode 10 mines and then resize / / initialize the chessboard void InitBoard according to your own needs (char board [ROWS] [COLS], int rows, int cols, char set) / / print chessboard void DisplayBoard (char mine [ROWS] [COLS], int row, int col); / / deploy mine void SetMine (char mine [ROWS] [COLS], int row, int col); / / Mine finding void FindMine (char mine [ROWS] [COLS], char show [ROWS] [COLS], int row, int col)
Then there is game.c:
One of the functions of the minesweeper game is to click on a location, which will be expanded if there is no mine around. We use recursion to achieve it here:
/ / functions that expand the chessboard void Open (char mine [ROWS] [COLS], char show [ROWS] [COLS], int x int y) {int n = get_mine_count (mine, x, y); if (n! = 0) {show [x] [y] ='0n;} else if (show [x] [y]! =') {show [x] [y] ='; int I = 0; for (I = x-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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.