昨天没事在用别人的文曲星查字!突然看到那个汉诺塔游戏.....
反正这两天刚好手有点痒,于是就有了:
#ifndef HANIO_H_
#define HANIO_H
class Stack
{
private:
enum{ MAX=50 };
int m_node[MAX];
int m_top;
int m_size;
int m_index;
public:
Stack();
~Stack() { };
bool Isfull() { return m_top==MAX-1 ;}; //堆栈满则返回TRUE
bool Isempty() { return m_top==-1;}; //堆栈空则返回TRUE
int Top() { return m_top; };
int TopValue() { return m_node[m_top];};
int GetDataFromIndex(int i) { return m_node[i]; };
int GetIndex() { return m_index; } ;
void SetIndex(int index) { m_index = index; };
int Size() { return m_top+1; };
bool Push(int data);
bool Pop(int * pData);
int MoveToNext();
void OutPrint();
};
class Hanio
{
Stack m_stack[3];
int m_num; //盘数
int m_steps; //移动次数
int m_times; //完成所用时间
void print(char ch,int n);
public:
Hanio(int num=3);
~Hanio() {};
void GameStart();
bool MoveFromTo(int x,int y); //从x号盘移动到y号盘
void DrawPaletes(char ch='*'); //打印3个堆的盘子
bool IsFinished() ; //结束返回TURE;
int Solve(char from,char to,char auxiliary,int n); //求解其解法路径
};
#endif
//hanio.cpp
#include "hanio.h"
#include
#include
#include
#include
#include
Stack::Stack()
{
m_top=-1;
m_index=m_top;
for(int i=0;i
}
bool Stack::Push(int data)
{
if(Isfull())
return false;
m_top++;
m_node[m_top]=data;
m_index=m_top;
return true;
}
bool Stack::Pop(int *pData)
{
if(Isempty())
return false;
*pData=m_node[m_top];
m_node[m_top]=0;
m_top--;
m_index=m_top;
return true;
}
int Stack::MoveToNext()
{
int temp=m_index;
m_index--;
return m_node[temp];
}
void Stack::OutPrint()
{
if(m_top!=-1)
{
for(int i=0;i<=m_top;i++)
std::cout<<"["<
}
///////////////////////////////////////
Hanio::Hanio(int num)
{
m_num=num;
m_steps=0;
m_times=0;
for(int i=num;i>=1;i--)
m_stack[0].Push(i);
//m_stack[0].OutPrint();
}
void Hanio::print(char ch,int n)
{
for(int i=1;i<=n;i++)
std::cout<
void Hanio::DrawPaletes(char ch) m_stack[0].SetIndex(max-1); if(data1==0) if(m_stack[x].Isempty()) } bool Hanio::IsFinished() void Hanio::GameStart() int Hanio::Solve(char from,char to,char auxiliary,int n) return 0; //main.cpp #include using namespace std; int StartPicture();//返回选择的盘数 int main() int StartPicture() system("cls"); print('=',80); return number; }
{
int max;
max=m_stack[0].Size()>m_stack[1].Size() ? m_stack[0].Size() : m_stack[1].Size();
max=m_stack[2].Size()>max ? m_stack[2].Size() : max;
//std::cout<<"Max:"<
m_stack[1].SetIndex(max-1);
m_stack[2].SetIndex(max-1);
for(int i=1;i<=max;i++)
{
int data1=m_stack[0].MoveToNext();
int data2=m_stack[1].MoveToNext();
int data3=m_stack[2].MoveToNext();
print(' ',20);
else
{
print(' ',10-data1);
print(ch,2*data1);
print(' ',10-data1);
}
if(data2==0)
print(' ',20);
else
{
print(' ',10-data2);
print(ch,2*data2);
print(' ',10-data2);
}
if(data3==0)
print(' ',20);
else
{
print(' ',10-data3);
print(ch,2*data3);
print(' ',10-data1);
}
std::cout<
}
bool Hanio::MoveFromTo(int x,int y)
{
m_steps++; //计算所走的步数
{
std::cout<
return false;
}
if(m_stack[y].Isempty())
{
int data;
m_stack[x].Pop(&data);
m_stack[y].Push(data);
return true;
}
else
{
if(m_stack[x].TopValue()>m_stack[y].TopValue())
{
std::cout<<"The board can't move from "<
return false;
}
else
{
int data;
m_stack[x].Pop(&data);
m_stack[y].Push(data);
return true;
}
}
{
return m_stack[2].Top()==m_num-1;
}
{
using namespace std;
UINT StartTime=::GetTickCount();
UINT EndTime;
while(1)
{
system("cls");
print('-',80);
cout<<"steps: "<
cout<
cout<<"C"<
cout<
//测试游戏是否结束
if(Hanio::IsFinished())
{
cout<<"你好强呀!从今天开始,维护世界和平的任务就交给你那!"<
break;
}
//输入命令并左相应的处理
char szCommand[50];
cout<<">>";
cin.getline(szCommand,50);
if(stricmp(szCommand,"QUIT")==0 || stricmp(szCommand,"Q")==0)
break;
if(stricmp(szCommand,"HELP")==0 || stricmp(szCommand,"H")==0)
{
cout<<" 本游戏说明 :"<
}
char ch1=toupper(szCommand[0]);
char ch2=toupper(szCommand[1]);
if( ch1=='A' && ch2=='B')
Hanio::MoveFromTo(0,1);
else if ( ch1=='A' && ch2=='C')
MoveFromTo(0,2);
else if ( ch1=='B' && ch2=='A')
MoveFromTo(1,0);
else if ( ch1=='B' && ch2=='C')
MoveFromTo(1,2);
else if ( ch1=='C' && ch2=='A')
MoveFromTo(2,0);
else if ( ch1=='C' && ch2=='B')
MoveFromTo(2,1);
else
{
cout<<"Bad command !"<
}
//统计游戏所用时间
EndTime=GetTickCount();
m_times=(EndTime-StartTime)/1000;
}
}
{
if(n==1)
}
#include"hanio.h"
#include
{
int number;
number=StartPicture();
Hanio hanio(number);
hanio.GameStart();
return 0;
}
void print(char ch,int n)
{
for(int i=1;i<=n;i++)
std::cout<
{
using namespace std;
int number;
system("color fc");
print(' ',20);
print('-',25);
cout<
cout<<" Hanio(汉诺塔)"<
print('-',25);
cout<
print('-',5);
cout<<"By David"<
cout<<" 相传在某一座古庙中有3根木桩,有24个铁盘由小到大放置在一根木柱上,庙中流传者一个传说:/"如果能把24个铁盘, 从一根木桩移动到另一个木桩,且必须遵守如下规则:"<
cout<<">>";
cin>>number;
cin.get();
system("cls");