扫雷

game.h //定义game.c中的函数
# define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.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 ScMinc(char board[ROWS][COLS], int row, int col, int num);
int FindMind(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col,int num);
char FistStep(char mine[ROWS][COLS], int row, int col, int x, int y);

game.c //实现扫雷游戏所需要的功能
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) //初始化棋盘
{

memset(board, set, rows*cols*sizeof(board[0][0]));

}
void DisplayBoard(char board[ROWS][COLS], int row, int col) //展示棋盘
{

int i = 0;
int j = 0;

printf("   ");
for (i = 0; i < row; i++)
{
	printf("  %d ", i + 1);
}
printf("\n");

for (j = 0; j < col + 1; j++)
{
	printf("---|");
}
printf("\n");

for (i = 0; i < row; i++)
{
	printf(" %d |", i + 1);
	for (j = 0; j < col; j++)
	{
		printf(" %c |", board[i][j]);
	}
	printf("\n");
	printf("   |", i + 1);

	for (j = 0; j < col; j++)
	{
		printf("---|");
	}
	printf("\n");
}

printf("\n");

}
void ScMinc(char board[ROWS][COLS], int row, int col,int num) //安置雷
{

int x = 0;
int y = 0;
;

while (num)
{
	x = rand() % 9 + 1;
	y = rand() % 9 + 1;

	if (board[x][y] == '0')
	{
		board[x][y] = '1';
		num--;
	}
}

}
static int GetMineCount(char mine[ROWS][COLS], int x, int y)
{

return mine[x - 1][y] +
	mine[x - 1][y - 1] +
	mine[x][y - 1] +
	mine[x + 1][y - 1] +
	mine[x + 1][y] +
	mine[x + 1][y + 1] +
	mine[x][y + 1] +
	mine[x - 1][y + 1] - 8 * '0';

}

//用递归排除周围没有雷的区域
static void NoMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{

int ret = GetMineCount(mine, x, y);
if (ret == 0)
{
	show[x][y] = ' ';

	if ((x - 1)>0 && (y - 1)>0 && (show[x - 1][y - 1] == '*'))
		NoMine(mine, show, x - 1, y - 1);

	if ((x - 1)>0 && (y)>0 && (show[x - 1][y] == '*'))
		NoMine(mine, show, x - 1, y);

	if ((x - 1)>0 && (y + 1)>0 && (show[x - 1][y + 1] == '*'))
		NoMine(mine, show, x - 1, y + 1);

	if ((x)>0 && (y - 1)>0 && (show[x][y - 1] == '*'))
		NoMine(mine, show, x, y - 1);

	if ((x)>0 && (y + 1)>0 && (show[x][y + 1] == '*'))
		NoMine(mine, show, x, y + 1);

	if ((x + 1)>0 && (y - 1)>0 && (show[x + 1][y - 1] == '*'))
		NoMine(mine, show, x + 1, y - 1);

	if ((x + 1)>0 && (y)>0 && (show[x + 1][y] == '*'))
		NoMine(mine, show, x + 1, y);

	if ((x + 1)>0 && (y + 1)>0 && (show[x + 1][y + 1] == '*'))
		NoMine(mine, show, x + 1, y + 1);
}
else
	show[x][y] = ret + '0';

}
int FindMind(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col,int num)
{ //排雷并判断是否排雷成功

int x = 0;
int y = 0;
int win = 0;  
int ret = 0;  

while (win < ROW*COL - num)
{
	int select = 0;
	system("CLS"); 
	DisplayBoard(show, ROW, COL);
	printf("----- 1.扫雷 --- 2.标记雷 -----\n");
	printf("请选择:>");
	scanf("%d", &select);
	if (select == 1)
	{
		printf("请输入要排查的坐标:>");
		scanf("%d %d", &x, &y);

		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x - 1][y - 1] == '0' && show[x - 1][y - 1] == '*')
			{
				NoMine(mine, show, x - 1, y - 1);
				DisplayBoard(show, ROW, COL);
				win = win + 8;
			}

			if (mine[x - 1][y - 1] == '1' && show[x - 1][y - 1] == '*')
			{
				show[x - 1][y - 1] = 'S';
				DisplayBoard(show, ROW, COL);
				printf("很遗憾,你被炸死了\n\n");
				DisplayBoard(mine, row, col);
				return 1;
			}
		}
		else
		{
			printf("坐标非法\n");
		}
	}

	if (select == 2)
	{
		printf("请输入要标记雷的坐标:>");
		scanf("%d %d", &x, &y);

		if (show[x - 1][y - 1] == '*')
		{
			show[x - 1][y - 1] = '@'; 
			DisplayBoard(show, ROW, COL);
		}
		else
		{
			printf("坐标非法\n");
		}
	}
}

if (win = ROW*COL - num)
{
	printf("恭喜你,排雷成功!\n");
	DisplayBoard(mine, row, col);
	return 0;
}

}
test.c //进行函数的调用,主体步骤的建立
#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"
void menu()
{

printf("*******************************\n");
printf("****      1.play          ****\n");
printf("****      0.exit          ****\n");
printf("*******************************\n");

}

void game()
{

int num = 0;
int ret = 0;
printf("请输入雷数\n");
scanf("%d", &num);
char mine[ROWS][COLS] = { 0 }; //置放雷的棋盘
char show[ROWS][COLS] = { 0 }; //展示给玩家的棋盘
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
ScMinc(mine, ROW, COL,num);        //置雷
DisplayBoard(mine, ROW, COL);
FindMind(mine, show, ROW, COL,num);//排雷
if (ret == 1)
{
	DisplayBoard(show, ROW, COL);
}

}

void test()
{

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");
	}
} while (input);

}

int main()
{
test();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值