C language to achieve simple minesweeper game

  • 2020-11-25 07:23:39
  • OfStack

This article is an example of C language to share the implementation of minesweeper game specific code, for your reference, the specific content is as follows

Before writing minesweeper, first, we should list the features we want to implement in minesweeper:

1. Display the number of mines around the selected coordinates.
2. Make sure that the coordinates selected at the first time are not mines.
3. If there is no thunder in the 8 grids around the selected coordinates, expand.

Second, we should understand that we need two boards to implement the minesweeper game: 1 board to show the player, the initial interface is all "*" (open grid), this page is our common minesweeper page. The other board is shown to the writer and contains only the characters' 1' and '0'. '1' is for thunder and' 0' is for non-thunder. Using the characters' 1' and '0' to represent mines and non-mines helps us to calculate the number of mines around the coordinates in the next step.

1. Initialize the checkerboard


nitboard(show_board, ROWS, COLS,'*'); // ' *' That's open coordinates 
 Initboard(mine_board, ROWS, COLS, '0'); // ' 0' On behalf of the ray 
 void Initboard(char board[ROWS][COLS], int row, int col, char set)
{
 memset(board, set, row*col * sizeof(board[0][0]));
}

2. Print the checkerboard


void DisPlayboard(char board[ROWS][COLS], int row, int col)
{
 int i = 0;
 int j = 0;
 printf(" ");
 for (i = 1; i <= row; i++) // Print the line 
 {
 printf("%d ", i);
 }
 printf("\n");
 for (i = 1; i <= row; i++)
 {
 printf("%d ", i); // Print columns 
 for (j = 1; j <= col; j++)
 {
 printf("%c ", board[i][j]);
 }
 printf("\n");
 }
 printf("\n");
}

3. Bury mines in the chessboard. (Random number is selected to bury mines since the position of mines cannot be repeated every time you restart the game.)


void GetBoom(char board[ROWS][COLS], int row, int col) // mine 
{
 int x = 0;
 int y = 0;
 int sum = 0;
 while(1)
 { 
 x = rand() % row +1; // Ray on 1-9 In the range of 
 y = rand() % col +1;
 if (1)
 {
 board[x][y] = '1';
 sum++;
 if (sum == M) // Jump out of the loop after being mined 
 {
 break;
 }
 }
 }

4. Make sure that you do not step on the thunder the first time. If you step on the thunder the first time, we will change the position to non-thunder and then move the thunder to a position where no thunder has been placed elsewhere.

(Also consider the special case that if the first drop wins, the function does not need to be executed. Therefore, the function should be changed to a function with a return value, so that when the function is called, it can be determined whether the first drop wins or not.)


int Judge_first(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col)
{
 int x = 0;
 int y = 0;
 int m;
 int n;
 printf(" Please enter the coordinates you want to check :");
 scanf("%d%d", &x, &y);
 while (1)
 {
 if (mine_board[x][y] == '1') // If the first 1 Step on thunder, change this coordinate to non - thunder 
 {
 mine_board[x][y] = '0';
 show_board[x][y] = ' ';
 while (1)
 {
 m = rand() % 9 + 1;
 n = rand() % 9 + 1;
 if (m != x && n != y && mine_board[m][n] != '1')
 {
 mine_board[m][n] = '1';
 }
 Open(mine_board, show_board, x, y);
 if (Full(show_board, ROW, COL) == 1) // Expand and decide whether or not 1 Second best 
 {
 printf(" The game victory \n");
 DisPlayboard(mine_board, ROW, COL);
 return 2;  //1 A victorious return 2  No longer perform PlayerMove function 
 break;
 }
 DisPlayboard(show_board, row, col);
 break;
 }
 break;
 }
 else if (mine_board[x][y] == '0')
 {
 show_board[x][y] = ' ';
 Open(mine_board, show_board, x, y);
 if (Full(show_board, ROW, COL) == 1)
 {
 printf(" The game victory \n");
 DisPlayboard(mine_board, ROW, COL);
 return 2;
 break;
 }
 DisPlayboard(show_board, row, col);
 break;
 }
 }
}

5. When the number of mines around the coordinate is 0, display the surrounding grid, and then expand with the 8 coordinates around X and Y as the center respectively. When there are mines around the coordinate, no recursively expand, and put the number of mines on this coordinate.


void Open(char mine_board[ROWS][COLS],char show_board[ROWS][COLS], int x, int y)
{
 int ret = 0;
 ret = Num(mine_board, x, y);
 if (ret == 0) // When the number of mines around the coordinate is zero 0 when   Open up the surrounding grid   And then to X and Y around 8 Coordinates are expanded at the center   Repetition is a recursive expansion 
 {
 show_board[x][y] = ' '; 
 if (x - 1 > 0 && y - 1 > 0 && show_board[x - 1][y - 1] == '*')
 {
 Open(mine_board, show_board, x - 1, y - 1);
 }
 if (x - 1 > 0 && y > 0 && show_board[x - 1][y] == '*')
 {
 Open(mine_board, show_board, x - 1, y);
 }
 if (x - 1 > 0 && y + 1 <= COL && show_board[x - 1][y + 1] == '*')
 {
 Open(mine_board, show_board, x - 1, y + 1);
 }
 if (x > 0 && y + 1 <= COL && show_board[x][y + 1] == '*')
 {
 Open(mine_board, show_board, x, y + 1);
 }
 if (x + 1 < ROW && y + 1 <= COL && show_board[x + 1][y + 1] == '*')
 {
 Open(mine_board, show_board, x + 1, y + 1);
 }
 if (x + 1 <= ROW && y > 0 && show_board[x + 1][y] == '*')
 {
 Open(mine_board, show_board, x + 1, y);
 }
 if (x + 1 <= ROW && y - 1 > 0 && show_board[x + 1][y - 1] == '*')
 {
 Open(mine_board, show_board, x + 1, y - 1);
 }
 if (x > 0 && y - 1 > 0 && show_board[x][y - 1] == '*')
 {
 Open(mine_board, show_board, x, y - 1);
 }
 }
 else if (ret != 0)
 show_board[x][y] = Num(mine_board, x, y) + 48;
}

6. Calculate the number of mines contained in 1 circle around the coordinates.


int Num(char board[ROWS][COLS], int x, int y)
{
 int sum = 0; //sum Is the number of mines around the coordinates 
 sum = board[x - 1][y - 1]
 + board[x][y - 1] + board[x + 1][y - 1]
 + board[x + 1][y] + board[x + 1][y + 1]
 + board[x][y + 1] + board[x - 1][y + 1]
 + board[x - 1][y] - (8 * '0');
 return sum;
}

7. Determine if the player has cleared all mines.


int Full(char board[ROWS][COLS], int row, int col) 
{
 int i;
 int j;
 int sum = 0; //sum Is the number of unopened squares of the checkerboard 
 for (i = 1; i <= row; i++)
 {
 for (j = 1; j <= col; j++)
 {
 if (board[i][j] == '*')
 sum++;
 } 
 }
 if (sum == M) // When the number of unturned cells is the number of mines   return 1
 return 1;
}

8. Player input coordinates for minesweeper.


void PlayerMove(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col)
{
 int x = 0;
 int y = 0;
 int ret = 0;
 while (1)
 {
 if (Full(show_board, ROW, COL) == 1) // The player wins when the checkerboard fails to open a grid equal to the number of mines 
 {
 printf(" The game victory \n");
 DisPlayboard(mine_board, ROW, COL);
 break;
 }
 printf(" Please enter coordinates :");
 scanf("%d%d", &x, &y);
 if (x >= 1 && x <= row && y >= 1 && y <= col)
 {
 
 if (mine_board[x][y] == '1') // The player steps on thunder and the game is over 
 {
 printf(" Game over \n");
 DisPlayboard(mine_board, ROW, COL);
 break;
 }
 ret = Num(mine_board, x, y);
 if (ret == 0) // When there is no thunder around, expand the surrounding grid 
 {
 Open(mine_board, show_board, x, y);
 DisPlayboard(show_board, ROW, COL);
 }
 else if (ret != 0) // Print the number of mines when there are mines around 
 {
 show_board[x][y] = ret + 48;
 DisPlayboard(show_board, ROW, COL);
 }
 }
 else
 printf(" Input error, please retype ");
 }
}

9. Function declaration, macro definition, function definition implementation, test.

Source: ES43en.c // Responsible for testing the entire code
Header file: ES46en. h // contains various function declarations, macro definitions
Source: ES49en. c // contains definitions for various functions

test.c


#include"game.h"
void menu()
{
 printf("************************************\n");
 printf("************************************\n");
 printf("******* 1.play  *******\n");
 printf("******* 0.exit  *******\n");
 printf("************************************\n");
}

int game()
{
 char show_board[ROWS][COLS] = { 0 };
 char mine_board[ROWS][COLS] = { 0 };
//
 Initboard(show_board, ROWS, COLS,'*'); // Initializing array 
 Initboard(mine_board, ROWS, COLS, '0'); 
 printf(" The game start \n");
 DisPlayboard(show_board, ROW, COL); // Print the player's lightning disk 
//
 GetBoom(mine_board, ROW, COL); // mine 
 printf("\n\n\n");
 DisPlayboard(mine_board, ROW, COL); // Print LeiPan 
//
 if (Judge_first(mine_board, show_board, ROW, COL) == 2) // if 1 Once won, it is no longer carried out PlayerMove function 
 return 2;
// 
 PlayerMove(mine_board, show_board, ROW, COL); 
}

void test()
{
 int n;
 menu();
 do
 {
 printf(" Please enter the option button : ");
 scanf("%d", &n);
 switch (n)
 {
 case 1:
 game();
 break;
 case 0:
 printf(" The game out of \n");
 break;
 default:
 printf(" Input error, please retype \n");
 break;
 } 
 }while(1);
}
int main()
{
 srand((unsigned int)time(NULL));
 test();
}

game.c


#include"game.h"
void Initboard(char board[ROWS][COLS], int row, int col, char set)
{
 memset(board, set, row*col * sizeof(board[0][0]));
}
void DisPlayboard(char board[ROWS][COLS], int row, int col)
{
 int i = 0;
 int j = 0;
 printf(" ");
 for (i = 1; i <= row; i++) // Print the line 
 {
 printf("%d ", i);
 }
 printf("\n");
 for (i = 1; i <= row; i++)
 {
 printf("%d ", i); // Print columns 
 for (j = 1; j <= col; j++)
 {
 printf("%c ", board[i][j]);
 }
 printf("\n");
 }
 printf("\n");
}

void GetBoom(char board[ROWS][COLS], int row, int col) // mine 
{
 int x = 0;
 int y = 0;
 int sum = 0;
 while(1)
 { 
 x = rand() % row +1; // Ray on 1-9 In the range of 
 y = rand() % col +1;
 if (1)
 {
 board[x][y] = '1';
 sum++;
 if (sum == M) // Bury the required mine and jump out of the loop 
 {
 break;
 }
 }
 }
}

int Judge_first(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col)
{
 int x = 0;
 int y = 0;
 int m;
 int n;
 printf(" Please enter the coordinates you want to check :");
 scanf("%d%d", &x, &y);
 while (1)
 {
 if (mine_board[x][y] == '1') // If the first 1 Step on thunder, change this coordinate to non - thunder 
 {
 mine_board[x][y] = '0';
 show_board[x][y] = ' ';
 while (1)
 {
 m = rand() % 9 + 1;
 n = rand() % 9 + 1;
 if (m != x && n != y && mine_board[m][n] != '1')
 {
 mine_board[m][n] = '1';
 }
 Open(mine_board, show_board, x, y);
 if (Full(show_board, ROW, COL) == 1) // Expand and decide whether or not 1 Second best 
 {
 printf(" The game victory \n");
 DisPlayboard(mine_board, ROW, COL);
 return 2;  //1 A victorious return 2  No longer perform PlayerMove function 
 break;
 }
 DisPlayboard(show_board, row, col);
 break;
 }
 break;
 }
 else if (mine_board[x][y] == '0')
 {
 show_board[x][y] = ' ';
 Open(mine_board, show_board, x, y);
 if (Full(show_board, ROW, COL) == 1)
 {
 printf(" The game victory \n");
 DisPlayboard(mine_board, ROW, COL);
 return 2;
 break;
 }
 DisPlayboard(show_board, row, col);
 break;
 }
 }
}

void PlayerMove(char mine_board[ROWS][COLS], char show_board[ROWS][COLS], int row, int col)
{
 int x = 0;
 int y = 0;
 int ret = 0;
 while (1)
 {
 if (Full(show_board, ROW, COL) == 1) // The board is full of players winning 
 {
 printf(" The game victory \n");
 DisPlayboard(mine_board, ROW, COL);
 break;
 }
 printf(" Please enter coordinates :");
 scanf("%d%d", &x, &y);
 if (x >= 1 && x <= row && y >= 1 && y <= col)
 {
 
 if (mine_board[x][y] == '1')
 {
 printf(" Game over \n");
 DisPlayboard(mine_board, ROW, COL);
 break;
 }
 ret = Num(mine_board, x, y);
 if (ret == 0) // When there is no thunder around, expand the surrounding grid 
 {
 Open(mine_board, show_board, x, y);
 DisPlayboard(show_board, ROW, COL);
 }
 else if (ret != 0) // Print the number of mines when there are mines around 
 {
 show_board[x][y] = ret + 48;
 DisPlayboard(show_board, ROW, COL);
 }
 }
 else
 printf(" Input error, please retype ");
 }
}

void Open(char mine_board[ROWS][COLS],char show_board[ROWS][COLS], int x, int y)
{
 int ret = 0;
 ret = Num(mine_board, x, y);
 if (ret == 0) // When the number of mines around the coordinate is zero 0 when   Open up the surrounding grid   And then to X and Y around 8 Coordinates are expanded at the center   Repetition is a recursive expansion 
 {
 show_board[x][y] = ' '; 
 if (x - 1 > 0 && y - 1 > 0 && show_board[x - 1][y - 1] == '*')
 {
 Open(mine_board, show_board, x - 1, y - 1);
 }
 if (x - 1 > 0 && y > 0 && show_board[x - 1][y] == '*')
 {
 Open(mine_board, show_board, x - 1, y);
 }
 if (x - 1 > 0 && y + 1 <= COL && show_board[x - 1][y + 1] == '*')
 {
 Open(mine_board, show_board, x - 1, y + 1);
 }
 if (x > 0 && y + 1 <= COL && show_board[x][y + 1] == '*')
 {
 Open(mine_board, show_board, x, y + 1);
 }
 if (x + 1 < ROW && y + 1 <= COL && show_board[x + 1][y + 1] == '*')
 {
 Open(mine_board, show_board, x + 1, y + 1);
 }
 if (x + 1 <= ROW && y > 0 && show_board[x + 1][y] == '*')
 {
 Open(mine_board, show_board, x + 1, y);
 }
 if (x + 1 <= ROW && y - 1 > 0 && show_board[x + 1][y - 1] == '*')
 {
 Open(mine_board, show_board, x + 1, y - 1);
 }
 if (x > 0 && y - 1 > 0 && show_board[x][y - 1] == '*')
 {
 Open(mine_board, show_board, x, y - 1);
 }
 }
 else if (ret != 0)
 show_board[x][y] = Num(mine_board, x, y) + 48;
}

int Num(char board[ROWS][COLS], int x, int y)
{
 int sum = 0;
 sum = board[x - 1][y - 1]
 + board[x][y - 1] + board[x + 1][y - 1]
 + board[x + 1][y] + board[x + 1][y + 1]
 + board[x][y + 1] + board[x - 1][y + 1]
 + board[x - 1][y] - (8 * '0');
 return sum;
}

int Full(char board[ROWS][COLS], int row, int col)
{
 int i;
 int j;
 int sum = 0;
 for (i = 1; i <= row; i++)
 {
 for (j = 1; j <= col; j++)
 {
 if (board[i][j] == '*')
 sum++;
 } 
 }
 if (sum == M)
 return 1;
}

game.h


void DisPlayboard(char board[ROWS][COLS], int row, int col)
{
 int i = 0;
 int j = 0;
 printf(" ");
 for (i = 1; i <= row; i++) // Print the line 
 {
 printf("%d ", i);
 }
 printf("\n");
 for (i = 1; i <= row; i++)
 {
 printf("%d ", i); // Print columns 
 for (j = 1; j <= col; j++)
 {
 printf("%c ", board[i][j]);
 }
 printf("\n");
 }
 printf("\n");
}
0

More interesting classic games to realize the special topic, to share with you:

C++ classic game summary

python classic game summary

python Tetris game set

JavaScript classic games play non-stop

java classic game summary

javascript classic game summary


Related articles: