Simple minesweeping game based on C language

As a small game with a wide audience, Minesweeper is simple and easy to use, so how to use multiple files to realize a small minesweeper game project?

Ideas

According to the nature of multiple files, we should divide this project into three files, the header file, the library file of the main function and the library file of the calling function.
The main programming thinking is to call functions. The functions to be implemented in this project should include:

  1. The player is not killed when he chooses coordinates for the first time
  2. Initialize the board (random mine)
  3. Show the total number of Jiugong Gneri
  4. Show chess board
  5. Menu selection interface

1. First we need a chessboard

We need to use a two-dimensional array to print two chessboards. If we want to print a 10X10 chessboard, do we have to have 10X10 two-dimensional array elements? , No, because we need to count the number of mines in 8 azimuths around the coordinates when designing the algorithm. If we want to count the number of mines around the boundary coordinates, then there will be the problem of array out-of-bounds, then we need to have more than 10X10 boundaries In the last circle of elements, we must also define the 12X12 array elements. Of course, we don’t print these elements when displaying the chessboard. As shown in the
figure:
Insert picture description here
we print the designer’s chessboard using the array mine[ROW][COL], and print the ones we want to show to the player. Array board[ROW][COL], the two arrays must be initialized at the beginning. In the design board, the character 0 means it is not a thunder, and the character * means the unopened position of the board. Initialize the two arrays. The
code is as follows:
header file contain:

#define ROW 12 //设计者棋盘横坐标
#define COL 12 //设计者棋盘纵坐标
#define TOTAL 10*10 //玩家棋盘的总数
#define MINE_NUM 20 //雷的个数

The Game function contains:

 char mine[ROW][COL];   
 char board[ROW][COL]; 
 memset(mine, '0', sizeof(mine));//初始化数组
 memset(board, '*', sizeof(board));

2. Bray, we use random function generation.

int GetRandIndex(int start, int end)//保证随机数在1-10
{
    
    
return rand() % (end - start + 1) + start;
}

void SetMine(char mine[][COL], int row, int col)
{
    
    
srand((unsigned long)time(NULL));
int count = 0;//count计算已经布雷的总数
while (count < MINE_NUM) {
    
    
int x = GetRandIndex(1, 10);
int y = GetRandIndex(1, 10);
if (mine[x][y] == '0') {
    
    
mine[x][y] = '1';
count++;
}
}}

3. Print Thunder Array

void ShowBoard(char board[][COL], int row, int col)
{
    
    
printf(" ");
int i = 1;
for (; i <= 10; i++) {
    
    
printf(" %d ", i);
}
printf("\n----");
for (i = 1; i <= 10; i++) {
    
    
printf("---");
}
printf("\n");
for (i = 1; i <= 10; i++) {
    
    
printf("%3d|", i);int j = 1;
for (; j <= 10; j++) {
    
    
printf("%2c|", board[i][j]);
}
printf("\n");
int k = 1;
for (k = 1; k <= 11; k++) {
    
    
printf("---");
}
printf("\n");
}
}

4. According to the rules of the game, the total number of mines to be displayed in the center of the Jiugongge where it has been deployed.

char GetMines(char mine[][COL], int row, int col)//char->'0'-'8'
{
    
    
return mine[row - 1][col - 1] + mine[row - 1][col] + \mine[row - 1][col + 1] + mine[row][col - 1] + mine[row][col + 1] + \
mine[row + 1][col - 1] + mine[row + 1][col] + \
mine[row + 1][col + 1] - 7 * '0';
}

5. Total game function

void Game()
{
    
    
 char mine[ROW][COL];   //set/judge mine
 char board[ROW][COL]; //show
 memset(mine, '0', sizeof(mine));
 memset(board, '*', sizeof(board));
 SetMine(mine, ROW, COL);
 int count = TOTAL;
 int x = 0;
 int y = 0;
 int times = 0;
 while (1) {
    
    
  ShowBoard(board, ROW, COL);
  printf("请选择坐标: ");
  scanf("%d,%d", &x, &y);
  times++;
  if (x >= 1 && x <= ROW - 2 && y >= 1 && y <= COL - 2) {
    
    
   if (times == 1) {
    
    
    mine[x][y] = '0';
   }
   if (mine[x][y] == '0') {
    
    
    if (mine[x - 1][y - 1] == '0')
     board[x - 1][y - 1] = GetMines(mine, x - 1, y - 1);
    if(mine[x - 1][y] == '0')
     board[x - 1][y] = GetMines(mine, x - 1, y);
    if(mine[x - 1][y + 1] == '0')
     board[x - 1][y + 1] = GetMines(mine, x - 1, y + 1);
    if(mine[x][y - 1] == '0')
     board[x][y - 1] = GetMines(mine, x, y - 1);
    if(mine[x][y + 1] == '0')
     board[x][y + 1] = GetMines(mine, x, y + 1);
    if(mine[x + 1][y - 1] == '0')
     board[x + 1][y - 1] = GetMines(mine, x + 1, y - 1);
    if( mine[x + 1][y] == '0') 
     board[x + 1][y] = GetMines(mine, x + 1, y);
    if(mine[x + 1][y + 1] == '0' )
     board[x + 1][y + 1] = GetMines(mine, x + 1, y + 1);
    char num = GetMines(mine, x, y);
    board[x][y] = num;
    count--;
    if (count <= 20) {
    
    
     printf("你赢了,厉害!\n");
     break;
    }
   }
   else {
    
    
    printf("不好意思,你被炸死了!\n");
    ShowBoard(mine, ROW, COL);
    break;
   }
  }
  else {
    
    
   printf("你输入的坐标有误,请重新输入!\n");
  }
 }
}

Among them, in
order to open an empty place as we used to play, open the empty place next to it. Need to make a judgment to achieve

if (mine[x - 1][y - 1] == '0')
     board[x - 1][y - 1] = GetMines(mine, x - 1, y - 1);
    if(mine[x - 1][y] == '0')
     board[x - 1][y] = GetMines(mine, x - 1, y);
    if(mine[x - 1][y + 1] == '0')
     board[x - 1][y + 1] = GetMines(mine, x - 1, y + 1);
    if(mine[x][y - 1] == '0')
     board[x][y - 1] = GetMines(mine, x, y - 1);
    if(mine[x][y + 1] == '0')
     board[x][y + 1] = GetMines(mine, x, y + 1);
    if(mine[x + 1][y - 1] == '0')
     board[x + 1][y - 1] = GetMines(mine, x + 1, y - 1);
    if( mine[x + 1][y] == '0') 
     board[x + 1][y] = GetMines(mine, x + 1, y);
    if(mine[x + 1][y + 1] == '0' )
     board[x + 1][y + 1] = GetMines(mine, x + 1, y + 1);

In addition, in order to ensure that customers enjoy the game, they cannot be killed for the first time, so:

 times++;//计算第几次输入坐标
  if (x >= 1 && x <= ROW - 2 && y >= 1 && y <= COL - 2) {
    
    
   if (times == 1) {
    
    
    mine[x][y] = '0';
   }

6. Menu selection

For the sake of brevity of the code and clear division, a .c file can be created for the menu part

void Menu()
{
    
    
 printf("###################################\n");
 printf("## 1. Play               2. Exit ##\n");
 printf("###################################\n");
 printf("Please Select: ");
}
int main()
{
    
    
 int quit = 0;
 int select = 0;
 while (!quit){
    
    
  Menu();
  scanf("%d", &select);
  switch (select)
  {
    
    
  case 1:
   Game();
   break;
  case 2:
   printf("拜拜!\n");
   quit = 1;
   break;
  default:
   printf("选择错误,请重新选择!\n");
   break;
  }
 }
 system("pause");
 return 0;
}

Finally, I provide the overall code:
https://github.com/GagaAutom/C-programming-language/tree/master/mine%20clearance

Reference source:
C language to achieve mine clearance-detailed explanation
https://blog.csdn.net/yc1515707718/article/details/78744978

Guess you like

Origin blog.csdn.net/qq_40893595/article/details/102999586