扫雷小游戏

扫雷小游戏,第1张

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define MaxMine 2
#define ROW 11
#define COL 11
using namespace std;
const int N = 15;
int mine[N][N];
char show[N][N];
void init()
{
	int x, y;
	int count = MaxMine;
	srand((unsigned)time(NULL));
	while (count)
	{
		x = rand() % (ROW - 2) + 1;
		y = rand() % (COL - 2) + 1;
		if (mine[x][y] == 0)
		{
			mine[x][y] = -1;
			count--;
		}
	}
	for (int i = 1; i < ROW - 1; i++)
		for (int j = 1; j < COL - 1; j++)
		{
			if (mine[i][j] != -1)
			{
				if (mine[i - 1][j - 1] == -1)
					mine[i][j]++;
				if (mine[i - 1][j] == -1)
					mine[i][j]++;
				if (mine[i - 1][j + 1] == -1)
					mine[i][j]++;
				if (mine[i][j - 1] == -1)
					mine[i][j]++;
				if (mine[i][j + 1] == -1)
					mine[i][j]++;
				if (mine[i + 1][j - 1] == -1)
					mine[i][j]++;
				if (mine[i + 1][j] == -1)
					mine[i][j]++;
				if (mine[i + 1][j + 1] == -1)
					mine[i][j]++;
			}
		}
	for (int i = 1; i < ROW - 1; i++)
		for (int j = 1; j < COL - 1; j++)
			show[i][j] = '*';
}
void color(int m)
{
	HANDLE consolehend;
	consolehend = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(consolehend, m);
}
void display_board()
{
	int i, j;
	printf("  ");
	color(2);
	for (i = 1; i < COL - 1; i++)
		printf(" %d", i);
	printf("\n");
	for (i = 1; i < ROW - 1; i++)
	{
		color(2);
		printf("%d ", i);
		for (j = 1; j < COL - 1; j++)
		{
			color(8);
			if (show[i][j] == '*')
				printf("■");
			else if (show[i][j] == 'o')
				printf("●");
			else if (show[i][j] == '0')
				printf("  ");
			else if (show[i][j] >= '1' && show[i][j] <= '8')
			{
				int q = show[i][j] - '0';
				color(q);
				printf(" %d", q);
			}
		}
		printf("\n");
	}
}
void get_empty(int x, int y)
{
	if (x < 1 || x>9 || y < 1 || y>9)
		return;
	if (show[x][y] != '*')
		return;
	show[x][y] = mine[x][y] + '0';
	if (mine[x][y] != 0)
		return;
	get_empty(x + 1, y);
	get_empty(x - 1, y);
	get_empty(x, y + 1);
	get_empty(x, y - 1);
}
void mine_sweep(int x, int y)
{
	if (mine[x][y] == -1)
		show[x][y] = 'o';
	else if (mine[x][y] >= 1 && mine[x][y] <= 8)
		show[x][y] = mine[x][y] + '0';
	else if (mine[x][y] == 0)
		get_empty(x, y);
} 
int main()
{
	init();
	display_board();
	printf("输入行列信息,空格间隔:");
	int a, b;
	int res = 0;
	while (~scanf("%d%d", &a, &b))
	{
		if (mine[a][b] == -1)
		{
			res = 1;
			mine_sweep(a, b);
			display_board();
			break;
		}
		mine_sweep(a, b);
		display_board();
		int sum = 0;
		for (int i = 1; i < ROW - 1; i++)
			for (int j = 1; j < COL - 1; j++)
				if (show[i][j] == '*')
					sum++;
		if (sum == MaxMine)
		{
			res = 2;
			display_board();
			break;
		}
		color(7);
		printf("输入行列信息,空格间隔:");
	}
	color(7);
	if (res == 1) printf("你输了!");
	else if (res == 2) printf("你赢了!");
	return 0;
}

MaxMine代表雷数,可以任意修改。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/662110.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-18
下一篇 2022-04-18

发表评论

登录后才能评论

评论列表(0条)

保存