⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hanta.c

📁 汉诺塔的源码
💻 C
字号:
#include "stdlib.h"
#include "dos.h"
#define DELAY 1000

int axis_disks[4]={0,0,0,0};
 /***** globle value to stroe curret disks number for each axis */
 /***** axis_disks[1],[2],[3] is available  */
void InitDraw(int,int);
void FrameLine();                /***** three axis line */
void hanoi(int,int,int,int);
void Move(int,int,int);
void DrawDisk(int,int,int);
void EraseDisk(int,int,int);
void SetMode(int);
void Line(int,int,int,int,int);

main()
{
int num,n=1,cur=1,des=2,temp=3;
do{
printf("\nInput number of disks(<9):");
scanf("%d",&n);
if(n<1||n>8)
 printf("\nError!the number of disks must >1 and <8!\nInput again!");
}while(n<1||n>8);
InitDraw(cur,n);
getch();
hanoi(cur,des,temp,n);
printf("Move successfully!");
getch();
SetMode(3);
}

void SetMode(int color)
{
union REGS in,out;
in.h.ah=0;
in.h.al=color;
int86(0x10,&in,&out);
}

void InitDraw(int num,int n)
{
int i=n+1;
SetMode(0x13);

FrameLine();
axis_disks[num]=n;
while(--i)
 DrawDisk(num,n-i,i);
}

void hanoi(int cur,int des,int temp,int n)
{
if(n==1){ Move(cur,des,1);return;}
hanoi(cur,temp,des,n-1);
Move(cur,des,n);
hanoi(temp,des,cur,n-1);
}

void Move(int cur,int des,int n)
{
int count;
EraseDisk(cur,axis_disks[cur]-1,n);
axis_disks[cur]--;
FrameLine();
for(count=0;count<100;++count)
 delay(DELAY);
DrawDisk(des,axis_disks[des],n);
axis_disks[des]++;
FrameLine();
for(count=0;count<100;++count)delay(DELAY*2);
}

void FrameLine()
{
Line(50,10,50,100,1);
Line(150,10,150,100,1);
Line(250,10,250,100,1);
}

void DrawDisk(int cur,int disks,int n)
{
int base_x,base_y,i,j;
base_x=-50+cur*100;
base_y=100-disks*10;
for(i=0;i<10;i++)
 Line(base_x-n*3-20,base_y-i,base_x+n*3+20,base_y-i,n);
}

void EraseDisk(int cur,int disks,int n)
{
int base_x,base_y,i,j;
base_x=-50+cur*100;
base_y=100-disks*10;
for(i=0;i<10;i++)
 Line(base_x-n*3-20,base_y-i,base_x+n*3+20,base_y-i,0);
}

void Line(int x1,int y1,int x2,int y2,int color)
{
unsigned char far *vp=(char far *)0xA0000000;
int i;

if(x1==x2)
 for(i=y1;i<=y2;i++)
 vp[i*320+x1]=color;
if(y1==y2)
 for(i=x1;i<=x2;i++)
 vp[y1*320+i]=color;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -