catalogue
4. Just run the program according to the prompts
preface:● realize the application and understanding of array and other knowledge points through the programming of mine sweeping game (primary).
● due to the limited level of the author, it is inevitable that there are fallacies in the article. Please correct the slang, and sincerely hope to give advice!
Text:
Note: the code compilation environment is visual studio 2019, in which the code statement specifically set for the compiler appears: #define_CRT_SECURE_NO_WARNINGS
Function of this code statement: to solve the error or warning problems of some library functions in the Visual Studio compilation environment
Note: this code statement is only used in the Visual Studio compilation environment. Other compilers need to mask this code statement
--------
1. Create header file: game.h The code is as follows:
#define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 #include <stdio.h> #include<stdlib.h> #include<time.h> void InitBoard(char board[ROWS][COLS], int rows, int cols,char set); void DisplayBoard(char board[ROWS][COLS], int row, int col); void SetMine(char board[ROWS][COLS], int row, int col); void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
The code is as follows:
#define _CRT_SECURE_NO_WARNINGS #include"game.h" 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; //'*' } } } void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; //Print column number for (i = 0; i <= col; i++) { printf("%d", i); } printf("\n"); for (i = 1; i <= row; i++) { printf("%d", i); for (j = 1; j <= col; j++) { printf("%c", board[i][j]); } printf("\n"); } } void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count) { int x = rand() % row + 1; //1-9 int y = rand() % col + 1; //1-9 if (board[x][y] == '0') { board[x][y] = '1'; count--; } } } //'0'-'0'=0 //'1'-'0'=1 //'3'-'0'=3 int get_mine_count(char mine[ROWS][COLS], int x, int y) { return mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0'; } void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x = 0; int y = 0; int win = 0; //If 9 * 9-10 = 71 while(win<row*col-EASY_COUNT) //while (1) this statement is replaced by the previous while statement { printf("Please enter the coordinates of the mine:>"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y <= col) { //Coordinate method //1. Stepping on thunder if (mine[x][y] == '1') { printf("I'm sorry, you were killed!\n"); DisplayBoard(mine, row, col); break; } else //Not ray { //How many mines are there around the X and Y coordinates int count = get_mine_count(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, row, col); win++; } } else { printf("The coordinates entered are illegal, please re-enter!\n"); } } if (win == row * col - EASY_COUNT) { printf("Congratulations on your success!\n"); DisplayBoard(mine, row, col); } } //Recursion is used in the de expansion function //1. The coordinates are not thunder //2. There is no thunder around the coordinates
3. Create source file: test.c
The code is as follows:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include"game.h" void menu() { printf("*********************\n"); printf("******* 1.Play *****\n"); printf("******* 0.Exit *****\n"); printf("*********************\n"); } void game() { //Mine information storage //1. Information of arranged mine char mine[ROWS][COLS] = { 0 }; //11*11 //2. Mine detection information char show[ROWS][COLS] = { 0 }; //initialization InitBoard(mine, ROWS, COLS,'0'); InitBoard(show, ROWS, COLS,'*'); //Print chessboard DisplayBoard(mine, ROW, COL); DisplayBoard(show, ROW, COL); //printf("mine clearance \ n"); //Lay thunder SetMine(mine, ROW, COL); DisplayBoard(mine, ROW, COL); //Real demining will shield this statement, which can check the location coordinates of mines //mine clearance FindMine(mine, show, ROW, COL); } void test() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("Please select the start or end of the game:>\n"); printf("#Enter the number 1 for the start of the game and the number 0 for the end of the game#\n"); printf("#Coordinate input method: Digital-Space bar-number#\n"); scanf("%d",&input); switch (input) { case 1: game(); break; case 0: printf("Exit the game\n"); break; defult: printf("Wrong selection, reselect!\n"); break; } } while (input); } int main() { test(); return 0; }4. Just run the program according to the prompts
Example of operation results:
instructions:(1) The red box marking statement can control the display and hiding of the position coordinates of "mine":
(2) The red box statement can change the number of "mines":
Since the chessboard size set in this program is 9 * 9, the number range here is [0,81],
(3) The checkerboard size can be changed by the red box marking statement:
Change the number 9. The number 9 represents the operation size of the chessboard, and ROW+2 represents the total size of the chessboard,
That is, the operation chessboard size is 9 * 9, and the total chessboard size (plus the outer frame) is 11 * 11
Through the preparation and learning of mine sweeping (primary) game program, as a novice in learning programming, the author's current level is limited and can only draw gourds and ladles according to gourds. The knowledge contained in it is very profound, and the author only understands one or two! Although it is only compiled into the compiler according to the gourd painting ladle, the debugging link does let the author learn a lot of knowledge!
Due to some reasons, this paper has many shortcomings, many of which still need the author to learn and understand repeatedly. I hope that in the future, the author can grow together with the readers!
Here, I pay tribute to the first developer to write mine clearance game code.
At the same time, I am also very grateful to those who move forward together on the road of life. No matter whether they meet or not, they have cured this lost and lonely heart, although it is very short!
Today is the sixth day of the National Day holiday and the sixth day of staying in school during the National Day holiday. As a "sacrificial person" during the holiday, I have mixed feelings, but I am honored to learn with you. Although our communication is only through a network cable, it has already gone beyond the physical boundary and is greeting across time, space and region: Hello! Friends who are reading this article now and in the future! I wish you success in your studies, down-to-earth and your dreams come true!
Bless the 72nd anniversary of the founding of the motherland!
--------
—— Since New dawn • old acquaintance