Article directory
1. Game rules
1. In the small grid of 9*9, select a coordinate (grid) arbitrarily. After selection, if the coordinate of the mine is not clicked, the number will be displayed (indicating how many mines are around). If the mine is clicked, the game is over
Troubleshooting:
- If this position is not a mine, calculate how many mines there are in the 8 coordinates around this position, and display the number of mines
- If it's mine, it blows up and it's game over
- If you find all the mines, you win and the game is over
- Link: Web version minesweeper
2. After selection, as shown in the figure
3. The case of being hit by a mine
2. Code logic
- game menu design
- Design minefields and arrange mines randomly
- Troubleshooting
3. Game realization
1. Game menu design
//test.c
#include "game.h"
void game()
{
printf("扫雷\n");
}
void menu()
{
printf("************************\n");
printf("****** 扫雷 *******\n");
printf("****** 1. play *******\n");
printf("****** 0. exit *******\n");
printf("************************\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择>:");
scanf("%d",&input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}
2. Design minefields and arrange mines randomly
(1) Setting minefields
Set a 9*9 two-dimensional array, 0 means no thunder, 1 means thunder
However, in order to avoid the number 1 (the number of mines) displayed to the player coincides with the 1 set mine, so choose * to bury mines
Because minesweeping is to sweep the 8 surrounding areas, you will encounter the problem of sweeping out of bounds
Therefore, it becomes a two-dimensional array of 11*11
to set the chessboard
//test.c 中的 game()
void game()
{
char mine[ROWS][COLS] = {
0};//放置雷的数组
char show[ROWS][COLS] = {
0};//显示的数组
InitBoard(mine,ROWS,COLS,'0');
DisplayBoard(mine, ROW, COL);
InitBoard(show,ROWS,COLS,'*');
DisplayBoard(show, ROW, COL);
}
//game.c
#include "game.h"
void InitBoard(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 DisplayBoard(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");
}
//game.h
#include <stdio.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void InitBoard(char board[ROWS][COLS],int rows,int cols,char set);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
(2) Arrange mines
Randomly generate thunder here.
The character 0 means it is not mine. Character 1 means thunder
//game.c
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = Easy_count;
while (count)
{
int x = rand() % row + 1;//生成坐标1-9
int y = rand() % col + 1;
if (mine[x][y] =='0') //避免在同一个地方布置雷
{
mine[x][y] = '1';
count--;
}
}
}
3. Check mine
Enter the coordinates of the mine to be checked, if it is a mine, then GAME OVER!, if it is not a mine, it will display how many mines are around the coordinate
//game.c
//排查雷
int GetCountMine(char mine[ROWS][COLS],int x,int y)
{
return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] +
mine[x][y - 1]+ mine[x][y + 1] +mine[x + 1][y - 1] +
mine[x + 1][y] + mine[x + 1][y + 1]) - (8 * '0');
}
void FindMine(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("请输入要排查的坐标:>");
scanf("%d %d", &x, &y);
if ( x >= 1 && x <= row && y >= 1 && y <= col)
{
//选中雷
if (mine[x][y] =='1')
{
printf("GAME OVER!!!,被炸死了\n");
DisplayBoard(mine,ROW,COL);
break;
}
else
{
//不是雷,统计周围有多少雷
int c = GetCountMine(mine,x,y);
show[x][y] = c + '0';
DisplayBoard(show,ROW,COL);
win++;
}
}
else
{
printf("坐标输入错误,重新输入\n");
}
}
if (win == ((row * col)- Easy_count)) //需要排的区域数
{
printf("恭喜您,排雷成功!\n");
}
}
Surrounding coordinates of mine clearance
x-1, y - 1 | x-1,y | x-1,y+1 |
x, y - 1 | x,y | x,y+1 |
x+1,y-1 | x+1,y | x+1,y+1 |
The situation when the mine is completed (80 mines are set here (for testing the success of mine clearance), so only one mine is safe)
mine, it's game over
4. Source code
game.h
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define Easy_count 10
//初始化
void InitBoard(char board[ROWS][COLS],int rows,int cols,char set);
//显示
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char mine[ROWS][COLS],int row,int col);
//排查雷
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);
test.c
#include "game.h"
void game()
{
char mine[ROWS][COLS] = {
0};//放置雷的数组
char show[ROWS][COLS] = {
0};//显示的数组
InitBoard(mine,ROWS,COLS,'0');
InitBoard(show,ROWS,COLS,'*');
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine,ROW,COL);
//排查雷
FindMine(mine,show,ROW,COL);
}
void menu()
{
printf("************************\n");
printf("****** 扫雷 *******\n");
printf("****** 1. play *******\n");
printf("****** 0. exit *******\n");
printf("************************\n");
}
int main()
{
int input = 0;
srand((unsigned int )time(NULL));//设置随机数种子
do
{
menu();
printf("请选择>:");
scanf("%d",&input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,请重新选择\n");
break;
}
} while (input);
return 0;
}
game.c
#include "game.h"
//初始化棋盘
void InitBoard(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 DisplayBoard(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 SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = Easy_count;
while (count)
{
int x = rand() % row + 1;//生成坐标1-9
int y = rand() % col + 1;
if (mine[x][y] =='0') //避免在同一个地方布置雷
{
mine[x][y] = '1';
count--;
}
}
}
//排查雷
int GetCountMine(char mine[ROWS][COLS],int x,int y)
{
return (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1] +
mine[x][y - 1]+ mine[x][y + 1] +mine[x + 1][y - 1] +
mine[x + 1][y] + mine[x + 1][y + 1]) - (8 * '0');
}
void FindMine(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("请输入要排查的坐标:>");
scanf("%d %d", &x, &y);
if ( x >= 1 && x <= row && y >= 1 && y <= col)
{
//选中雷
if (mine[x][y] =='1')
{
printf("GAME OVER!!!,被炸死了\n");
DisplayBoard(mine,ROW,COL);
break;
}
else
{
//不是雷,统计周围有多少雷
int c = GetCountMine(mine,x,y);
show[x][y] = c + '0';
DisplayBoard(show,ROW,COL);
win++;
}
}
else
{
printf("坐标输入错误,重新输入\n");
}
}
if (win == ((row * col)- Easy_count)) //需要排的区域数
{
printf("恭喜您,排雷成功!\n");
}
}