C language simple minesweeper game

  • 2020-06-19 11:21:58
  • OfStack

This article examples for you to share C language minesweeper specific code, for your reference, the specific content is as follows


#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX_ROW 9
#define MAX_COL 9
#define MINE_C0UNT 10
void menu() {
 printf("************************\n");
 printf("*****   1.play  ****\n");
 printf("*****   0.exit  ****\n");
 printf("************************\n");
}
//1 First initialize the two maps, the landmine layout as seen by the player. 
void Init(char show_map[MAX_ROW][MAX_COL],char mine_map[MAX_ROW][MAX_COL]) {
 // For the map the player sees, the unflipped map is set to * ; 
 for (int row = 0; row < MAX_ROW; row++) {
 for (int col = 0; col < MAX_COL; col++) {
  show_map[row][col] = '*';
 }
 }
 // For mine layouts, use 0 There are no landmines 1 Ray said. 
 for (int row = 0; row < MAX_ROW; row++) {
 for (int col = 0; col < MAX_COL; col++) {
  mine_map[row][col] = '0';
 }
 }
 // Assuming that set 10 A landmine 
 int n = MINE_C0UNT;
 while (n > 0) {
 int row = rand() % MAX_ROW;
 int col = rand() % MAX_COL;
 if (mine_map == '1') {
  continue;
 }
 mine_map[row][col] = '1';
 --n;
 }
}
void printmap(char map[MAX_ROW][MAX_COL]) {
 // Not only can you print maps, you can also print coordinates 
// Print the first 1 line 
 printf("  ");
 for (int i = 0; i < MAX_COL; i++) {
 printf("%d ", i);
 }
 printf("\n");
 // print 1 A line 
 for (int col = 0; col < MAX_COL - 2; ++col) {
 printf("---");
 }
 printf("\n");
 // Print the other line 
 for (int row = 0; row < MAX_ROW; row++) {
 printf(" %d| ", row);
 // Print each of the lines 1 column 
 for (int col = 0; col < MAX_COL; col++) {
  printf("%c ", map[row][col]);
 }
 printf("\n");
 }
}
void updateshowmap(int row,int col,char show_map[MAX_ROW][MAX_COL], char mine_map[MAX_ROW][MAX_COL]) {
 int count = 0;
 if (row - 1 >= 0 && col - 1 >= 0 && row - 1 < MAX_ROW && col - 1 < MAX_COL && mine_map[row - 1][col - 1] == '1') {
 count++;
 }
 if (row - 1 >= 0 && col >= 0 && row - 1 < MAX_ROW && col < MAX_COL && mine_map[row - 1][col] == '1') {
 count++;
 }
 if (row - 1 >= 0 && col + 1 >= 0 && row - 1 < MAX_ROW && col + 1 < MAX_COL && mine_map[row - 1][col + 1] == '1') {
 count++;
 }
 if (row >= 0 && col - 1 >= 0 && row < MAX_ROW && col - 1 < MAX_COL && mine_map[row][col - 1] == '1') {
 count++;
 }
 if (row >= 0 && col + 1 >= 0 && row < MAX_ROW && col + 1 < MAX_COL && mine_map[row][col + 1] == '1') {
 count++;
 }
 if (row + 1 >= 0 && col - 1 >= 0 && row + 1 < MAX_ROW && col - 1 < MAX_COL && mine_map[row + 1][col - 1] == '1') {
 count++;
 }
 if (row + 1 >= 0 && col + 1 >= 0 && row + 1 < MAX_ROW && col + 1 < MAX_COL && mine_map[row + 1][col + 1] == '1') {
 count++;
 }
 show_map[row][col] = '0' + count;
}
void game() {
 char show_map[MAX_ROW][MAX_COL];
 char mine_map[MAX_ROW][MAX_COL];
 Init(show_map,mine_map);
 while (1) {
 printmap(show_map);
 printf(" Please enter 1 Set of coordinates: ");
 int row, col;
 int blank_count_already_show = 0;
 scanf("%d%d", &row, &col);
 system("cls");
 if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL) {
  printf(" Your input is illegal, please re-enter! \n");
  continue;
 }
 if (show_map[row][col] != '*') {
  printf(" The position you entered has been occupied, please re-enter! \n");
  continue;
 }
 // Determine if the coordinates entered by the player correspond to a mine. If so, the game is over 
 if (mine_map[row][col] == '1') {
  printf(" Game over! \n");
  printmap(mine_map);
  break;
 }
 // Determine whether the game is won by counting the number of non-mined squares that have been turned over 
 ++blank_count_already_show;
 if (blank_count_already_show == MAX_ROW * MAX_COL - MINE_C0UNT) {
  printf(" The game victory   ! \n");
  printmap(mine_map);
  break;
 }
 // Count the number of surrounding mines in the current position 
 updateshowmap(row, col, show_map, mine_map);
 }
}
int main() {
 srand((unsigned)time(0));
 int input = 0;
 while (1) {
 menu();
 printf(" Please select: ");
 scanf("%d", &input);
 if (input == 1) {
  printf(" Start the game! \n");
  game();
 }
 else if (input == 0) {
  printf(" Quit the game! \n");
  break;
 }
 else {
  printf(" Input error, please re-input! \n");
  continue;
 }
 }
 system("pause");
 return 0;
}

Related articles: