📄 tower.cpp
字号:
#include "Stack.cpp"
#include<sstream>
// Hanoi class
class Hanoi
{
private:
Stack<int> s1, s2, s3;
bool flag;
int size;
void moveDisk(Stack<int> & s1, Stack<int> & s2); // if the movement is possible, move the disk at the top of s1 to s2
void printStack(Stack<int>& m, int tag);
bool IsOver(); // function to examine whether the game is over
public:
Hanoi(int n);
void RunGame(); // function to repeat movement until game is over
void DisplayStatus(); // display current status of each tower
};
// function definition +++++++++++++++++++++++++++++++++++++++++++++++++++++
Hanoi::Hanoi(int n):s1(n), s2(n), s3(n),size(n) // constructor
{
flag=true;
for(int i=n-1; i>=0; i--)
{
if(!s1.push(i)) break;
}
cout<<"Now you have three tower that have No. 1, 2, 3 respectively\n";
cout<<"you are to move all disks from stack 1 to stack 3 according Tower of Hanoi rules\n";
}
void Hanoi:: moveDisk(Stack<int> & source, Stack<int> & dest)
{
int temp=-1;
int destDiskNo=-1;
const string MSG = "you can't move like this!\n";
try
{ // examine whether the movement is possible. If not possible throw exception
if(source.pop(temp)==true)
{
if(dest.pop(destDiskNo)==true)
{
dest.push(destDiskNo);
if(temp < destDiskNo)
{
dest.push(temp);
}
else
{
source.push(temp);
throw (MSG); // throw exception1
}
}
else
dest.push(temp);
}
else
throw (MSG); // throw exception2
DisplayStatus(); //display status after movement
return;
}
catch(string MSG)
{
cout<<MSG<<endl;
return; }
}
// function to repeat movement until game is over
void Hanoi:: RunGame()
{
while(!IsOver())
{
int a=1, b=2;
cout<<"please input the source disk No. and the destination disk No.\n";
cin>>a>>b;
if(a==0 && b==0)
{
cout<<"you select to quit the game!\n";
return ;
}
else
{
switch(a)
{
case 1:
switch(b)
{
case 1:
//moveDisk(s1, s1);
break;
case 2:
moveDisk(s1, s2);
break;
case 3:
moveDisk(s1, s3);
break;
}break;
case 2:
switch(b)
{
case 1:
moveDisk(s2, s1);
break;
case 2:
//moveDisk(s2, s2);
break;
case 3:
moveDisk(s2, s3);
break;
}break;
case 3:
switch(b)
{
case 1:
moveDisk(s3, s1);
break;
case 2:
moveDisk(s3, s2);
break;
case 3:
//moveDisk(s3, s3);
break;
}break;
}
}
}
cout<<"Congratulation! You are successful to complete the game.\n";
}
// function to examine whether the game is over
bool Hanoi::IsOver()
{
int temp1, temp2;
if(s1.pop(temp1))
s1.push(temp1);
else if(s2.pop(temp2))
s2.push(temp2);
else
return true; // if s1 is empty and s2 is empty, game is over
return false;
}
void Hanoi::DisplayStatus() // function to display current state of each tower
{
printStack(s1, 1);
printStack(s2, 2);
printStack(s3, 3);
}
void Hanoi::printStack(Stack<int>& s, int stackNumber)
{
cout<<"stack "<<stackNumber<<endl;
int x[1024];
bool voidStack;
int sum=0;
for(int i=0; i<1024; i++)
{
voidStack=s.pop(x[i]);
if(!voidStack) break;
sum++;
for(int j=0; j<x[i]+1;j++)
cout<<'+';
cout<<'\n';
}
for(int tm=sum-1;tm>=0; tm--)
{
s.push(x[tm]);
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int main(int argc, char** argv)
{
int n=0;
cout<<"Please input the number of disks"<<"\n";
cin>>n;
if(argv[1]!=NULL)
{
string s(argv[1]);
istringstream sin(s);
sin>>n;
}
else {
cout<<"you didn't input the number of disks\n";
return 0;
}
Hanoi hanoi(n);
hanoi.DisplayStatus();
hanoi.RunGame();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -