<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");
}
C++ 面向对象写汉罗塔游戏(作风::懒)
最新推荐文章于 2023-11-26 00:10:26 发布