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

📄 hanoi3.c

📁 用c编写的在dos下运行的汉诺塔小游戏
💻 C
字号:
//file : hanota.c
//designed by: 
//finshed date: 04/12/09

#include <graphics.h>
struct H
{
   int data[15];        //盘子的个数
   int top;                //塔的高度
}num[3];                
void move(char x,char y,struct H num[3]);
void hanoi(char x,char y,char z,int n,struct H num[3]);
void Init(void);
void Close(void);
int computer=1;
int speed=0;                      //显示的速度
void main(void)
{
   Init();
   Close();
   exit(0);
}
void Init(void)
{
   int gd=DETECT,gm;
   int i,n,color;
   clrscr();
   printf("please input the num of discs n(n<=10): ");                   //输入设定盘子的个数
   scanf("%d",&n);
   printf("Please select 1 or 2:\n1.computer 2.people\n");
   scanf("%d",&i);
   if(i==2)
      computer=0;
   if(n<1||n>10)
      n=10;
   if(computer)
   {
      printf("please input speed(delay_ms): ");
      scanf("%d",&speed);
   }
   initgraph(&gd,&gm,"c:\\tc");                    // 图形初始化
   cleardevice();                                            //清屏
   for(i=0;i<3;i++)
      num[i].top=-1;
   for(i=0;i<n;i++)                                 //开始移动盘子时在A座上显示设定的盘子
   {
      num[0].top++;                            
      num[0].data[num[0].top]=i;       //从塔的最底部给盘子编号,依次为1,2,3.....
      color=num[0].data[num[0].top]+1;     //设定盘子的颜色
      setfillstyle(SOLID_FILL,color);
      bar(100-(33-3*num[0].data[num[0].top]),400-20*i-8,100+
                   (33-3*num[0].data[num[0].top]),400-20*i+8);  //draw the disc ,is bar
   }
   setcolor(YELLOW);
   outtextxy(180,450,"press any key to continue");
   settextstyle(0,0,2);
   outtextxy(90,420,"A"); 
   outtextxy(240,420,"B");
   outtextxy(390,420,"C");
   getch();                          
   hanoi('a','b','c',n,num);
}
void move(char x,char y,struct H num[3])                  
  {
   int i;
   char num1[3],num2[3];
   sprintf(num1,"%c",x-32);                     
   sprintf(num2,"%c",y-32);
   setfillstyle(SOLID_FILL,BLACK);          // 如果移动某个盘子时,清楚原位置的图像
   bar(0,0,640,60);
   setcolor(RED);
   outtextxy(150,30,num1);                     
   outtextxy(200,30,"--->");
   outtextxy(310,30,num2);
   settextstyle(0,0,2);
   setfillstyle(SOLID_FILL,BLACK);                           
   bar(100+150*(x-97)-(33-3*num[x-97].data[num[x-97].top]),
400-20*num[x-97].top-8,100+150*(x-97)+(33-3*
num[x-97].data[num[x-97].top]),400-20*num[x-97].top+8);
   num[y-97].top++;                                         
   num[y-97].data[num[y-97].top]=num[x-97].data[num[x-97].top];
   num[x-97].top--;                           // 塔的层数递减 
   setfillstyle(SOLID_FILL,num[y-97].data[num[y-97].top]+1);
   bar(100+150*(y-97)-(33-3*num[y-97].data[num[y-97].top]),
400-20*num[y-97].top-8,100+150*(y-97)+
(33-3*num[y-97].data[num[y-97].top]),400-20*num[y-97].top+8);
   if(computer)                  
      delay(speed); 
   else
      getch();         
}
void hanoi(char one,char two,char three,int n,struct H num[3])      //*递归n为盘子数,num为堆栈*//
{
   if(n==1)
      move(one,three,num);                     
   else
   {
      hanoi(one,three,two,n-1,num);           //从A上移动 n-1个盘子到B上   
      move(one,three,num);                          //把剩下的一个盘子从A移动到B上
      hanoi(two,one,three,n-1,num);        // 再把n-1个盘子从B上移动到C上
   }
}
void Close(void)                          
{
   getch();
   closegraph();
}

⌨️ 快捷键说明

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