C language code to realize mine sweeping game

Time:2022-1-3

This example shares the specific code of minesweeping game in C language for your reference. The specific contents are as follows

We also use arrays to complete the game. We set two arrays, a mine array and a show array.

The mine array stores mines, and the show array displays the chessboard and whether there are mines in the position or several mines around the position.

The array size is particular. We define the macro variable row col as 9 and define rows cols as 11. We display the 9×9 chessboard and set the thunder in the position of 9×9. However, we set the array as 11×11, because it is convenient for us to traverse the number of thunder around a certain position on the four sides of the 9×9 chessboard, otherwise an out of bounds error will occur.

For the symbol of thunder, we set the empty position as 0 and the position with thunder as 1, so it is easy for us to count the number of thunder around a certain position.

The key point is the minesweeping function. The player inputs the X and y position. We judge whether there are mines in the position. If not, we judge how many mines are around the position and store them in the show array display to the player. And we input a position. When the position is not mine, the counter count + +; If the counter count = = col * row eaxy_ COUNT; We judge the player to win.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define ROW 9
#define COL 9

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

#define EASY_COUNT 10

//Function declaration
void ChushiBoard(char board[ROWS][COLS],int rows,int cols,char set);
void Dayinboard(char board[ROWS][COLS], int row, int col);
void BuzhiBoard(char mine[ROWS][COLS], int row, int col);
void CaoleiBoard(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);

void menu()
{
 printf("*****************************************\n");
 printf("*****************************************\n");
 Printf ("* * * * enter 1 to start the game and enter 0 to exit the game ***** \ n");
 printf("*****************************************\n");
 printf("*****************************************\n\n\n");
}

void game()
{
 char mine[ROWS][COLS];// Storage mine
 char show[ROWS][COLS];// Show the checked mine
 //Initialization
 ChushiBoard(mine, ROWS, COLS,'0');//'0'
 ChushiBoard(show, ROWS, COLS,'*');//'*'
 //Lay thunder
 BuzhiBoard(mine,ROW,COL);
 //Dayinboard(mine, ROW, COL);
 //Print chessboard
 Dayinboard(show, ROW, COL);
 //Minesweeping
 CaoleiBoard(mine,show,ROW,COL);
}



void test()
{
 int input = 0;
 do
 {
 menu();
 Printf ("please enter: >");
 scanf("%d", &input);
 switch (input)
 {
 case 1:
 game();
 break;
 case 0:
 Printf ("quit the game \ n \ n");
 break;
 default:
 Printf ("input error, re-enter \ n \ n");
 break;
 }
 } while (input);
}


int main()
{
 test();
 system("pause");
 return 0;
}


void ChushiBoard(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 Dayinboard(char board[ROWS][COLS], int row, int col)
{
 int i = 0;
 int j = 0;
 printf("------------------------------\n");
 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");
 }
 printf("------------------------------\n");
}


void BuzhiBoard(char mine[ROWS][COLS], int row, int col)
{
 int count = EASY_COUNT;
 while (count)
 {
 int x = rand()%row+1;
 int y = rand()%col+1;
 if (mine[x][y] == '0')
 {
 mine[x][y] = '1';
 count--;
 }
 }
}

static int GetMineCount(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 CaoleiBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
 int x = 0;
 int y = 0;
 int win = 0;
 while (win<row*col-EASY_COUNT)
 {
 Printf ("please enter the coordinates to be checked (Format: X space X enter): >");
 scanf("%d%d", &x, &y);
 if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
 {
 if (mine[x][y] == '1')
 {
 Printf ("unfortunately, you were killed!!! \ n");
 Dayinboard(mine, row, col);
 break;
 }
 else
 {
 int count = GetMineCount(mine,x,y);
 show[x][y] = count+'0';
 Dayinboard(show, row, col);
 win++;
 }

 }
 else
 {
 Printf ("illegal coordinates entered \ n");
 }
 }
 if (win == row*col - EASY_COUNT)
 {
 Printf ("Congratulations, you succeeded in mine clearance!!! \ n");
 }
}

Operation results:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

Vue2 technology finishing 3 – Advanced chapter – update completed

3. Advanced chapter preface Links to basic chapters:https://www.cnblogs.com/xiegongzi/p/15782921.html Link to component development:https://www.cnblogs.com/xiegongzi/p/15823605.html 3.1. Custom events of components 3.1.1. Binding custom events There are two implementation methods here: one is to use v-on with vuecomponent$ Emit implementation [PS: this method is a little similar to passing from child to parent]; The other is to use ref […]