目录
项目的创建:
test.c:用于代码的测试;
game.h:用于宏定义,头文件,和函数声明;
game.c:用于函数功能的实现;
test.c:用于代码的测试;
game.h:用于宏定义,头文件,和函数声明;
game.c:用于函数功能的实现;
直接上代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include"mine.h"
void menu()
{
printf("****************************************\n");
printf("*********1.play 0.exit *******\n");
printf("****************************************\n");
printf("****************************************\n");
}
int main()
{
int input = 0;
srand((unsigned)time(NULL));
do
{
menu();
printf("请输入:>");
scanf_s("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出");
break;
default:
printf("输入错误,请重新输入!");
break;
}
} while (input);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
#define DEFAULT_SIZE 10
#include
#include
#include
#include
//游戏函数
void game();
//数组初始化函数
void reset(char arr[ROWS][COLS], int row, int col, char ans);
//打印棋盘
void DisplayArr(char arr[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col);
//排雷
void FindMine(char mine[ROWS][COLS], char clear[ROWS][COLS], int row, int col);
//获得雷的个数
int GetMineCount(char mine[ROWS][COLS], int x, int y);
#define _CRT_SECURE_NO_WARNINGS 1
#include"mine.h"
//获得雷的个数
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x - 1][y] +
mine[x + 1][y] +
mine[x - 1][y + 1] +
mine[x][y + 1] +
mine[x + 1][y + 1] - 8 * '0';
}
//排雷
void FindMine(char mine[ROWS][COLS], char clear[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - DEFAULT_SIZE)
{
printf("请输入要排查雷的坐标:>");
scanf_s("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (clear[x][y] == '*')
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
DisplayArr(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
clear[x][y] = count + '0';
system("cls");
DisplayArr(clear, ROW, COL);
win++;
}
}
else
{
printf("该坐标已经被排查过了\n");
}
}
else
{
printf("坐标非法,请重新输入:>");
}
}
if (win == row * col - DEFAULT_SIZE)
{
printf("恭喜你排雷成功!\n");
DisplayArr(mine, ROW, COL);
}
}
//布置雷
void SetMine(char arr[ROWS][COLS], int row, int col)
{
//布置10个雷
int count = DEFAULT_SIZE;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}
//打印棋盘
void DisplayArr(char arr[ROWS][COLS], int row, int col)
{
for (int j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (int i = 1; i <= row; i++)
{
printf("%d ", i);
for (int j = 1; j <= col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}
//数组初始化函数
void reset(char arr[ROWS][COLS], int rows, int cols, char ans)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
arr[i][j] = ans;
}
}
}
//游戏函数
void game()
{
char mine[ROWS][COLS] = { 0 };
char clear[ROWS][COLS] = { 0 };
//初始化棋盘
reset(mine, ROWS, COLS, '0');
reset(clear, ROWS, COLS, '*');
//布置雷
SetMine(mine, ROW, COL);
//DisplayArr(mine, ROW, COL);
DisplayArr(clear, ROW, COL);
//排雷
FindMine(mine, clear, ROW, COL);
}
如果文章内容帮到你,请点个赞吧