【游戏】合到十源码

#include <iostream>
#include <cstring>
#include <windows.h>
#include <cstdlib>
#include <string>

#define SIZE 103

int a[SIZE][SIZE], b[SIZE][SIZE], n = 5, st, hh = 3, sc = 0, la;
bool flag;

using namespace std;

void dfs(int x, int y) // 消除
{
	int c;
	
	if (a[x][y] == 0)
	{
		return;
	}
	if (((x > n) || (x < 1)) || ((y > n) || (y < 1)))
	{
		return;
	}
	c = a[x][y];
	a[x][y] = 0;
	sc += c;
	la += c;
	if ((x > 1) && (a[x-1][y] == c))
	{
		flag = true;
		dfs(x - 1, y);
	}
	if ((x < n) && (a[x+1][y] == c))
	{
		flag = true;
		dfs(x + 1, y);
	}
	if ((y > 1) && (a[x][y-1] == c))
	{
		flag = true;
		dfs(x, y - 1);
	}
	if ((y < n) && (a[x][y+1] == c))
	{
		flag = true;
		dfs(x, y + 1);
	}
	
	return;
}

int main(int argc, char** argv)
{
	int m, i, j, k, x = 1, y = 1, cc;
	
	srand(cc);
	a[n][n] = 3;
	while (true) {
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
	cin >> x >> y;
	la = 0;
	flag = false;
	cc = a[x][y];
	dfs(x, y);
	if (flag) a[x][y] = ++cc, ++st; else a[x][y] = cc, sc -= cc, la = 0;
	hh = max(a[x][y], hh);
	memset(b, 0, sizeof (b));
	for (i = 1; i <= n; i++)
	{
		k = 0;
		for (j = n; j >= 1; j--)
		{
			if (a[j][i])
			{
				b[n-k][i] = a[j][i];
				k++;
			}
		}
	}
	Sleep(300);
	system("cls");
	for (i = 1; i <= n; i++)
	{
		for (j = 1; j <= n; j++)
		{
			if (b[i][j] == 0)
			{
				srand(cc + y * i + 3 * (i + j + j * i) + x * j + n * i + j + rand() % 233); // 随机填空
				cc = rand() % 100;
				if (cc < 80)
				{
					b[i][j] = 1;
				}
				else if (cc < 95)
				{
					b[i][j] = 2;
				}
				else
				{
					b[i][j] = 3;
				}
			}
			a[i][j] = b[i][j];
			SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a[i][j] * 1);
			
			if ((i == x) && (j == y))
			{
				SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 233);
			}
			if (a[i][j] < 10)
			{
				cout << " ";
			}
			if (a[i][j] < 100)
			{
				cout << " ";
			}
			cout << b[i][j] << " ";
		}
		cout << endl;
	}
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
	cout << "Steps:" << st << endl;
	cout << "Highest:";
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), hh);
	cout << hh << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
	cout << "Score:" << sc;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
	cout << " +" << la << endl;
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/drtlstf/article/details/80951529