跳到正文

剪贴板

剪贴板 fczax215

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
#include <windows.h>
#define ll long long
using namespace std;

const int s = 16,boom = 40;
char a[s + 2][s + 2],b[s + 2][s + 2];

struct mines
{
	int h,l;
	bool operator<(const mines x)const
	{
		return (h != x.h ? h < x.h : l < x.l);
	}
}p[s + 2];

void color(WORD c)
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);
}

void intro()
{
	memset(a,'?',sizeof(a));
	memset(b,'0',sizeof(b));
	for(int i = 1;i <= s;i++)
		a[i][0] = a[0][i] = b[i][0] = b[0][i] = '1';
}

char lei(int x,int y)
{
	int cnt = 0;
	for(int i = x - 1;i <= x + 1;i++)
	{
		for(int j = y - 1;j <= y + 1;j++)
		{
			if(i < 1 || i > s || j < 1 || j > s)
				continue;
			cnt += (b[i][j] == '9');
		}
	}
	return cnt + '0';
}

void init()
{
	srand(time(0));
	for(int i = 1;i <= boom;i++)
	{
		p[i].h = rand() % s + 1;
		p[i].l = rand() % s + 1;
		for(int j = 1;j < i;j++)
		{
			while(p[i].h == p[j].h && p[i].l == p[j].l)
			{
				p[i].h = rand() % s + 1;
				p[i].l = rand() % s + 1;
			}
		}
		b[p[i].h][p[i].l] = '9';
	}
	sort(p + 1,p + boom + 1);
	for(int i = 1;i <= s;i++)
	{
		for(int j = 1;j <= s;j++)
		{
			if(b[i][j] != '9')
				b[i][j] = lei(i,j);
		}
	}
}

bool neard(int x,int y)
{
    for(int i = x - 1;i <= x + 1;i++)
        for(int j = y - 1;j <= y + 1;j++)
            if(a[i][j] == '0')
                return true;
    return false;
}

void open(int h1, int l1)
{
	a[h1][l1] = b[h1][l1];
	for(int t = 1;t < s;t++)
	{
	    for(int i = 1;i <= s;i++)
	    {
	        for(int j = 1;j <= s;j++)
	        {
				if(neard(i,j))
					a[i][j] = b[i][j];
	        }
	    }
	}
}

void drawboard()
{
	system("cls");
	cout << "输入3个数a,b,c,代表行、列、操作(0标记/取消标记,1打开),行列下标从1开始\n";
	cout << "注意:不保证第一次不是雷(16×16,40个雷)\n";
	for(int i = 1;i <= s;i++)
	{
		for(int j = 1;j <= s;j++)
		{
			if(a[i][j] == '0')
			{
				color(15);
				cout << a[i][j] << ' ';
			}
			if(a[i][j] == '1')
			{
				color(11);
				cout << a[i][j] << ' ';
			}
			if(a[i][j] == '2')
			{
				color(10);
				cout << a[i][j] << ' ';
			}
			if(a[i][j] == '3' || a[i][j] == '5')
			{
				color(12);
				cout << a[i][j] << ' ';
			}
			if(a[i][j] == '4')
			{
				color(9);
				cout << a[i][j] << ' ';
			}
			if(a[i][j] == '6')
			{
				color(3);
				cout << a[i][j] << ' ';
			}
			if(a[i][j] == '7')
			{
				color(0x70);
				cout << a[i][j];
				color(0x0F);
				cout << ' ';
			}
			if(a[i][j] == '8')
			{
				color(8);
				cout << a[i][j] << ' ';
			}
			if(a[i][j] == '?')
			{
				color(7);
				cout << a[i][j] << ' ';
			}
			if(a[i][j] == '!')
			{
				color(12);
				cout << a[i][j] << ' ';
			}
		}
		cout << endl;
	}
	color(7);
}

bool gamewin()
{
	int cnt = 0;
	for(int i = 1;i <= s;i++)
	{
		for(int j = 1;j <= s;j++)
		{
			if(b[i][j] == '9')
				cnt++;
			else if(a[i][j] != '?')
				cnt++;
		}
	}
	return cnt == s * s;
}

int main()
{
	srand(time(0));
	intro();
	init();
	drawboard();
	while(1)
	{
		int h1,l1;
		bool caozuo;
		cin >> h1 >> l1 >> caozuo;
		if(caozuo)
		{
			if(a[h1][l1] != '!')
			{
				if(b[h1][l1] == '9')
				{
					cout << "游戏失败" << endl;
					for(int i = 1;i <= s;i++)
					{
						for(int j = 1;j <= s;j++)
						{
							if(b[i][j] == '0')
								color(0xF);
							if(b[i][j] == '1')
								color(0xB);
							if(b[i][j] == '2')
								color(0xA);
							if(b[i][j] == '3' || b[i][j] == '5')
								color(0xC);
							if(b[i][j] == '4')
								color(0x9);
							if(b[i][j] == '6')
								color(0x3);
							if(b[i][j] == '7')
								color(0x70);
							if(b[i][j] == '8')
								color(0x8);
							if(b[i][j] == '9')
								color(0x7);
							if(b[i][j] != '9')
								cout << b[i][j] << ' ';
							else
								cout << "* ";
						}
						cout << endl;
					}
					color(0x7);
					return 0;
				}
				open(h1,l1);
			}
			drawboard();
			if(gamewin())
			{
				cout << "游戏胜利" << endl;
				return 0;
			}
		}
		else
		{
			if(a[h1][l1] == '?')
				a[h1][l1] = '!';
			else if(a[h1][l1] == '!')
				a[h1][l1] = '?';
			drawboard();
		}
	}
}