C language implements simple minesweeper games
- 2020-06-23 01:40:39
- OfStack
This article is an example of C language to share the implementation of simple minesweeper game code, for your reference, the specific content is as follows
steps
1. First print the game selection menu
2. Map and initialization
1) Map shown to players (show_map)
2) Map of the mine (mine_map knows the location of the mine)
Print the map
4. User input coordinates and check the rationality of coordinates
5. Determine whether there are mines in the current position, and if there are no mines, a number will appear (indicating how many mines are in the surrounding grid).
Print a new map
7. Repeat steps 3-6 until the thunder game is over or you win.
Resume the game or quit!
code
Main function and game selection menu
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define COUNT 10 // Number of mines
int menu()
{
printf("*************************\n");
printf("****** 1.play ******\n");
printf("****** 0.exit ******\n");
printf("*************************\n");
printf(" Please select a :>");
int choice = 0;
scanf("%d", &choice);
return choice;
}
int main()
{
int choice = 0;
srand((unsigned int)time(NULL));
while (1)
{
choice = menu();
if (choice == 1)
{
game();
}
else if (choice == 0)
{
printf("goodbye!\n");
break;
}
else
{
printf(" Input error, please re-input! \n");
}
}
system("pause");
return 0;
}
~ Enter the game
1. Create two 2-dimensional arrays; 2. Initialize two 2-dimensional arrays; 3. Print the map; 4. Let the user input 1 set of coordinates; 5. Determine whether to step on a mine; 6. Judge whether the game is won or not; 7. Update the map to change the current location to a number (the number is the number of mines in the surrounding grid).
void game()
{
//1. Create two 2 Dimensional array
char show_map[ROW][COL];
char mine_map[ROW][COL];
//2. The two 2 The dimension array is initialized
Init(show_map, mine_map);
int blank_count = 0;
while (1)
{
PrintMap(mine_map);// To facilitate debugging, print temporarily mine_map
printf("\n");
//3. Print the map
PrintMap(show_map);
//4. Let the user enter 1 Set of coordinates
printf(" Please enter coordinates :>");
int i = 0;
int j = 0;
scanf("%d %d", &i, &j);
if (i < 0 || i >= ROW || j < 0 || j >= COL)
{
printf(" Input error, please re-input! \n");
continue;
}
if (show_map[i][j] != '*')
{
// This position has been flipped
printf(" The input position has been turned over, please re-enter! \n");
continue;
}
//5. Decide whether to step on a mine
if (mine_map[i][j] == '1')
{
// Step on thunder, prompt game is over
PrintMap(mine_map);
printf(" Game over !\n");
break;
}
blank_count++;
//6. Whether the game wins
if (blank_count == ROW * COL - COUNT)
{
printf(" Congratulations, mine clearance is a success !\n");
break;
}
//7. Update the map to change the current location to 1 A digital
UpdateShowMa(show_map, mine_map, i, j);
}
}
Initialization map
void Init(char show_map[ROW][COL], char mine_map[ROW][COL])
{
//1.show_map All initialized to *
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
show_map[i][j] = '*';
}
}
//2.mine_map Initialize the
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
mine_map[i][j] = '0';
}
}
//3. Randomly generated 10 Three locations as mines
int mine_count = COUNT;
while (mine_count > 0)
{
// Generate random coordinates
int i = rand() % ROW;
int j = rand() % COL;
if (mine_map[i][j] == '1')
{
continue;
}
mine_map[i][j] = '1';
--mine_count;
}
}
Print the map
void PrintMap(char map[ROW][COL])
{
//1. Print the first 1 Row (coordinate)
printf(" |");
for (int j = 0; j < COL; j++)
{
printf(" %d", j);
}
printf("\n");
//2. print 1 Line separators
for (int j = 0; j < COL; j++)
{
printf("---");
}
printf("\n");
//3. Print each line separately
for (int i = 0; i < ROW; i++)
{
printf(" %d|", i);
for (int j = 0; j < COL; j++)
{
printf(" %c", map[i][j]);
}
printf("\n");
}
}
Determine the number of grid mines around the drop
void UpdateShowMa(char show_map[ROW][COL], char mine_map[ROW][COL], int i, int j)
{
// Determine the current position ( i . j ) , around 8 There are several mines in this grid
int count = 0;
if (i - 1 >= 0 && j - 1 >= 0 && mine_map[i - 1][j - 1] == '1')
{
count++;
}
if (i - 1 >= 0 && mine_map[i - 1][j] == '1')
{
count++;
}
if (i - 1 >= 0 && j + 1 < COL && mine_map[i - 1][j + 1] == '1')
{
count++;
}
if (j - 1 >= 0 && mine_map[i][j - 1] == '1')
{
count++;
}
if (j + 1 < COL && mine_map[i][j + 1] == '1')
{
count++;
}
if (i + 1 < ROW && j - 1 >= 0 && mine_map[i + 1][j - 1] == '1')
{
count++;
}
if (i + 1 < ROW && mine_map[i + 1][j] == '1')
{
count++;
}
if (i + 1 < ROW && j + 1 < COL && mine_map[i + 1][j + 1] == '1')
{
count++;
}
//count The value inside is already the number of surrounding mines
show_map[i][j] = count + '0';
}