C实现扫雷

game.h

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>

#define ROW 9
#define COL 9

#define _ROW 11
#define _COL 11


//设置雷区
void MineSet(char Mine[_ROW][_COL], int row, int col);
//打印
void DisPlay(char arr[_ROW][_COL], int row, int col);
//初始化
void BoardInit(char arr[_ROW][_COL], int row, int col, char change);
//排查雷
void FindMine(char Mine[_ROW][_COL],char Player[_ROW][_COL], int row, int col);
//利用递归进行展开
void Recursion(char Mine[_ROW][_COL], char Player[_ROW][_COL], int row, int col);
//周围雷数
int Mine_Num(char Mine[_ROW][_COL], int row, int col);
//胜利条件
int Win(char Player[_ROW][_COL], int row, int col);
//第一次安全
void safe(char Mine[_ROW][_COL], int row, int col);

game.c

#include"game.h"

// 初始化          
void BoardInit(char arr[_ROW][_COL], int row, int col, char change)
{
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			arr[i][j] = change;
		}
	}
}
//设置雷区      
void MineSet(char Mine[_ROW][_COL], int row, int col)
{
	int Mine_num = 10;
	srand((unsigned)time(NULL));
	while (Mine)
	{
		int Board_x = rand() % row + 1;
		int Board_y = rand() % col + 1;
		if (Mine[Board_x][Board_y] == '0')
		{
			Mine[Board_x][Board_y] = '1';
			--Mine_num;
		}		
	}
}
//打印               
void DisPlay(char arr[_ROW][_COL], int row, int col)
{
	for (int i = 0; i <= col; ++i)
	{
		if (i == 0)
			printf("  ");
		else
		printf("%d ", i);
	}
	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");
	}
}
//胜利条件
int Win(char Player[_ROW][_COL], int row, int col)
{
	int count = 0;
	for (int i = 0; i, row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			if (Player[i][i] = '*')
				++count;
		}
	}
	return count;
}
//排查雷        
void FindMine(char Mine[_ROW][_COL], char Player[_ROW][_COL], int row, int col)
{
	int Board_x = 0, Board_y = 0;
	while (1)
	{
		printf("\n");
		printf("请输入你要展开的位置坐标:\n");
		scanf("%d%d", &Board_x, &Board_y);
		if (Board_x > 0 && Board_x <= row && Board_y > 0 && Board_y <= col)
		{
			if (Mine[Board_x][Board_y] == '1')
			{
				printf("很遗憾,你踩雷了!\n");
				DisPlay(Mine, ROW, COL);
				printf("\n");
				break;
			}
			else
			{            
				Recursion(Mine,Player,Board_x,Board_y);
				DisPlay(Player, ROW, COL);
				printf("\n");
				int safe_num = Win(Player, _ROW, _COL);
				if (safe_num == 10)     //当剩余的 * 为雷的个数时玩家胜利
				{
					printf("恭喜你,扫雷成功\n");
					break;
				}					
			}
		}
		else
			printf("无效坐标,请重新输入!\n");
	}
}
//第一次安全
//void safe(char Mine[_ROW][_COL], int row, int col)  
//{
//	Mine[x][y] = '0';
//	x = rand() % ROW + 1;
//	y = rand() % COL + 1;
//	Mine[x][y] = '1';
//}
//周围雷数    //Mine
int Mine_Num(char Mine[_ROW][_COL], int row, int col)
{
	return Mine[row - 1][col] + Mine[row - 1][col + 1] + Mine[row][col + 1] + Mine[row + 1][col + 1]
		+ Mine[row + 1][col] + Mine[row + 1][col - 1] + Mine[row][col - 1] + Mine[row - 1][col - 1] - 8 * 48; //因为是char,所以要减去8*48(48是字符0的ASCII码)值转换成int
}

//利用递归进行展开 
void Recursion(char Mine[_ROW][_COL], char Player[_ROW][_COL], int row, int col)
{
	int ret = 0;
	ret = Mine_Num(Mine,row, col);

	if ((ret == 0 && Player[row][col] == '*'))
	{
		Player[row][col] = ' ';
		if ((row - 1) > 0 && col > 0 && (Player[row - 1][col] == '*')) //上方
			Recursion(Mine, Player, row - 1, col);
		if ((row - 1) > 0 && (col + 1) <= COL && (Player[row - 1][col + 1] == '*'))//右上方
			Recursion(Mine, Player, row - 1, col);
		if ((row) > 0 && (col + 1) <= COL && (Player[row][col + 1] == '*'))//右方
			Recursion(Mine, Player, row - 1, col);
		if ((row + 1) <= ROW && (col + 1) <= COL && (Player[row + 1][col + 1] == '*'))//右下方
			Recursion(Mine, Player, row - 1, col);
		if ((row + 1) <= ROW && col > 0 && (Player[row + 1][col] == '*'))//下方
			Recursion(Mine, Player, row - 1, col);
		if ((row + 1) <= ROW && (col - 1) > 0 && (Player[row + 1][col - 1] == '*'))//左下方
			Recursion(Mine, Player, row - 1, col);
		if ((row) > 0 && (col - 1) > 0 && (Player[row][col - 1] == '*'))//左方
			Recursion(Mine, Player, row - 1, col);
		if ((row - 1) > 0 && (col - 1) > 0 && (Player[row - 1][col - 1] == '*'))//左上方
			Recursion(Mine, Player, row - 1, col);
	}
	else
		Player[row][col] = Mine_Num(Mine, row, col) + '0';
}

main.c

#include"game.h"

void menu()
{
	printf("********************************************\n");
	printf("***********1.         Play          ********\n");
	printf("***********0.         Exit          ********\n");
	printf("********************************************\n");
}
//测试
void game()
{
	char Mine[_ROW][_COL] = { 0 };    //雷区界面 (0 -- 安全   1 -- 雷)
	char Player[_ROW][_COL] = { 0 };  //玩家界面
	BoardInit(Mine, _ROW, _COL, '0');
	BoardInit(Player, _ROW, _COL, '*');
	MineSet(Mine, ROW, COL);
	DisPlay(Player, ROW, COL);
	FindMine(Mine, Player, ROW, COL);
}

int main()
{
	int input = 0;
	do
	{
		menu();
		printf("请输入你的选择:  \n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:printf("游戏开始!\n");
			game();
			break;
		case 0:printf("游戏结束!\n");
			break;
		default:printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}
发布了27 篇原创文章 · 获赞 10 · 访问量 700
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览