C++小游戏——2048 v1.2.0

加入洛谷团队98418获取更多游戏资源!

2048 v1.2.0: 修改边框大小以及数字颜色

代码见下,或前往团队下载

如果报错,打开工具

在这里插入图片描述

点击编译选项

请添加图片描述

添加-std=c++11

请添加图片描述

打钩

在这里插入图片描述

确定

在这里插入图片描述

编译运行即可

#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
#define CLS system("CLS")
#define scta SetConsoleTextAttribute
#define gsh GetStdHandle
#define soh STD_OUTPUT_HANDLE
#define fi FOREGROUND_INTENSITY
#define red FOREGROUND_RED
#define blue FOREGROUND_BLUE
#define green FOREGROUND_GREEN
#define common_col scta(gsh(soh), fi | red | blue | green);
using namespace std;

int mp[10][10];
char break_up[70] = "---------------------------------",
	 empty_line[70] = "|       |       |       |       |";

default_random_engine e;
uniform_int_distribution<int> u1(1,2), u2(0,15);

void set_rand()
{
	e.seed(time(0));
}

void num_give()
{
	int p = u2(e);
	int x = p % 4, y = p / 4;
	while (mp[x][y])
	{
		p++;
		p %= 16;
		x = p % 4;
		y = p / 4;
	}
	mp[x][y] = pow(2, u1(e));
}

bool if_lose()
{
	for(int i = 0; i < 4; i++)
	{
		for(int j = 1; j < 4; j++)
		{
			if(mp[i][j] == mp[i][j - 1] || mp[j][i] == mp[j - 1][i]) return false;
			if(mp[i][j] == 0 || mp[i][j - 1] == 0) return false;
		}
	}
	return true;
}

void num_print(int need_prt)
{
	for(int i = 0; i <= 20; i++)
	{
		if (need_prt == (1 << 1 + i * 6)) {
			scta(gsh(soh), fi | red | green);	//黄色 
			break;
		} else if (need_prt == (1 << 2 + i * 6)) {
			scta(gsh(soh), fi | green);	//绿色 
			break;
		} else if (need_prt == (1 << 3 + i * 6)) {
			scta(gsh(soh), fi | green | blue);	//青色 
			break;
		} else if (need_prt == (1 << 4 + i * 6)) {
			scta(gsh(soh), fi | blue);	//蓝色 
			break;
		} else if (need_prt == (1 << 5 + i * 6)) {
			scta(gsh(soh), fi | blue | red);	//粉色 
			break;
		} else if (need_prt == (1 << 6 + i * 6)) {
			scta(gsh(soh), fi | red);	//红色 
			break;
		}
	}
	printf("%d", need_prt);
	common_col;
}

void lose()
{
	CLS;
	printf("You lose!\n\n");
	getch();
}

bool move()
{
	bool flag = false;
	char c = getch();
	int temp[10][10];
	for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) temp[i][j] = mp[i][j];
	if(c == 'w')
	{
		for(int i = 0; i < 4; i++)
		{
			queue<int> q1, q2;
			int lst;
			for(int j = 0; j < 4; j++)
			{
				if(mp[j][i]) q1.push(mp[j][i]);
			}
			if(q1.empty()) continue;
			lst = q1.front();
			q1.pop();
			while(!q1.empty())
			{
				if(lst == -1) lst = q1.front();
				else if(lst == q1.front()) q2.push(2 * lst), lst = -1;
				else q2.push(lst), lst = q1.front();
				q1.pop();
			}
			if(lst != -1) q2.push(lst);
			for(int j = 0; j < 4; j++) mp[j][i] = 0;
			for(int j = 0; j < 4 && !q2.empty(); j++)
			{
				mp[j][i] = q2.front();
				q2.pop();
			}
		}
	}
	else if(c == 's')
	{
		for(int i = 0; i < 4; i++)
		{
			queue<int> q1, q2;
			int lst;
			for(int j = 3; j >= 0; j--)
			{
				if(mp[j][i]) q1.push(mp[j][i]);
			}
			if(q1.empty()) continue;
			lst = q1.front();
			q1.pop();
			while(!q1.empty())
			{
				if(lst == -1) lst = q1.front();
				else if(lst == q1.front()) q2.push(2 * lst), lst = -1;
				else q2.push(lst), lst = q1.front();
				q1.pop();
			}
			if(lst != -1) q2.push(lst);
			for(int j = 0; j < 4; j++) mp[j][i] = 0;
			for(int j = 3; j >= 0 && !q2.empty(); j--)
			{
				mp[j][i] = q2.front();
				q2.pop();
			}
		}
	}
	else if(c == 'a')
	{
		for(int i = 0; i < 4; i++)
		{
			queue<int> q1, q2;
			int lst;
			for(int j = 0; j < 4; j++)
			{
				if(mp[i][j]) q1.push(mp[i][j]);
			}
			if(q1.empty()) continue;
			lst = q1.front();
			q1.pop();
			while(!q1.empty())
			{
				if(lst == -1) lst = q1.front();
				else if(lst == q1.front()) q2.push(2 * lst), lst = -1;
				else q2.push(lst), lst = q1.front();
				q1.pop();
			}
			if(lst != -1) q2.push(lst);
			for(int j = 0; j < 4; j++) mp[i][j] = 0;
			for(int j = 0; j < 4 && !q2.empty(); j++)
			{
				mp[i][j] = q2.front();
				q2.pop();
			}
		}
	}
	else if(c == 'd')
	{
		for(int i = 0; i < 4; i++)
		{
			queue<int> q1, q2;
			int lst;
			for(int j = 3; j >= 0; j--)
			{
				if(mp[i][j]) q1.push(mp[i][j]);
			}
			if(q1.empty()) continue;
			lst = q1.front();
			q1.pop();
			while(!q1.empty())
			{
				if(lst == -1) lst = q1.front();
				else if(lst == q1.front()) q2.push(2 * lst), lst = -1;
				else q2.push(lst), lst = q1.front();
				q1.pop();
			}
			if(lst != -1) q2.push(lst);
			for(int j = 0; j < 4; j++) mp[i][j] = 0;
			for(int j = 3; j >= 0 && !q2.empty(); j--)
			{
				mp[i][j] = q2.front();
				q2.pop();
			}
		}
	}
	else if(c == '0')
	{
		int x, y, val;
		cin >> x >> y >> val;
		mp[x][y] = val;
	}
	for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) if(mp[i][j] != temp[i][j]) return true;
	return false;
}

void game_main()
{
	int max_num = 0;
	memset(mp, 0, sizeof(mp));
	while(1)
	{
		CLS;
		num_give();
		for (int i = 0; i < 4; i++)
		{
			printf("%s\n%s\n", break_up, empty_line);
			for (int j = 0; j < 4; j++)
			{
				printf("|");
				if(mp[i][j])
				{
					max_num = max(max_num, mp[i][j]);
					num_print(mp[i][j]);
				}
				printf("\t");
			}
			printf("|\n%s\n", empty_line);
		}
		printf("%s", break_up);
		if(if_lose())
		{
			lose();
			return;
		}
		while(!move()){}
	}
}

int main()
{
//	gamestart();
	set_rand();
	while (1)
	{
		common_col;
//		gameset();
		game_main();
//		if (!ifagain())
//		{
//			break;
//		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值