扫雷
所需函数
void Menu();
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);
int Getmine(char mine[ROWS][COLS], int x, int y);
static int Getmine(char mine[ROWS][COLS], int x, int y);
void spread(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y);
函数主体
定义两个二维数组:
mine[ROWS][COLS],show[ROWS][COLS]
mine用于布置雷
show用于与玩家交互
注意:
为防止溢出,我们将9X9的操作盘增加为11X11
#define _CRT_SECURE_NO_WARNINGS 1
#include "header.h"
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
DisplayBoard(show, ROW, COL);
SetMine(mine, ROW, COL);
DisplayBoard(mine, ROW, COL);
FindMine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
Menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
case 0:
break;
default:
printf("输入无效,请重新输入:>");
}
} while (input);
}
初始化棋盘
此函数较为简单 由于有两个棋盘mine和show,传递set形参用于填充棋盘的内容
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for ( i &#61; 0; i < rows; i&#43;&#43;)
{
int j &#61; 0;
for (j &#61; 0; j < cols; j&#43;&#43;)
{
board[i][j] &#61; set;
}
}
}
打印棋盘
这里&#xff0c;我们为棋盘填上行列标号
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("---------扫雷----------\\n");
int i &#61; 0;
for (i &#61; 0; i <&#61;col; i&#43;&#43;)
{
printf("%d-", i);
}
printf("\\n");
for ( i &#61; 1; i <&#61; row; i&#43;&#43;)
{
int j &#61; 0;
printf("%d|", i);
for ( j &#61; 1; j <&#61; col; j&#43;&#43;)
{
printf("%c ", board[i][j]);
}
printf("\\n");
}
printf("---------扫雷----------\\n");
}
布置雷
用到rand函数
rand() % row &#43; 1 表示从1到9随机
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count &#61; EASY_COUNT;
while (count)
{
int x &#61; rand() % row &#43; 1;
int y &#61; rand() % col &#43; 1;
if (mine[x][y] &#61;&#61; &#39;0&#39;)
{
mine[x][y] &#61; &#39;1&#39;;
count--;
}
}
}
统计雷&#xff08;后面排雷函数会用到&#xff09;
注释内容为直接加四周的值返回
这里我选择for循环方法
static int Getmine(char mine[ROWS][COLS], int x, int y)
{
int count &#61; 0;
for (int i &#61; -1; i <&#61; 1; i&#43;&#43;)
{
for (int j &#61; -1; j <&#61; 1; j&#43;&#43;)
{
if (mine[x &#43; i][y &#43; j] &#61;&#61; &#39;1&#39;)
{
count&#43;&#43;;
}
}
}
return count;
}
&#xff01;&#xff01;递归&#xff01;&#xff01;&#xff1a;判断展开
本人在这个上面费了很多功夫&#xff0c;
结果发现是判断后没有标记mine数组&#xff0c;
导致无限递归栈溢出
不同于网上大部分代码
我选择for循环递归
void spread(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
char count;
for (int i &#61; -1; i <&#61; 1; i&#43;&#43;)
{
for (int j &#61; -1; j <&#61; 1; j&#43;&#43;)
{
count &#61; Getmine(mine, x &#43; i, y &#43; j) &#43; &#39;0&#39;;
if (mine[x &#43; i][y &#43; j] &#61;&#61; &#39;0&#39;)
{
if (count &#61;&#61; &#39;0&#39; && x&#43;i>0 && x&#43;i<ROWS-1 && y&#43;j>0 && y&#43;j < COLS-1)
{
mine[x &#43; i][y &#43; j] &#61; &#39;&#64;&#39;;
show[x &#43; i][y &#43; j] &#61; &#39; &#39;;
spread(mine, show, x &#43; i, y &#43; j);
}
else
{
show[x &#43; i][y &#43; j] &#61; count;
}
}
}
}
}
排查雷&#xff08;会用到统计雷和递归展开&#xff09;
&#xff08;包含标记功能&#xff09;
标记功能和排查功能放在switch语句中
标记符号为‘&#xff01;’
以‘*’和‘&#xff01;’总数是否等于雷的个数来判断 “是否继续” 或 “胜利”
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int count_for_star;
while (1)
{
int x, y;
int choice;
while(1)
{
printf("1:标记雷 2:排查雷&#xff0c;请选择(错误输入会重新输入):>");
scanf("%d", &choice);
switch (choice)
{
case 1:
do {
printf("标记雷&#xff0c;输入坐标(错误输入会重新输入):>");
scanf("%d %d", &x, &y);
} while (x > 9 || y > 9);
show[x][y] &#61; &#39;!&#39;;
DisplayBoard(show, row, col);
break;
case 2:
do{
printf("排查雷&#xff0c;输入坐标(错误输入会重新输入):>");
scanf("%d %d", &x, &y);
}while (x > 9 || y > 9);
if (mine[x][y] &#61;&#61; &#39;1&#39;)
{
printf("此处为雷&#xff0c;失败\\n");
goto flag;
}
else
{
spread(mine, show, x, y);
DisplayBoard(show, ROW, COL);
count_for_star &#61; 0;
for (int i &#61; 1; i <&#61; row; i&#43;&#43;)
{
for (int j &#61; 1; j <&#61; col; j&#43;&#43;)
{
if (show[i][j] &#61;&#61; &#39;*&#39;|| show[i][j] &#61;&#61; &#39;!&#39;)
{
count_for_star&#43;&#43;;
}
}
}
if (count_for_star &#61;&#61; EASY_COUNT)
{
goto flag;
}
break;
}
}
}
}
flag:printf("\\n");
if(count_for_star &#61;&#61; EASY_COUNT)
{
printf("!!!!!!胜利!!!!!!\\n");
}
}