#include<windows.h>
#include<iostream>
#include<cstdlib>
#include<conio.h>
#include<cstring>
#include<string>
#include<cctype>
#include<ctime>
using namespace std;
typedef string plate;
plate numPlate[6]={" "," @@@@@@@@@@@"," ######### "," $$$$$$$ "," ***** "," ±±± "};
#define f(hs,nps) \
if(hs.length()>=nps) \
cout<<(hs.z[nps]); \
else \
cout<<numPlate[0];
int tot=0;
int pow(int a,int b)
{
int n=1;
if(b==0) return 1;
while(b--)
{
n*=a;
}
return n;
}
class hanoiStack
{
public:
hanoiStack()
{
len=1;
memset(r,0,sizeof(r));
for(int i=0;i<1000;i++) z[i]="";
}
plate z[1000];
int r[1000];
int len;
void operator+=(hanoiStack& x)
{
if(x.length()<=0||x.r[x.length()]<r[len-1]) return;
cout<<"move...\n";
push(x.z[x.length()]);
x.pop();
tot++;
}
int length()
{
return len-1;
}
void push(plate x)
{
switch(x[5])
{
case ' ':r[len]=0;break;
case '@':r[len]=1;break;
case '#':r[len]=2;break;
case '$':r[len]=3;break;
case '*':r[len]=4;break;
case '\241':r[len]=5;break;
}
z[len++]=x;
}
void pop()
{
r[len]=0;
z[len--]="";
}
};
class hanoi
{
public:
hanoiStack A;
hanoiStack B;
hanoiStack C;
void printHanoi()
{
f(A,5);
f(B,5);
f(C,5);
cout<<endl;
f(A,4);
f(B,4);
f(C,4);
cout<<endl;
f(A,3);
f(B,3);
f(C,3);
cout<<endl;
f(A,2);
f(B,2);
f(C,2);
cout<<endl;
f(A,1);
f(B,1);
f(C,1);
cout<<endl;
cout<<" ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄";
}
};
void move(hanoi& h,hanoiStack& a, char b)
{
switch(b)
{
case 'A':h.A+=a;break;
case 'B':h.B+=a;break;
case 'C':h.C+=a;break;
}
}
int main()
{
char at, to;
hanoi h;
int num;
cout<<"输入挑战盘数(1~5):";
cin>>num;
if(num<1) num=3;
if(num>5) num=5;
for(int i=1;i<=num;i++)
{
h.A.push(numPlate[i]);
}
cout<<"依次输入移动指令。如\"A C\"表示将A柱上的第一个盘移动到C柱上面。\n本游戏是将A柱上的所有盘移动到C柱。\n\t规则:不允许大盘压小盘。只能一个一个拿。\n同意按任意键继续。";
getch();
system("cls");
clock_t start=clock();
while(h.C.length()!=num)
{
h.printHanoi();
cout<<"移动次数:"<<tot<<endl;
cout<<"所用时间:"<<((clock()-start)/1000)<<"秒"<<endl;
cout<<"\nCommand(Exanple:\"A C\"):";cin>>at>>to;
at=toupper(at);
to=toupper(to);
switch(at)
{
case 'A':move(h,h.A,to);break;
case 'B':move(h,h.B,to);break;
case 'C':move(h,h.C,to);break;
}
Sleep(100);
system("cls");
}
h.printHanoi();
clock_t end=clock();
Sleep(1000);
system("cls");
cout<<"恭喜!你赢了!\n用时"<<(((end-start)-100*tot)/1000)<<"秒"<<endl;
int ci=pow(2,num)-1;
if(tot==ci) cout<<"\t正解!\n";
else if(tot-ci>10) cout<<"\t再接再厉!\n";
else cout<<"\t一般般。\n";
getch();
return 0;
}