昨天没事在用别人的文曲星查字!突然看到那个汉诺塔游戏.....
反正这两天刚好手有点痒,于是就有了:
#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 <iostream>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<windows.h>
Stack::Stack()
{
m_top=-1;
m_index=m_top;
for(int i=0;i<MAX;i++)
m_node[i]=0;
}
bool Stack::Push(int data)
{
if(Isfull())
return false;
m_top++;
m_node[m_top]=data;
m_index=m_top;
return true;
}