"Minesweeper": a popular puzzle game, the goal of the game is to find all non-mine grids in the shortest time according to the numbers that appear on the clicked grid, and at the same time avoid stepping on the thunder, stepping on a thunder will lose the whole game.
Without further ado, let's get straight to the point.
First, we have to display a menu bar for the user to choose from, so let's write the menu function first:
void menu() { printf("********************\n"); printf("********Minesweeper********\n"); printf("** 1.paly 0.exit **\n"); printf("********************\n"); printf("Please select:"); }
After the menu is available, it is necessary to write the main function:
intmain() { int select = 0;//Initialize the variable select to receive player input do{ menu();//First display the menu except what we just wrote scanf("%d",&select);//Receive player input switch(select) { case 1://player input 1, enter the game directly game(); break; case 0://If the player enters 0, exit the game directly exit(0); default://The player has entered other characters, prompting the player to enter an error printf("Error!please try again!\n"); break; } }while(1); system("pause"); return 0; }
After writing the main function, we need to write the main logic game function of the game, directly enter the code, and write the ideas into the comments:
Before writing the main logic function of the game, we first define a few parameters in the macro for the convenience of subsequent application. Here, the game we designed is a 10*10 grid, and then the number of mines in the first level is set to 20:
#define ROW 10//Define the size of ROW as 10 #define COL 10//Define the size of COL as 10 #define MINE_NUM 20//Define the size of mine's MINE_NUM as 20
void game() { int i = 1;//Initialize a variable first to control the level int x,y;//Define two variables to receive the coordinates entered by the player int a = 0;//Initialize the variable a to count the number of squares excluded by the player do {//We first need to define two two-dimensional arrays, one for the system to mine and the other to display to the player char mine[ROW+2][COL+2];//Define the array of system mine char show[ROW+2][COL+2];//Define the array displayed to the user memset(mine,'0',(ROW+2)*(COL+2));//Set the entire content of the system mine array to the character '0' memset(show,'*',(ROW+2)*(COL+2));//Set the entire content of the display array to the character '*' printf("Level %d\n",i);//Display the current level lay_mines(mine,ROW+2,COL+2,i);//lay do{ print_board(show,ROW+2,COL+2);//Display the coordinate interface to the user printf("Please input your coordinate:<x,y>");//Prompt the player to enter the coordinates scanf("%d%d",&x,&y);//Receive the coordinates entered by the player if(x>=1&&x<=10&&y>=1&&y<=10)//Determine whether the player's input coordinates are legal { if(mine[x][y] == '1')//Determine whether the coordinate position selected by the player is a mine. Here the character '1' stands for thunder { printf("It's too bad!Game over!\n");//Prompt the player that the game is over print_board(mine,ROW+2,COL+2);//Display the position of mine goto END;//Jump to the END position } else//Indicates that the location selected by the player is not a mine, we have to calculate the number of mines around the location { int count = get_mine_num(mine,ROW+2,COL+2,x,y);//Use the get_mine_num() function to pass in the number of mines around the location show[x][y] = count + '0';//The number of mines in this position (add the character '0' here, the purpose is to convert the number into the corresponding character) to the corresponding position of the display array a++;//Exclude the number of self-increment if(ROW*COL-i*MINE_NUM == a)//If the number of exclusions is equal to the number of non-mines in the system mine array, it means the player wins { printf("Congratulations!You win!\n"); i++;//The number of levels increases automatically, so that you can enter the next level later break; } } } }while(1); }while(i<=4);//The highest level set by the game is 4. END: printf("Do you want to play again?\n"); }
After the main logic of the game is written, let's write the functions used in the main logic function. In the order from top to bottom, we will first write the functions of the system:
void lay_mines(char mine[][COL+2],int row,int col,int a) { int x;//Define the variable x, which is used to record the abscissa of the system mine position int y;//Define the variable y, which is used to record the ordinate of the system mine position int count = 0;//Define the variable count to control the number of mines srand((unsigned long)time(NULL));//Function to generate random numbers do{ x = (rand() % (ROW))+1;//The generated random number is the remainder of 10 plus one, to control the value range of x must be 1~10 y = (rand() % (COL))+1;//Same as above if(mine[x][y] == '0')//If the coordinate position corresponding to the random number is the character '0', it means there is no mine here { mine[x][y] = '1';//Write the character '1' to this position count++;//The number of mined mines is incremented } }while(count < (a*MINE_NUM));//If the number of mines already mined is less than the number of levels *MINE, continue to loop until a*MINE_NUM mines are filled }
The function that displays the coordinates: The function that displays the coordinates is based on personal preference. Here is the coordinates displayed by this function:
void print_board(char board[][COL+2],int row,int col) { int i = 1; int j = 0; printf(" ");//First output 3 spaces for(;i<=COL;i++) printf("%3d",i);//Output the column coordinates, output the corresponding number every 3 positions printf("\n");//换行 printf(" ");//output 3 spaces for(i=0;i<ROW;i++) printf("---");//The horizontal line below the output column coordinates printf("\n");//换行 for(i = 1;i<=ROW;i++) { printf("%2d|",i);//Output column coordinates for(j = 1;j<=COL;j++) { printf("%2c|",board[i][j]);//Output the corresponding coordinate content } printf("\n");换行 } }
The next step is to count the number of mines around the corresponding position: this function is very easy, nothing more than adding the contents of the eight positions around the corresponding position of the system mine array, and then returning the result.
int get_mine_num(char mine[][COL+2],int row,int col,int x,int y) { int num = 0; num = (mine[x-1][y-1] - '0') + (mine[x-1][y] - '0') + (mine[x-1][y+1] - '0') + (mine[x][y-1] - '0') + (mine[x][y+1] - '0') + (mine[x+1][y-1] - '0') + (mine[x+1][y] - '0') + (mine[x+1][y+1] - '0'); return num; }
So far, all the work is done!
Below is the entire code of the editor:
Header part:
#ifndef _GAME_H_ #define _GAME_H_ #include<stdio.h> #include<stdlib.h> #include<windows.h> #include<time.h> #pragma warning(disable:4996) #define ROW 10 #define COL 10 #define MINE_NUM 20 void game(); #endif
Source file main.c:
#include "game.h" void menu() { printf("********************\n"); printf("********Minesweeper********\n"); printf("** 1.paly 0.exit **\n"); printf("********************\n"); printf("Please select:"); } intmain() { int select = 0; do{ menu(); scanf("%d",&select); switch(select) { case 1: game(); break; case 0: exit(0); default: printf("Error!please try again!\n"); break; } }while(1); system("pause"); return 0; }
Source file game.c:
#include "game.h" void print_board(char board[][COL+2],int row,int col) { int i = 1; int j = 0; printf(" "); for(;i<=COL;i++) printf("%3d",i); printf("\n"); printf(" "); for(i=0;i<ROW;i++) printf("---"); printf("\n"); for(i = 1;i<=ROW;i++) { printf("%2d|",i); for(j = 1;j<=COL;j++) { printf("%2c|",board[i][j]); } printf("\n"); } } void lay_mines(char mine[][COL+2],int row,int col,int a) { int x; int y; int count = 0; srand((unsigned long)time(NULL)); do{ x = (rand() % (ROW))+1; y = (rand() % (COL))+1; if(mine[x][y] == '0') { mine[x][y] = '1'; count++; } }while(count < (a*MINE_NUM)); } int get_mine_num(char mine[][COL+2],int row,int col,int x,int y) { int num = 0; num = (mine[x-1][y-1] - '0') + (mine[x-1][y] - '0') + (mine[x-1][y+1] - '0') + (mine[x][y-1] - '0') + (mine[x][y+1] - '0') + (mine[x+1][y-1] - '0') + (mine[x+1][y] - '0') + (mine[x+1][y+1] - '0'); return num; } void game() { int i = 1; int x,y; int win = 0; do { char mine[ROW+2][COL+2]; char show[ROW+2][COL+2]; memset(mine,'0',(ROW+2)*(COL+2)); memset(show,'*',(ROW+2)*(COL+2)); printf("Level %d\n",i); lay_mines(mine,ROW+2,COL+2,i); do{ system("cls"); print_board(show,ROW+2,COL+2); printf("Please input your coordinate:<x,y>"); scanf("%d%d",&x,&y); if(x>=1&&x<=10&&y>=1&&y<=10) { if(mine[x][y] == '1') { printf("It's too bad!Game over!\n"); print_board(mine,ROW+2,COL+2); goto END; } else { int count = get_mine_num(mine,ROW+2,COL+2,x,y); show[x][y] = count + '0'; win++; if(ROW*COL-i*MINE_NUM == win) { printf("Congratulations!You win!\n"); i++; break; } } } }while(1); }while(i<=4); END: printf("Do you want to play again?\n"); }
Here is the result of running the program: