Implementation of C language minesweeper

  • 2020-05-17 06:01:42
  • OfStack

Implementation of C language minesweeping program

This game can be divided into simple, common and difficult according to the difference of the range of minefields displayed after selecting coordinates

Game procedures, the realization of the game timing (minutes) function, the realization of the most basic functions of minesweeper game.

Defined a 9*9 board, players only according to the prompt to choose the game, and difficulty;

According to the prompt input legal coordinates, if the input is not law will be prompted;

The end of mine clearance will prompt; If a mine is struck, the game will automatically end.

The main functions in the whole game design are:

1. Initialize the mine board and display the board

2. Checkerboard printing function

3. Implementation of mine clearance function

4. Main functions of the game

You can change the size of the checkerboard, the number of mines, and the displayed minefield area in the macro definition.

Don't talk nonsense directly on the code :(not optimized, please forgive me more)


<pre name="code" class="objc">#ifndef __MINE_H__ 
#define __MINE_H__ 
 
#define LINES 11     //  Board of the line  
#define ROWS 11     //  The columns of the board  
#define mine_MAX 10    //  The number of the ray  
#define EAXY 3      //  Simply display the scope  
#define COMMON 2     //  Normal display range  
#define DIFFICULT 1     //  Difficulty displays the range  
 
enum op 
{ 
 EXIT, 
 PLAY 
}; 
void game(char mine[LINES][ROWS],char text[LINES][ROWS],int lines,int rows);  // The game basically implements the function  
void init_mine(char mine[LINES][ROWS],char text[LINES][ROWS],int lines,int rows);//  Initializes the checkerboard function  
void play_EAXY_game(char mine[LINES][ROWS],char text[LINES][ROWS]);    //  Simple game function  
void play_COMMON_game(char mine[LINES][ROWS],char text[LINES][ROWS]) ;   // General game function  
void play_DIFFICULT_game(char mine[LINES][ROWS],char text[LINES][ROWS]) ;  // Difficult game function  
void mine_EAXY_sweep(char mine[LINES][ROWS],char text[LINES][ROWS]);    // Simple minesweeping implementation  
void mine_COMMON_sweep(char mine[LINES][ROWS],char text[LINES][ROWS]);   // Common mine clearance implementation  
void mine_DIFFICULT_sweep(char mine[LINES][ROWS],char text[LINES][ROWS]) ;  // Difficult de-mining implementation  
void print(char tab[LINES][ROWS]); // Printed board  
 
#endif 


#include<stdio.h> 
#include"mine.h" 
#include<stdlib.h> 
#include<time.h> 
void init_mine(char mine[LINES][ROWS],char text[LINES][ROWS],int lines,int rows)  // Initializes the display board and the lightning disk  
{ 
 int i; 
 int j; 
 int a; 
 int b; 
 int count=0; 
 for(i=1; i<LINES-1;i++)   // Initializes the display checkerboard  
 { 
  for(j=1; j<ROWS-1;j++) 
  { 
   text[i][j]='*'; 
  } 
 } 
 for(i=1; i<LINES-1;i++)   // Initializes the lightning disk  
 { 
  for(j=1; j<ROWS-1;j++) 
  { 
   mine[i][j]='1'; 
  } 
 } 
 srand((unsigned)time(NULL)); // I'm going to randomly generate two Numbers  
 while(count<mine_MAX) 
 { 
  a = rand()%9 + 1; 
  b = rand()%9 + 1; 
  if(mine[a][b]!='0') 
  { 
   mine[a][b]='0';    // Define ray as a character  0 
   count++; 
  } 
 } 
} 
void print(char tab[LINES][ROWS])      // Print checkerboard function  
{ 
 int i;  // line  
 int j;  // column  
 
 for(i=0;i<LINES-1;i++)  // Define the first 1 Lines of print  
 { 
  printf("%d ",i); 
 } 
 printf("\n"); 
 for(i=1; i<LINES-1;i++)  // Output board  
 { 
  printf("%d",i); 
  printf("%c",'|'); 
  for(j=1; j<ROWS;j++) 
  { 
   printf("%c ",tab[i][j]); 
  } 
  printf("\n"); 
 } 
 
} 
void mine_EAXY_sweep(char mine[LINES][ROWS],char text[LINES][ROWS])    // Simple entire game progression  
{ 
 int a; 
 int b; 
 int count = 0;  //  The number of ray  
 do     // Whether to sweep all the mines  
 { 
  int i; 
  int j; 
  int x; 
  int y; 
  print(text); 
  print(mine); 
flag:  printf(" Please enter coordinates = " "); 
  scanf("%d %d",&a,&b); 
  if(a>(LINES-2) || a<0 || b<0 || b>(ROWS-2) || text[a][b]!='*') 
   { 
    printf(" Illegal input! \n"); 
    goto flag; 
  } 
  else 
 if(mine[a][b]=='0')   // Determine if there is a mine  
 { 
  printf(" You stepped on thunder! \n"); 
  break; 
 } 
 else 
 { 
  for(i=(a-EAXY);i<=(a+EAXY);i++) 
  { 
    for(j=(b-EAXY);j<=(b+EAXY);j++) 
    { 
     if(mine[i][j]=='1') 
     { 
      int x=0;         // The number of surrounding mines  
      if(mine[i-1][j-1]=='0') 
      { 
       x++; 
      } 
       if(mine[i-1][j]=='0') 
      { 
       x++; 
      }  
       if(mine[i-1][j+1]=='0') 
      { 
       x++; 
      }  
       if(mine[i][j-1]=='0') 
      { 
       x++; 
      }  
       if(mine[i][j+1]=='0') 
      { 
       x++; 
      }  
       if(mine[i+1][j-1]=='0') 
      { 
       x++; 
      }  
       if(mine[i+1][j]=='0') 
      { 
       x++; 
      }  
       if(mine[i+1][j+1]=='0') 
      { 
       x++; 
      }  
       text[i][j]=(x+'0'); 
     } 
 
    } 
  } 
 } 
  for(x=1;x<=(LINES-2);x++) 
  { 
   for(y=1;y<=(ROWS-2);y++) 
   { 
    if(text[x][y]=='*') 
     count++; 
   } 
  } 
  
 }while(count>mine_MAX); 
 if(count==mine_MAX) 
 { 
  printf(" Congratulations on your   Mine clearance successful! \n"); 
 } 
} 
void mine_COMMON_sweep(char mine[LINES][ROWS],char text[LINES][ROWS])    // Normal entire game progression  
{ 
 int a; 
 int b; 
 int count; 
 do     // Whether to sweep all the mines  
 { 
  int i; 
  int j; 
  int x; 
  int y; 
   count = 0;  //  The number of ray  
  print(text); 
  print(mine); 
flag:  printf(" Please enter coordinates = " "); 
  scanf("%d %d",&a,&b); 
  if(a>(LINES-2) || a<0 || b<0 || b>(ROWS-2) || text[a][b]!='*') 
   { 
    printf(" Illegal input! \n"); 
    goto flag; 
  } 
  else 
 if(mine[a][b]=='0')   // Determine if there is a mine  
 { 
  printf(" You stepped on thunder! \n"); 
  break; 
 } 
 else 
 { 
  for(i=(a-COMMON);i<=(a+COMMON);i++) 
  { 
    for(j=(b-COMMON);j<=(b+COMMON);j++) 
    { 
     if(mine[i][j]=='1') 
     { 
      int x=0;         // The number of surrounding mines  
      if(mine[i-1][j-1]=='0') 
      { 
       x++; 
      } 
       if(mine[i-1][j]=='0') 
      { 
       x++; 
      }  
       if(mine[i-1][j+1]=='0') 
      { 
       x++; 
      }  
       if(mine[i][j-1]=='0') 
      { 
       x++; 
      }  
       if(mine[i][j+1]=='0') 
      { 
       x++; 
      }  
       if(mine[i+1][j-1]=='0') 
      { 
       x++; 
      }  
       if(mine[i+1][j]=='0') 
      { 
       x++; 
      }  
       if(mine[i+1][j+1]=='0') 
      { 
       x++; 
      }  
       text[i][j]=(x+'0'); 
     } 
 
    } 
  } 
  print(text); 
 } 
  for(x=1;x<=(LINES-2);x++) 
  { 
   for(y=1;y<=(ROWS-2);y++) 
   { 
    if(text[x][y]=='*') 
     count++; 
   } 
  } 
  
 }while(count>mine_MAX); 
 if(count==mine_MAX) 
 { 
  printf(" Congratulations on your   Mine clearance successful! \n"); 
 } 
} 
void mine_DIFFICULT_sweep(char mine[LINES][ROWS],char text[LINES][ROWS])    // Difficulty throughout game progression  
{ 
 int a; 
 int b; 
 int count ;  //  The number of ray  
 do     // Whether to sweep all the mines  
 { 
  int i; 
  int j; 
  int x; 
  int y; 
  count = 0; 
  print(text); 
  print(mine); 
flag:  printf(" Please enter coordinates = " "); 
  scanf("%d %d",&a,&b); 
  if(a>(LINES-2) || a<0 || b<0 || b>(ROWS-2) || text[a][b]!='*') 
   { 
    printf(" Illegal input! \n"); 
    goto flag; 
  } 
  else 
 if(mine[a][b]=='0')   // Determine if there is a mine  
 { 
  printf(" You stepped on thunder! \n"); 
  break; 
 } 
 else 
 { 
  for(i=(a-DIFFICULT);i<=(a+DIFFICULT);i++) 
  { 
    for(j=(b-DIFFICULT);j<=(b+DIFFICULT);j++) 
    { 
     if(mine[i][j]=='1') 
     { 
      int x=0;         // The number of surrounding mines  
      if(mine[i-1][j-1]=='0') 
      { 
       x++; 
      } 
       if(mine[i-1][j]=='0') 
      { 
       x++; 
      }  
       if(mine[i-1][j+1]=='0') 
      { 
       x++; 
      }  
       if(mine[i][j-1]=='0') 
      { 
       x++; 
      }  
       if(mine[i][j+1]=='0') 
      { 
       x++; 
      }  
       if(mine[i+1][j-1]=='0') 
      { 
       x++; 
      }  
       if(mine[i+1][j]=='0') 
      { 
       x++; 
      }  
       if(mine[i+1][j+1]=='0') 
      { 
       x++; 
      }  
       text[i][j]=(x+'0'); 
     } 
 
    } 
  } 
  print(text); 
 } 
  for(x=1;x<=(LINES-2);x++) 
  { 
   for(y=1;y<=(ROWS-2);y++) 
   { 
    if(text[x][y]=='*') 
     count++; 
   } 
  } 
  
 }while(count>mine_MAX); 
 if(count==mine_MAX) 
 { 
  printf(" Congratulations on your   Mine clearance successful! \n"); 
 } 
} 









#include<stdio.h> 
#include"mine.h" 
#include<time.h> 
void emun() 
{ 
 printf("***********************\n"); 
 printf("******* 1.play ******\n"); 
 printf("******** 0.exit ******\n"); 
 printf("***********************\n"); 
} 
void emun_dift()         // Select difficulty menu  
{ 
 printf("*********************\n"); 
 printf("****** 1. simple  *******\n"); 
 printf("****** 2. ordinary  *******\n"); 
 printf("****** 3. difficult  *******\n"); 
 printf("*********************\n"); 
} 
void play_EAXY_game(char mine[LINES][ROWS],char text[LINES][ROWS]) // Play simple games  
{ 
  time_t t_start,t_end; 
 printf(" Time to start!! \n"); 
 t_start = time(NULL);  // Start the time  
 mine_EAXY_sweep(mine,text); 
 t_end = time(NULL);  // End of the timing  
 printf(" Your time is: %.0f \n",difftime(t_end,t_start)); 
} 
void play_COMMON_game(char mine[LINES][ROWS],char text[LINES][ROWS]) // Play normal games  
{ 
  time_t t_start,t_end; 
 printf(" Time to start!! \n"); 
 t_start = time(NULL);  // Start the time  
 mine_COMMON_sweep(mine,text); 
 t_end = time(NULL);  // End of the timing  
 printf(" Your time is: %.0f \n",difftime(t_end,t_start)); 
} 
void play_DIFFICULT_game(char mine[LINES][ROWS],char text[LINES][ROWS]) // Play difficult games  
{ 
  time_t t_start,t_end; 
 printf(" Time to start!! \n"); 
 t_start = time(NULL);  // Start the time  
 mine_DIFFICULT_sweep(mine,text); 
 t_end = time(NULL);  // End of the timing  
 printf(" Your time is: %.0f \n",difftime(t_end,t_start)); 
} 
void game() 
{ 
 int input = 0; 
 int flout = 0; 
 char mine[LINES][ROWS]={0};   // Define LeiPan  
 char text[LINES][ROWS]={0};   // Define display panel  
 
 init_mine(mine,text,LINES,ROWS);      // Initializes the lightning disk  
 
 do 
 { 
  emun(); 
  init_mine(mine,text,LINES,ROWS);      // Initializes the lightning disk  
  printf(" Please select a = " "); 
   scanf("%d",&input); 
  switch(input) 
  { 
  case PLAY: 
   { 
    emun_dift(); 
   printf(" Please select a = " "); 
   scanf("%d",&flout); 
   switch(flout) 
   { 
   case 1: 
    play_EAXY_game(mine,text); 
    break; 
   case 2: 
    play_COMMON_game(mine,text); 
    break; 
   case 3: 
    play_DIFFICULT_game(mine,text); 
    break; 
   } 
   } 
   break; 
  case EXIT: 
   break; 
  } 
 }while(input); 
 
} 
int main() 
{ 
 game(); 
 return 0; 
} 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: