C++

C language to achieve minesweeper game


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

Main function: main.c

#include "game.h"
void Menu()
{
  printf("##########################\n");
  printf("##1.play 0.exit##########\n");
  printf("##########################\n");
  printf("## Please Enter select! ##\n");
}

int main()
{
  Menu();
  srand((unsigned int)time(NULL));
  int quit = 0;
  while (!quit)
  {
   int select = 0;
   printf(" Please enter your choice: \n");
   scanf("%d", &select);
   switch (select)
   {
   case 1:
   game();
   break;
   case 2:
   quit = 1;
   break;
   default :
   printf(" You typed it incorrectly, please re-type: \n");
   break;
   }
  }
  printf("Bye Bye!\n");
  system("pause");
  return 0;
}

Subfunction: game.c

#include "game.h"
void game()
{
 char mine[ROWS][COLS] = { 0 };
 char show[ROWS][COLS] = { 0 };
 memset(mine, '0', sizeof(mine));// Initialize array to 0
 memset(show, '*', sizeof(show));// Initialize array to *
 int no_y, no_x;
 set_mine(mine,ROWS,COLS,&no_x,&no_y);// Mr.   ' 1' Said ray
 int x = 0;
 int y = 0;
 int time = 100 - NUM;
 while (time > 0)
 {
 system("cls");// Clear the screen
 Show(show, ROWS, COLS);// print   The board
 printf(" Please enter coordinates: \n");
 scanf("%d%d", &x, &y);
 if (x<1 || x>10 || y<1 || y>10)
 {
 printf(" You typed it incorrectly, please re-type: \n");
 continue;
 }
 if (show[x][y] != '*')
 {
 printf(" You typed it incorrectly, please re-type: \n");
 continue;
 }
 if (mine[x][y] == '1')
 {
 if (time == 80)// If the first 1 Once in a while, use it 1 No thunder in exchange for this
 {
 mine[x][y] = '0';
 mine[no_y][no_y] = '1';
 }
 else
 {
 printf("game over!\n");
 Show(mine, ROWS, COLS);
 break;
 }
 }
 show[x][y] = get_mine_count(mine, x, y) + '0';
 Expand(mine, show, x, y);
 time--;
 }

}
void set_mine(char mine[ROWS][COLS],int col,int row,int *no_x,int *no_y)// Declare mine function
{
 int count = NUM;// Set a counter to count the number of mines
 while (count > 0)
 {
 int x = rand() % (col-2) + 1;
 int y = rand() % (col-2) + 1;
 if ((mine[x][y]) == '0')
 {
 mine[x][y] = '1';
 count--;
 }
 }
 for (int i = 1; i <= 10; i++)
 {
 for (int j = 1; i <= 10; j++)
 {
 if (mine[i][j] == '0')
 {
 no_x = i;
 no_y = j;
 return;
 }
 }
 }
}
void Show(char mine[ROWS][COLS], int row, int col)// Declare the print checkerboard function
{
 int i = 0;
 int j = 0;
 printf(" ");
 for (i = 1; i <= 10 ; i++)
 {
 printf("%2d |", i);
 }
 printf("\n");
 for (i = 1; i <= 11; i++)
 {
 printf("----");
 }
 printf("\n");
 for (i = 1; i <= 10 ; i++)
 {
 printf("%2d |", i);
 for (j = 1; j <= 10; j++)
 {
 printf("%2c |", mine[i][j]);
 }
 printf("\n");
 for (int i = 1; i <= 11; i++)
 {
 printf("----");
 }
 printf("\n");
 }
}
int get_mine_count(char mine[ROWS][COLS], int x, int y)// Ray number statistics
{
 return 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]+ mine[x - 1][y] - 8 * '0';
}
void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
 if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
 {
 if (get_mine_count(mine, x, y) + '0' == '0') // said x . y No ray around
 {
 show[x][y] = '0';
 if (show[x - 1][y - 1] == '*')
 {
 Expand(mine, show, x - 1, y - 1);
 }
 if (show[x - 1][y] == '*')
 {
 Expand(mine, show, x - 1, y );
 }
 if (show[x - 1][y + 1] == '*')
 {
 Expand(mine, show, x - 1, y + 1);
 }
 if (show[x ][y - 1] == '*')
 {
 Expand(mine, show, x , y - 1);
 }
 if (show[x][y + 1] == '*')
 {
 Expand(mine, show, x , y + 1);
 }
 if (show[x + 1][y - 1] == '*')
 {
 Expand(mine, show, x + 1, y - 1);
 }
 if (show[x + 1][y] == '*')
 {
 Expand(mine, show, x + 1, y );
 }
 if (show[x + 1][y + 1] == '*')
 {
 Expand(mine, show, x + 1, y + 1);
 }
 }
 }
}

Function declaration: game.h

#ifndef _GAME_H_
#define _GAME_H_
#include<stdio.h>
#include<Windows.h>
#pragma warning(disable:4996)
#include<time.h>
#include<string.h>

#define ROWS 12
#define COLS 12

#define NUM 20 // Ray number


void game();
void set_mine(char mine[ROWS][COLS],int row, int col, int *no_x, int *no_y);
void Show(char mine[ROWS][COLS], int row, int col);
int get_mine_count(char mine[ROWS][COLS], int x, int y);
void Expand(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);

#endif