开始游戏后打印地雷图(本文以9*9的地雷图为例子),让用户选择要扫雷的坐标(x,y)
如果该位置没有雷,则在该位置打印围绕在该坐标的地雷数。如果周围的地雷数为0,则会自动检查(x-1,y-1),(x-1,y),(x-1,y+1),(x,y-1),(x,y+1),(x+1,y-1),(x+1,y),(x+1,y+1)周围的地雷数并以此类推,直至每个坐标周围都有地雷围绕。
如果该位置有雷则输出“您踩到雷了,游戏失败”并将地雷图打印出来(数字1的地方是埋雷的地方,数字0的地方是没有雷的地方)进入下次游戏的选择。
如果最终游戏胜利则输出恭喜您游戏胜利
在本次游戏中我们将会创建两个二维数组
一个用于记录地雷埋下的位置(该数组初始化为0),坐标上为1的表示有雷,为0的表示无雷,如图所示
void Init_board(char board[ROWS][COLS], int row, int col, char a)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = a;
}
}
}
void print_board(char board[ROWS][COLS], int row, int col)
{
printf("*************扫雷*************\n");
for (int i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ",board[i][j]);
}
printf("\n");
}
printf("*************扫雷*************\n");
}
该函数在传入的Myboard二维数组中埋下地雷,也就是使部分字符0用1取代
srand((unsigned int)time(NULL));//与下图的rand()函数一起生成随机数
void Set_mine(char Myboard[ROWS][COLS])
{
int count = Number_mine;
while (count)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (Myboard[x][y] == '0')
{
Myboard[x][y] = '1';
count--;
}
}
}
使用递归的方法查找地雷
int Sround_mine(char Myboard[ROWS][COLS], int x, int y)
{
return (Myboard[x - 1][y - 1] + Myboard[x - 1][y] + Myboard[x - 1][y + 1] + Myboard[x][y - 1]
+ Myboard[x][y + 1] + Myboard[x + 1][y - 1] + Myboard[x + 1][y] + Myboard[x + 1][y + 1] - 8 * '0');
}
void Find_mine(char Myboard[ROWS][COLS], char Showboard[ROWS][COLS], int x, int y,int row,int col)
{
if (Showboard[x][y] != '*')
return ;
int ret = Sround_mine(Myboard, x, y);
if (ret == 0)
{
Showboard[x][y] = ret + '0';
Sum++;
for (int i = x - 1; i <= x + 1; i++)
{
if (i >= 1 && i <= 9)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (j >= 1 && j <= 9)
{
if (i == x && j == y)
continue;
else
{
Find_mine(Myboard, Showboard, i, j, row, col);
}
}
}
}
}
}
else
{
Showboard[x][y] = ret + '0';
Sum++;
}
}
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#define ROW 9//定义该游戏为9*9的布局
#define COL 9
#define ROWS ROW+2//增加一层边界,便于游戏实现
#define COLS COL+2
#define Number_mine 9//埋下地雷的数目
int Sum;//用于记录已经扫出来多少个没有雷的坐标,当Sum==ROW*COL-Number_mine时游戏结束
void Init_board(char board[ROWS][COLS], int row, int col, char a);//初始化两个二维数组
void print_board(char board[ROWS][COLS], int row, int col);//打印两个二维数组
void Set_mine(char Myboard[ROWS][COLS]);//放置雷
int Sround_mine(char Myboard[ROWS][COLS], int x, int y);//判断一个坐标周围的雷
void Find_mine(char Myboard[ROWS][COLS],char Showboard[ROWS][COLS], int x, int y,int row,int col);//查找雷
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("**************************\n");
printf("*********1.play***********\n");
printf("*********0.exit***********\n");
printf("**************************\n");
}
void play_game(char Myboard[ROWS][COLS],char Showboard[ROWS][COLS],int row,int col)
{
while (1)
{
printf("请输入您要选择的坐标:");
int x = 0, y = 0;
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (Myboard[x][y] == '1')
{
printf("您踩到雷了,游戏失败\n");
print_board(Myboard, ROW, COL);
break;
}
if (Showboard[x][y] == '*')
{
Find_mine(Myboard,Showboard, x, y,ROW,COL);
//int ret = Find_mine(Myboard, x, y);
//Showboard[x][y] = ret + '0';
print_board(Showboard, ROW, COL);
//print_board(Myboard, ROW, COL);
if (Sum >= row * col - Number_mine)
{
break;
}
}
else
{
printf("该位置已经检查过,请重新输入\n");
continue;
}
}
else
{
printf("输入不合法,请重新输入\n");
}
}
if (Sum == row * col - Number_mine)
{
printf("恭喜您游戏胜利\n");
printf("地雷放置图如下:\n");
print_board(Myboard, ROW, COL);
}
}
void game()
{
printf("开始游戏\n");
char Myboard[ROWS][COLS] = { 0 };
Init_board(Myboard, ROWS, COLS, '0');
char Showboard[ROWS][COLS] = { 0 };
Init_board(Showboard, ROWS, COLS, '*');
//print_board(Myboard, ROW, COL);
print_board(Showboard, ROW, COL);
Set_mine(Myboard);
print_board(Myboard, ROW, COL);
//进入扫雷
play_game(Myboard,Showboard,ROW,COL);
}
void test()
{
int n = 0;
srand((unsigned int)time(NULL));
do {
menu();
printf("请输入您选择的数字:");
scanf("%d", &n);
switch (n)
{
case 1:
Sum = 0;
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
}while (n);
}
int main()
{
test();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void Init_board(char board[ROWS][COLS], int row, int col, char a)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = a;
}
}
}
void print_board(char board[ROWS][COLS], int row, int col)
{
printf("*************扫雷*************\n");
for (int i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ",board[i][j]);
}
printf("\n");
}
printf("*************扫雷*************\n");
}
void Set_mine(char Myboard[ROWS][COLS])
{
int count = Number_mine;
while (count)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (Myboard[x][y] == '0')
{
Myboard[x][y] = '1';
count--;
}
}
}
int Sround_mine(char Myboard[ROWS][COLS], int x, int y)
{
return (Myboard[x - 1][y - 1] + Myboard[x - 1][y] + Myboard[x - 1][y + 1] + Myboard[x][y - 1]
+ Myboard[x][y + 1] + Myboard[x + 1][y - 1] + Myboard[x + 1][y] + Myboard[x + 1][y + 1] - 8 * '0');
}
void Find_mine(char Myboard[ROWS][COLS], char Showboard[ROWS][COLS], int x, int y,int row,int col)
{
if (Showboard[x][y] != '*')
return ;
int ret = Sround_mine(Myboard, x, y);
if (ret == 0)
{
Showboard[x][y] = ret + '0';
Sum++;
for (int i = x - 1; i <= x + 1; i++)
{
if (i >= 1 && i <= 9)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (j >= 1 && j <= 9)
{
if (i == x && j == y)
continue;
else
{
Find_mine(Myboard, Showboard, i, j, row, col);
}
}
}
}
}
}
else
{
Showboard[x][y] = ret + '0';
Sum++;
}
}