C language to realize simple minesweeping program

Time:2020-2-20

Minesweeping is a popular puzzle game. The goal of the game is to find out all the non thunder squares according to the numbers that appear in the click lattice in the shortest time, and avoid stepping on the thunder at the same time. Stepping on one thunder means losing all the game.

First of all, let’s take a brief look at the game mechanism of mine clearance:

If you click any grid, there will be one. The number above represents that there will be several mines in eight grids around the number. There will be only one mine around the number 1, two mines around the number 2, and so on. Until all the grids without thunder are found, the player will win. As long as you click to a grid with thunder, you will be judged to lose.

Once you understand the mechanism, write the program. The program is divided into three parts: test. C, game. C and game. H. The main function is written in test. C, the function of game is implemented in game. C, the header file is stored in game. H, and the function declaration is made.

In the test. C module, first define a menu to store the start menu of a game

int menu()
{
 printf("********************\n");
 printf("********************\n");
 Printf ("* * * * 1, start game **** \ n");
 Printf ("* * * * 0, quit the game **** \ n");
 printf("********************\n");
 printf("********************\n");

}

Write game function to store the game implementation module


void game()
{
}

Write the main function to call game. Menu is implemented through do – while loop, and game start and end are selected by switch statement

int main()
{
 int input = 0;
 do
 {
 menu();
 Printf ("please select: \ n");
 scanf("%d", &input);
 switch (input)
 {
 case 1:
 Printf ("game start: \ n");
 game();
 break;
 case 0:
 Printf ("game exit \ n");
 break;
 default:
 Printf ("please enter the correct number: \ n");
 break;
 }

 } while (input);
 return 0;
}

Now enter the code implementation of game module

First of all, we need to write a function to realize the mine sweeping interface
Here we use the initboard function to implement the 9×9 minesweeping interface. Use * to represent the lattice that is not opened, 1 to represent ray, and 0 to represent empty lattice.


void InitBoard(char board[][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;
 }
 }
}

Calling function in game function in test.c

// storing mines 
 char mine[ROWS][COLS] = { 0 };
 // investigation of thunder 
 char mineInfo[ROWS][COLS] = { 0 };
 InitBoard(mine, ROWS, COLS, '0');
 InitBoard(mineInfo, ROWS, COLS, '*');

Write showboard function in game. C module to print the minesweeping interface written before

void ShowBoard(char board[][COLS],int row,int col)
{
 int i = 0;
 int j = 0;
 printf("===================================\n");
 for (i = 0;i <= row;i++)
 {
 Printf (""% d ", I); // print abscissa
 }
 printf("\n");
 for (i = 1;i <=col;i++)
 {
 Printf (""% d ", I); // print ordinate
 for (j = 1;j <= col;j++)
 {
 printf("%c ", board[i][j]);
 }
 printf("\n");
 }
 printf("===================================\n");

}

At this time, the operation result is;

Then write the setmine function to set the thunder
Use the rand function to randomly set the coordinates of the mines. The number of mines is mine num. here we set 10 mines.


void SetMine(char mine[][COLS], int row, int col)
{
 int x = 0;
 int y = 0;
 int count = 0;
 while (count < MINE_NUM)
 {
 x = rand() % row + 1;
 y = rand() % col + 1;
 if (mine[x][y] == '0')
 {
 mine[x][y] = '1';
 count++;
 }
 }
}

Function is used to calculate the number of mines around the current lattice. Note that since the mines we defined are of char type, we need to convert them to int type when calculating quantity. Data to be added – 8 * ‘0’.


static int GetMineCount(char mine[][COLS], int x, int y)
{
 return mine[x - 1][y - 1] + 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] - 8 * '0';
}

Finally, we use findmine function to realize the operation of player minesweeping

void FindMine(char mine[][COLS], char mineInfo[][COLS], int row, int col)
{
 int count = 0;
 int x = 0;
 int y = 0;
 while (count < row*col - MINE_NUM)
 {
 Printf ("please enter coordinates:");
 scanf("%d%d", &x, &y);
 if (x >= 1 && y <= 9 && y >= 1 && x <= 9)
 {
 if (mine[x][y] == '1')
 {
 Printf ("you've been killed by the explosion \ n");
 ShowBoard(mine, row, col);
 break;
 }
 else
 {
 int ret = GetMineCount(mine, x, y);
 mineInfo[x][y] = ret + '0';
 ShowBoard(mineInfo, row, col);
 count++;
 }
 }
 else {
 Printf ("the entered coordinates are illegal \ n");
 }
 }
}

Remember to call the function of the game function in game.c in the game function of test.c.


void game()
{
 char mine[ROWS][COLS] = { 0 };
 char mineInfo[ROWS][COLS] = { 0 };
 InitBoard(mine, ROWS, COLS, '0');
 InitBoard(mineInfo, ROWS, COLS, '*');
 ShowBoard(mineInfo, ROW, COL);
 SetMine(mine, ROW, COL);
 FindMine(mine, mineInfo, ROW, COL);
}

Preparation of header file:


#ifndef _GAME_H_
#define _GAME_H_
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MINE_NUM 10

#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

void InitBoard(char board[][COLS], int rows, int cols, char set);
void ShowBoard(char board[][COLS], int row, int col);
void SetMine(char mine[][COLS], int row, int col);
void FindMine(char mine[][COLS], char mineInfo[][COLS], int row, int col);

#endif // !_GAME_H

So far, a simple minesweeping program has been written

Operation result:

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.

Recommended Today

Api: tiktok: user video list

Tiktok tiktok, tiktok, tiktok, Api, Api, jitter, voice, bullet screen comments, jitter, jitter, and jitter. Tiktok tiktok tiktok data, jitter data acquisition, live broadcast of shaking sound Titodata: professional short video data acquisition and processing platform. For more information, please contact:TiToData Massive data collection Collect 500 million pieces of data for customers every day Tiktok […]