目录
1.gane.h文件中函数的申明
2.game.c文件游戏函数的实现
3.test.c文件游戏的测试(主函数)
4.初始化雷场
5.打印雷场
6.布雷
7.扫雷
8.得到周围雷的数量
1.gane.h文件中函数的申明
#define _CRT_SECURE_NO_WARNINGS 1
#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);
//布雷
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 get_mine_count(char mine[ROWS][COLS],int x,int y);
2.game.c文件游戏函数的实现
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
//打印
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("------扫雷------\n");
int i = 0;
int j = 0;
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
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");
}
//布雷
#define easy_count 20
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = easy_count;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] != '1')
{
mine[x][y] = '1';
count--;
}
}
}
//排雷
//雷数
int get_mine_count(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 show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int ret = row * col - easy_count;
while (ret)
{
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
printf("输入排雷坐标:->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("你被炸死了!\n");
break;
}
else
{
if (show[x][y] == '*')
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
ret--;
}
else
{
printf("此坐标已经被排查了!\n");
}
}
}
else
{
printf("坐标非法!\n");
}
}
if (ret == 0)
{
printf("恭喜排雷成功!!!\n");
DisplayBoard(show, ROW, COL);
}
else
{
DisplayBoard(mine, ROW, COL);
}
}
3.test.c文件游戏的测试(主函数)
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("***************************\n");
printf("****** 1.play 0.exit ******\n");
printf("***************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
//初始化
InitBoard(mine, ROWS, COLS,'0');
InitBoard(show, ROWS, COLS,'*');
//打印
/*DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);*/
//布雷
SetMine(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();
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("选择有误!\n");
break;
}
} while (input);
}
4.初始化雷场
//初始化(test.c)
InitBoard(mine, ROWS, COLS,'0');
InitBoard(show, ROWS, COLS,'*');
//初始化(game.h)
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//初始化(game.c)
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
5.打印雷场
//打印(test.c)
DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//打印(game.h)
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//打印(game.c)
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
printf("------扫雷------\n");
int i = 0;
int j = 0;
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
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");
}
6.布雷
//布雷(test.c)
SetMine(mine, ROW, COL);
//布雷(game.h)
void SetMine(char mine[ROWS][COLS], int row, int col);
//布雷(game.c)
#define easy_count 20
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = easy_count;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] != '1')
{
mine[x][y] = '1';
count--;
}
}
}
7.扫雷
//排雷(test.c)
FindMine(mine, show, ROW, COL);
//排雷(game.h)
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//(game.c)
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int ret = row * col - easy_count;
while (ret)
{
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
printf("输入排雷坐标:->");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("你被炸死了!\n");
break;
}
else
{
if (show[x][y] == '*')
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
ret--;
}
else
{
printf("此坐标已经被排查了!\n");
}
}
}
else
{
printf("坐标非法!\n");
}
}
if (ret == 0)
{
printf("恭喜排雷成功!!!\n");
DisplayBoard(show, ROW, COL);
}
else
{
DisplayBoard(mine, ROW, COL);
}
}
8.得到周围雷的数量
//雷数
int get_mine_count(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';
}
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/94511.html