DOS版2048

本人写的C++2048源码加注释,用的DOS做界面所以界面可能就没法看了....游戏还是基本没问题,调试了很久,用Vector做了棋盘,事实证明容器确实不适合做数组的工作,调试的时候经常out of range 如果有人发现BUG了可以回复我,我会再做修改的

#include<iostream>
#include<regex>
#include<string>
#include<fstream>
#include<iomanip>
#include<random>
#include<vector>
#include<algorithm>
#include<ctime>
#include <conio.h>
using namespace std;
#define winnumber 2048
void hello()
{
	cout << setw(50) << "欢迎来到李琦2048" << endl;
	double start = double(clock()) / CLOCKS_PER_SEC;
	while ((double(clock()) / CLOCKS_PER_SEC - start) < 1);
	system("cls");
	cout << "\n\n\n\t\t\t" << "即将进入游戏"<<endl;
	for (int i = 3; i > 0; i--)
	{	
		system("cls");
		cout << "\n\n\n\t\t\t倒计时" << i << "秒" << endl;
		double start = double(clock()) / CLOCKS_PER_SEC;
		while ((double(clock()) / CLOCKS_PER_SEC - start) < 1);
		

	}
}

class Qipan
{
public:
	Qipan() {
		vector<int> pan;
		int m, n;//开始数字位置
		for (int i = 0; i < 4; i++)
			pan.push_back(0);
		for (int i = 0; i < 4; i++)
		{
			pane.push_back(pan);
		}
			//for (int i = 0; i <= 3; i++) //初始化棋盘
			//	for (int j = 0; j <= 3; j++)//开始用二维数组 很奇怪的是使用全部初始化得到结果并不是0语句为:pane[4][4]={0};
			//	{
			//		
			//		if (i == 3) pane[i][j] = 2;
			//	}
		for (int i = 0; i < 2; i++)
		{
			m = rand() % 4;
			n = rand() % 4;
			pane[m][n] = 2;
		}
		}	
	Qipan(Qipan &qipan1)=default; //拷贝控制成员,没有指针成员可以不写。
	int operator =(int);
	void update();
	void randpane();
	bool ifzero(int x);
	int moveup();
	int movedown();
	int moveleft();
	int moveright();
	int GetDirection();
	bool wingame();
	int maxnumber();

private:
	vector<vector<int>> pane;
};
int Qipan::operator=(int x)
{
	cout << "lilili" << endl;
	return 3;
}
int Qipan::maxnumber()
{
	int max = pane[0][0];
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			if (pane[i][j] > max)
				max = pane[i][j];
		}
	}
	return max;
}
//Qipan::Qipan(Qipan &qipan1)
//{
//	vector<int> pan;
//	int m, n;//开始数字位置
//	for (int i = 0; i < 4; i++)
//		pane.push_back(qipan1.pane[i]);
//	//for (int i = 0; i < 4; i++)
//	//{
//	//	this->pane.push_back(pan);
//	//}
//	//for (int i = 0; i < 4; i++)
//	//	for (int j = 0; j < 4; j++)
//	//	{
//	//		this->pane[i][j] = qipan1.pane[i][j];
//	//	}
//}
void Qipan::update()
{
	system("cls");
	cout << setw(36) << "李琦版2048" << endl;
	cout << setw(46) << "|-----|-----|-----|-----|" << endl;
	for(int i=0;i<4;i++)
		for (int j = 0; j < 4; j++)
		{	
			if (pane[i][j] == 0) {

				if (j == 0)
					cout << setw(20) << "" << setw(2) << "|" << setw(4)<<"";
				else
					cout << setw(2) << "|" << setw(4)<<"";
				if (j == 3)
				{
					cout << setw(2) << "|" << endl;
					cout << setw(46) << "|-----|-----|-----|-----|" << endl;
				}
			}
			else {
				if (j == 0)
					cout << setw(20) << "" << setw(2) << "|" << setw(3)<< pane[i][j] << " ";
				else
					cout << setw(2) << "|" << setw(3) << pane[i][j] << " ";
				if (j == 3)
				{
					cout << setw(2) << "|" << endl;
					cout << setw(46) << "|-----|-----|-----|-----|" << endl;
				}
			}
		}
}

void Qipan::randpane()
{
	static default_random_engine e(time(0));  //C++新标准的随机引擎
	static uniform_int_distribution<unsigned>u(1, 2);

	int i,m,n;
	i = u(e);
	int newnumber = pow(2, i);
	//while ((i = u(e)) !=2|| (i = u(e)) != 4);
		do {
			 m = rand() % 4;
			 n = rand() % 4;
		} while (pane[m][n] != 0);
		pane[m][n] = newnumber;

}
bool Qipan::ifzero(int x)
{
	if (x != 0);
	return true;
	return false;
}
int Qipan::moveup()
{
	int flag = 0;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 1; j < 4; j++)
		{
			int k = j;
			for (; k > 0; k--)				//数字合并后再向上靠拢一次。
			{
				if (pane[k - 1][i] == 0)
				{
					pane[k - 1][i] = pane[k][i];
					pane[k][i] = 0;
					flag = 1;
				}
			}

		}
		
	}
	this->update();
	for (int i = 0; i < 4; i++)  //数字合并
	{
		for (int j = 1; j < 4; j++)
		{
			if (pane[j - 1][i] == pane[j][i])
			{
				pane[j - 1][i] *= 2;
				pane[j][i] = 0;
				flag = 1;
			}	
		}

	}
	for (int i = 0; i < 4; i++)
	{
		for (int j = 1; j < 4; j++)
		{
			int k = j;
			for (; k > 0; k--)				//用的是冒泡算法
			{
				if (pane[k - 1][i] == 0)
				{
					pane[k - 1][i] = pane[k][i];
					pane[k][i] = 0;
					
				}
			}

		}
	}

	return flag;

}
int Qipan::movedown()
{
	int flag = 0;
		for (int i = 0; i < 4; i++)
		{
			for (int j = 2; j >= 0; j--)
			{
				int k = j;
				for (; k <3; k++)				//数字合并后再向xia靠拢一次。
				{
					if (pane[k + 1][i] == 0)
					{
						pane[k + 1][i] = pane[k][i];
						pane[k][i] = 0;
						flag = 1;
					}
				}

			}

		}
		for (int i = 0; i < 4; i++)  //数字合并
		{
			for (int j = 2; j >=0; j--)
			{
				if (pane[j +1][i] == pane[j][i])
				{
					pane[j + 1][i] *= 2;
					pane[j][i] = 0;
					flag = 1;
				}
			}

		}
		for (int i = 0; i < 4; i++)
		{
			for (int j = 2; j >= 0; j--)
			{
				int k = j;
				for (; k <3; k++)				//数字合并后再向xia靠拢一次。
				{
					if (pane[k + 1][i] == 0)
					{
						pane[k + 1][i] = pane[k][i];
						pane[k][i] = 0;
					}
				}

			}

		}

		return flag;
}
int  Qipan::moveleft()
{
	int flag = 0;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 1; j<4; j++)
		{
			int k = j;
			for (; k >0; k--)				//数字合并后再向zuo靠拢一次。
			{
				if (pane[i][k-1] == 0)
				{
					pane[i][k-1] = pane[i][k];
					pane[i][k] = 0;
					flag = 1;
				}
			}

		}

	}
	for (int i = 0; i < 4; i++)  //数字合并
	{
		for (int j = 1; j <4; j++)
		{
			if (pane[i][j-1] == pane[i][j])
			{
				pane[i][j-1] *= 2;
				pane[i][j] = 0;
				flag = 1;
			}
		}

	}
	for (int i = 0; i < 4; i++)
	{
		for (int j = 1; j<4; j++)
		{
			int k = j;
			for (; k >0; k--)				//数字合并后再向zuo靠拢一次。
			{
				if (pane[i][k - 1] == 0)
				{
					pane[i][k - 1] = pane[i][k];
					pane[i][k] = 0;
				}
			}

		}

	}

	return flag;
}
int  Qipan::moveright()
{
	int flag = 0;
	for (int i = 0; i < 4; i++)
	{
		for (int j = 2; j>=0; j--)
		{
			int k = j;
			for (; k <3; k++)				//数字合并后再向you靠拢一次。
			{
				if (pane[i][k +1] == 0)
				{
					pane[i][k +1] = pane[i][k];
					pane[i][k] = 0;
					flag = 1;
				}
			}

		}

	}
	for (int i = 0; i < 4; i++)  //数字合并
	{
		for (int j = 2; j >=0; j--)
		{
			if (pane[i][j + 1] == pane[i][j])
			{
				pane[i][j + 1] *= 2;
				pane[i][j] = 0;
				flag = 1;
			}
		}

	}
	for (int i = 0; i < 4; i++)
	{
		for (int j = 2; j >= 0; j--)
		{
			int k = j;
			for (; k <3; k++)				//数字合并后再向you靠拢一次。
			{
				if (pane[i][k + 1] == 0)
				{
					pane[i][k + 1] = pane[i][k];
					pane[i][k] = 0;
				}
			}

		}

	}

	return  flag;
}
bool Qipan::wingame()
{
	for(int i=0;i<4;i++)
		for (int j = 0; j < 4; j++)
		{
			if (pane[i][j] >= winnumber)
			{
				cout << setw(45) << "You Win!" << endl;
				return false;
			}
		}
	return true;
}
bool gameover(Qipan qipan)
{
	if (!(qipan.movedown() || qipan.moveleft() || qipan.moveup() || qipan.moveright()))
	{
		cout << setw(45) << "Game over" << endl;
			cout<< setw(45) << "Your score is " << qipan.maxnumber();
		return false;
	}
	return true;
}
int Qipan::GetDirection()
{

	int ret = 0;
	do
	{
		int ch = _getch();
		if (isascii(ch)) 
			continue;
		ch = _getch();
		switch (ch)
		{
		case 72:
			ret = 2; // top
			break;
		case 75:
			ret = 1; // left 
			break;
		case 77:
			ret = 3; // right
			break;
		case 80:
			ret = 4; // down
			break;
		default:
			break;
		}
	} while (ret == 0);
	return ret;
}

int main()
{
	
	while (1)
	{
		system("color e4");
		Qipan qipan;
		hello();	
		qipan.update();
		int testup, testdown,testleft, testright;
		while (qipan.wingame() && gameover(qipan))
		{
			int i = qipan.GetDirection();
			switch (i)
			{
			case 2:
				if (testup = qipan.moveup())
				{
					qipan.randpane();
					qipan.update();
				}
				
				break;
			case 4:
				 if (testdown = qipan.movedown())
				{
					qipan.randpane();
					qipan.update();
				}
				break;
			case 1:
				if (testleft = qipan.moveleft())
				{
					qipan.randpane();
					qipan.update();
				}
				break;
			case 3:
				if (testright = qipan.moveright())
				{
					qipan.randpane();
					qipan.update();
				}
				break;
			default:
				break;
			}
		}
		int a=qipan.maxnumber();
		char x;
	loop:
		
		cout << "Do you want to play again?Y/N";
		cin >> x;
		if (x == 'Y' || x == 'y')
			continue;
		else if (x == 'N' || x == 'n')
			break;
		else
		{
			cout << "Invalid input";
			goto loop;
		}


	}

	return 0;
}


MyQQ is a cross-platform library for communication which uses a TencentQQ-like protocol to communicate with friends on the Internet. It can work well now and maintained by Xiaoxia (gdxxhg@gmail.com). If you are interested in MyQQ and have improved it, I suggest you that you send your source code to me then everyone will know your work and thank you! You can get the latest version of this software (including its source code) at http://home.xxsyzx.com 注意:本软件以及源代码仅供学习研究使用。所用协议皆属个人业余的黑匣分析结果。 Developer List: 小虾 (gdxxhg@gmail.com) 千月(改进myqq.c界面) ccpaging Windows编译: 需要Mingw32(Devcpp的bin也可以)。 打开控制台,在当前目录下执行 make -C src clean all Linux编译: 在终端里执行 make -C src -flinux.mak clean all MacOSX编译: 在终端里执行 make -C src -fmac.mak clean all 由于最初没有周全考虑,目前本暂时不兼容64位的机器,望见谅! Update History: Version 3.17 (2009-6-30) 1. 修正发送消息后头像的变化。 2. 修正09SP1接收信息的bug。 3. 修正0x18包中获取好友失败时的内存访问错误。 4. 修正09SP1系统消息协议。 5. 盲目修正09接收消息的bug。 6. 增加qqconn.c,支持代理登录。 Version 3.16 (2009-6-27) 1. 增加NoColor配置项关闭色彩文字。 2. 增加cls/clear/clrscr命令来清屏。 3. 提示消息发送失败,但不具体。 Version 3.15 (2009-6-24) 1. 增加qqclient_detach()。 2. 编写成libqq,供外部程序调用。 Version 3.14 (2009-6-21) 1. 命令行参数方式登录失败后的死循环。 2. 内核使用QQ2009SP1协议。 3. 修正08之前接收消息的bug。 4. 兼容Mac32。 5. utf8.c里添加qqdef.h头文件。 Version 3.13 (2009-3-29) 1. Linux(Ubuntu) version compiled! Version 3.12 (2009-3-22) 1. 用Windows的Sleep代替不推荐的_sleep。 2. 在Mingw32-gcc4.3.3上编译成功。 Version 3.11 (2009-2-8) 1. 输入验证码提示。 2. 修正myqq.c删除好友的bug。 3. 修正添加附言的bug。 Version 3.10 (2009-2-7) 1. 修正09接收消息协议。 2. 全部源代码文件更改为UNIX-UTF8格式。 3. 增加Preference来自定义屏蔽部分协议。 4. 增加添加、删除好友基本协议,支持验证码。 5. 登录后更改状态。 6. 登录机器数据随机填充。 7. 修正myqq.c里无法打印某些消息的bug。 Version 3.08 (2009-1-27) 1. 修正myqq.c里显示好友状态。 2. 修正因缺少pthread_mutex_destroy引起的资源泄漏(可以检测到)。 3. 修正好友数目多时导致分组信息与群信息未能获取的问题。 4. 10分钟刷新群在线成员。 5. 自动建立qqconfig.txt配置文件。 Version 3.03 (2009-1-26) 1. 修正prot_im.c中buf->pos += get_word( buf );在Linux上运行时发生的错误。 2. 编译Linux本。 3. qqconfig.txt缺失提示。 Version 3.00 (2009-1-25) 1. 更换QQ2008贺岁协议为QQ2009Preview4协议。 2. 全面使用utf8,myqq.c为Windows用户转换为gb。 3. 移除所有多余,功能不确切的协议处理函数。 4. 能够接收08,09协议的好友消息与群消息。 5. 处理消息中的表情字符,转换成[face:*]以及[image]来表示。 Version 2.95 (2008-10-26) 1. 处理返回NULL的可能。 2. 服务器列表由配置文件导入。 Version 2.9 (2008-10-1) 1. 支持字符颜色显示。 2. 中文字符界面。 3. 补充了几个新的服务器IP。 4. 输入密码时去掉回显或打*。 5. 修正prot_login_verify_password_reply处理返回包的误解,感谢CC-Akita(CCPaging)。 6. 增加Dev-cpp工程文件,增加程序图标。 7. Makefile里定向pthread库,便于不同环境的编译。 Version 2.8 (2008-8-3) 1. Linux Build Support. Version 2.7 (2008-8-2) 1. 使用上次登录IP,加快多Q登录速度。 2. 修正list.c里添加项在满时没有返回<0的漏洞。 3. 在登录未完成时,不接收消息。 Version 2.6 (2008-7-27) 1. 登录中的未知字节用0填充,原来是用随机数。 2. 根据文档,进一步完善登录协议,但还是没有解决验证码的出现问题。 Version 2.5 (2008-7-26) 1. 修正一个发包的bug,包被送进已发送队列之后time_alive应该为当前时间。之前因为 这个问题,导致一个包超时其他包也要重发? 2. 恢复注销命令(没有这个,怎么下线呢)。 3. 登录首先获取所有钥匙,避免出现登录后被Kick out。 4. 感谢网友008的提醒,号码格式化字符%d已被替换为%u。 Version 2.4 (2008-7-24) 1. 好友添加。 2. Packed with pthread library. 3. 删除注销命令(可能引发异常) 4. 重发时长为6秒 Version 2.3 (2008-7-22) New features: 1. 支持UDP协议登录。 2. 调整了登录后的发包次序。 3. 好友使用qsort快排。 4. 完善了event的缓冲区,和以前的webqq结合使用。 5. 使用assert函数,便于精简发布本的代码。 Version 2.0 (2008-7-17) New featrues: 1. 包管理器的链表改用loop数据结构,精简代码,减少错误。 2. (--a)%b可能为负数,原来没注意到,引发了改写mcb的内存错误。 3. 发包序号策略改善,不再每次发包都递增。 Version 1.9 (2008-7-15) New features: 1. 重新策划程序,采用QQ2008贺岁协议。 2. 原来的group改名为qun,原group被用作管理好友分租。 3. 增加memory.c内存管理和debug.c调试信息及日志记录。 4. ccpaging建议改掉qqqun这个结构名,我打算下个本把它改为qun_t,其它类似如 member_t, group_t, buddy_t。 5. 具备登录输入验证码功能,验证码图片保存在web/verify目录下。 6. 修正qqsocket里接收数据溢出。 Version 0.9 (2008-2-12) New features: 1. MyQQ库可以登录多个ID,API全部更新。 2. 修正好友名称里混有QQ号码和特殊字符。 3. 完善了好友状态回调。 4. 延长了重发包的时间,避免发送两次。 5. 修正在自动回复时控制台提示的问题。 6. 回调函数全部使用stdcall模式。 Version 0.6 (2008-2-5) New features: 1. Wait for message to send. 2. Improve input. 3. Check for repeated messages. 4. Add refresh command. Version 0.5 (2008-2-4) New features: 1. Send message to group. 2. Get group list. 3. List groups, online users and online group members. 4. Get group member information. 5. Get buddy information. 6. The original interface works. Version 0.1 (2008-2-1) Use QQ2006 Protocol to login. Support Receiving messages from buddies and groups, sending messages to buddies. Compiled and linked on Linux(GCC 4.1, Debian etch). Compiled and linked on Windows(MingwGCC 3.4.2).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值