C++ 面向对象写汉罗塔游戏(作风::懒)

<pre name="code" class="cpp">#include <iostream>

using namespace std;

class data
{
public:
	data();
	~data()
	{
		if (p)
			delete[]p;
		if (t)
			delete[]t;
	}
	void SetH();
	int GetH(const unsigned char ch);
	void show_map();
	void Run();
	int Top(const unsigned int i);
	void init(const unsigned int i);
	void initT();
	void mova_T(const unsigned int leve, const int res, const int NewRes);
	void bilid(const unsigned int i);
private:
	enum{Min=3,Max=8};
	class stack
	{
	public:	
		stack();
		~stack()
		{
			if (arr)
			{
				delete[]arr;
			}
		}
		void Push(const int i);
		const int Pop();
		int GetH()const{ return H; }
		int Pops()
		{
			--H;
			return arr[H];
		}
		void SetH(const int i){ H = i; }
		int Top(){ return arr[H - 1]; }
	private:
		int H;
		int *arr;
	};
	class T
	{
	public:
		T() :H(0),number(0),arr(0){}
		int GetH()const { return H; }
		void SetH(const int i){ H = i; }
		int GetArr()const{ return arr; }
		void SetArr(const int i){ arr = i; }
		int GetNumber(){ return number; }
		void SetNumber(const int i){ number = i; }
	private:
		int H;
		int arr;
		int number;
	};
	stack *p;
	T *t;
	bool quit;
};
data::data()
{
	quit = false;
	p = new stack[Min];
	t = new T[Max];
}
data::stack::stack() :H(0)
{
	arr = new int[Max];
	memset(arr, 0, sizeof(int)*Max);
}
void data::stack::Push(const int i)
{
	arr[H] = i;
	H++;
}
void data::init(const unsigned int i)
{
	for (int j = 0; j < i; j++)
	{
		p[0].Push(j+1);
	}
}
void data::initT()
{
	for (int i = 0; i < Max; i++)
	{
		t[i].SetH(i+1);
		t[i].SetNumber(Min + 1);
		t[i].SetArr(Max-(i+1));
	}
}
void data::bilid(const unsigned int i)
{
	for (int j = 0; j < i; j++)
	{
		t[j].SetNumber(0);
	}
}
void data::show_map()
{
	for (int i = 0; i < Max; i++)
	{
		for (int j = 0; j < Min; j++)
		{
			if (t[0].GetArr() == i&&t[0].GetNumber() == j)
			{
				cout << "  |          1          | ";
			}
			else if (t[1].GetArr() == i&&t[1].GetNumber() == j)
				cout << "   |         2         |  ";
			else if (t[2].GetArr() == i&&t[2].GetNumber() == j)
				cout << "    |        3        |   ";
			else if (t[3].GetArr() == i&&t[3].GetNumber() == j)
				cout << "     |       4       |    ";
			else if (t[4].GetArr() == i&&t[4].GetNumber() == j)
				cout << "      |      5      |     ";
			else if (t[5].GetArr() == i&&t[5].GetNumber() == j)
				cout << "       |     6     |      ";
			else if (t[6].GetArr() == i&&t[6].GetNumber() == j)
				cout << "        |    7    |       ";
			else if (t[7].GetArr() == i&&t[7].GetNumber() == j)
				cout << "         |   8   |        ";
			else
				cout << "                          ";
		}
		cout << endl;
	}
	cout << "=============================================================================" << endl;
}
int data::GetH(const unsigned char ch)
{
	switch (ch)
	{
	case 'A':
		return p[0].GetH();
		break;
	case 'B':
		return p[1].GetH();
		break;
	case 'C':
		return p[2].GetH();
		break;
	}
}
int data::Top(const unsigned int i)
{
	switch (i)
	{
	default:
		return p[i].Top();
		break;
	}
}
void data::mova_T(const unsigned int leve, const int res, const int NewRes)
{
	int Newarr = 0;
	if (leve <= Max / 2)
	{
		if (leve == Max / 2)
		{
			Newarr = leve  -1+ leve - (p[NewRes].GetH());
		}
		else
		{
			Newarr = leve + 1 + leve - (p[NewRes].GetH());
		}
	}
	else
		Newarr = Max - 1 - (p[NewRes].GetH());

	t[p[res].Top() - 1].SetArr(Newarr);
	t[p[res].Top() - 1].SetNumber(NewRes);

	p[NewRes].Push(p[res].Pops());


}
void data::Run()
{
	unsigned int leve=0, res=0, NewRes=0, sp = 0;;
	cout << "请输入游戏等级:";
	cin >> leve;
	while (leve<3 || leve>8)
	{
		cout << "\n请重新输入:";
		cin >> leve;
	}
	init(leve);//初始化构建塔元素;
	initT();
	bilid(leve);//构建模拟塔
	
	while (!quit)
	{
		
		while (true)
		{
			system("cls");
			show_map();
			cout << "雷峰塔有几:" << GetH('A');
			cout << "                    雷峰塔B有几:" << GetH('B');
			cout << "                雷峰塔C有几" << GetH('C');
			cout << "\n此时走了" << sp << endl;
			cout << "请输入你的移动塔和零时塔:";
			cin >> res;
			cin >> NewRes;
			--res;
			--NewRes;
			if (res < 0 || res >= 3 || NewRes < 0 || NewRes >= 3)
			{
				cout << "输入的地点不在范围;" << endl;
				break;
			}
			if (Top(res) < Top(NewRes))
			{
				cout << "\n材料不符合\n";
				break;
			}
			if (res != NewRes)
			{
				sp++;
				mova_T(leve,res,NewRes);
			}
			else
			{
				cout << "请认真玩游戏\n";
				break;
			}

		}
	}



}
void main111()
{

	data *r = new data;
	r->Run();
	delete r;


	



	system("pause");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值