递归算法 汉诺塔 演示
目的:图形处理使用,分支\循环\数组\函数等的综合运用:程序功能:应用递归算法演示汉诺塔问题的求解过程.程序设计盘子规模可选1到10,并且可以人工控制演示和自动演示,如果...
目的:图形处理使用,分支\循环\数组\函数等的综合运用:
程序功能:应用递归算法演示汉诺塔问题的求解过程.程序设计盘子规模可选1到10,并且可以人工控制演示和自动演示,如果为自动演示,还需要输入演示速度,以毫秒为单位.
如果有好答案,将追加悬赏!
谢谢了! 展开
程序功能:应用递归算法演示汉诺塔问题的求解过程.程序设计盘子规模可选1到10,并且可以人工控制演示和自动演示,如果为自动演示,还需要输入演示速度,以毫秒为单位.
如果有好答案,将追加悬赏!
谢谢了! 展开
5个回答
展开全部
TurboC 1024×768 真彩色,演示全木质汉诺塔
ESC 退出, 空格切换 自动手动
tc 2.0, 3.0 均可运行
#include <dos.h>
#include <fcntl.h>
#include <io.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MINDISK 1
#define MAXDISK 15
#define DISKHEIGHT 30
#define TEXT_BIG_CURSOR 0
#define TEXT_NORMAL_CURSOR 7
typedef struct /*16Mrgb像素类型*/
{
unsigned char b;
unsigned char g;
unsigned char r;
}rgb16M;
typedef struct tagDisks
{
int x;
int y;
int Left;
int Top;
int Lenth;
int Height;
int Tag;
int Cylinder1;
int Cylinder2;
int Cylinder3;
}Disks;
void DrawPillar(int number, int bottom);
void DrawDisk(int x, int y, int lenth, int height);
void HanoiMove(int n, int X, int Y, int Z);
void MoveDisk(int n, int DiskFrom, int DiskTo);
void SetXY(Disks *disk, int nX, int nY, int cleardisk);
int disk_num;
int automove = 1;
int toquit = 0;
int delayperiod;
int CylinderHeight[3];
Disks disks[MAXDISK + 1];
rgb16M backcolor = {140, 197, 227}, backcolor1 = {34, 95, 149};
rgb16M woodcolor[2][4] = {{{180, 223, 240},{138, 205, 230},{98, 188, 222},{58, 171, 214}},{{2, 96,132},{4,110,151},{3, 121, 166},{4, 135, 183}}};
rgb16M bordercolor = {52, 128, 203};
long woodfillcolor[35][30] = {
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1}};
unsigned char set_SVGA_mode(int vmode)
{union REGS r;
r.x.ax=0x4f02;
r.x.bx=vmode;
int86(0x10,&r,&r);
return(r.h.ah);
}
void hide_text_cursor(void)
{union REGS r;
r.h.ah=1;
r.h.ch=32;
int86(0x10,&r,&r);
}
void selectpage(register char page)
{union REGS r;
r.x.ax=0x4f05;
r.x.bx=0;
r.x.dx=page;
int86(0x10,&r,&r);
}
void show_text_cursor(char size)
{union REGS r;
r.h.ah=1;
r.h.cl=size;
r.h.ch=7;
int86(0x10,&r,&r);
}
unsigned int get_SVGA_mode()
{union REGS r;
r.x.ax=0x4f03;
int86(0x10,&r,&r);
return(r.x.bx);
}
unsigned int Vmode=0;
void init16M()
{Vmode=get_SVGA_mode();
hide_text_cursor();
if(set_SVGA_mode(0x118))
{printf("\nSorry, Your graphics driver does not supplied.\n");
show_text_cursor(TEXT_NORMAL_CURSOR);
exit(0);
}
}
void close16M()
{
union REGS r;
r.h.ah = 0;
r.h.al = 3;
int86(0x10, &r, &r);
r.h.ah = 0x05;
r.h.al = 0;
int86(0x10, &r, &r);
}
void putpoint16M(int x, int y, rgb16M color)
{
static int nowpage = 0, lastpage = 50;
static long temp;
unsigned char far *p = (unsigned char *)0xa0000000;
temp = y;
x <<= 2;
temp <<= 12;
temp += x;
nowpage = temp >> 16;
temp &= 0xffff;
if (nowpage != lastpage)
{
static union REGS r;
r.x.ax = 0x4f05;
r.x.bx = 0;
r.x.dx = nowpage;
int86(0x10, &r, &r);
lastpage = nowpage;
}
*((rgb16M *)(p + temp)) = color;
}
void line(int startx, int starty, int endx, int endy, rgb16M color)
{
register int t, distance;
int x = 0, y = 0;
int delta_x, delta_y;
int incx, incy;
delta_x = endx-startx;
delta_y = endy-starty;
if (delta_x > 0) incx = 1;
else if(delta_x == 0) incx = 0;
else incx = -1;
if(delta_y > 0) incy = 1;
else if (delta_y == 0) incy = 0;
else incy = -1;
if (delta_x < 0) delta_x = -delta_x;
if (delta_y < 0) delta_y = -delta_y;
if(delta_x > delta_y) distance = delta_x;
else distance = delta_y;
for (t = 0; t <= distance + 1; t++){
putpoint16M(startx, starty, color);
x += delta_x;
y += delta_y;
if (x > distance){
x -= distance;
startx += incx;
}
if(y > distance){
y -= distance;
starty += incy;
}
}
}
void fillbox(int startx, int starty, int endx, int endy, rgb16M color)
{
int i, j;
if (startx > endx)
{
i = startx;
startx = endx;
endx = i;
}
if (starty > endy)
{
i = starty;
starty = endy;
endy = i;
}
if (startx < 0)
startx = 0;
if (endx > 1023)
endx = 1023;
if (starty < 0)
starty = 0;
else if (endy > 767)
endy = 767;
for (j = starty; j <= endy; j++)
for (i = startx; i <= endx; i++)
putpoint16M(i, j, color);
}
int main(void)
{
int i, j;
int left, top;
int width = 975;
int height = 600;
reinnum:;
printf("input the number of disk(%d-%d, 0 to quit): ", MINDISK, MAXDISK);
delayperiod = 500;
scanf("%d", &disk_num);
if (disk_num == 0)
return 0;
if (disk_num < MINDISK || disk_num > MAXDISK)
goto reinnum;
printf("select play style(0:auto, 1:manual): ");
scanf("%d", &automove);
if (automove != 0)
automove = 1;
if (automove == 0)
{
reintime:;
printf("input the delay period(100-5000): ");
scanf("%d", &delayperiod);
if (delayperiod < 100 || delayperiod > 5000)
goto reintime;
}
init16M();
left = (1024 - width) >> 1;
top = (768 - height) >> 1;;
fillbox(left, top, left + width, top + height, backcolor1);
fillbox(left - 10 , top - 12, left - 2, top + height + 13, bordercolor);
fillbox(left + width + 2, top - 12, left + width + 10, top + height + 13, bordercolor);
fillbox(left, top - 12, left + width, top - 2, bordercolor);
fillbox(left, top + height + 2, left + width, top + height + 13, bordercolor);
for (i = 0; i < 3; i++)
CylinderHeight[i] = 0;
disks[1].Cylinder1 = left + width / 6;
disks[1].Cylinder2 = left + width / 2;
disks[1].Cylinder3 = left + width / 6 * 5;
DrawPillar(1, 0);
DrawPillar(2, 0);
DrawPillar(3, 0);
DrawDisk(disks[1].Cylinder1 - ((width / 4) + 7) / 2 - 20, 673, (width / 4) + 47, 12);
DrawDisk(disks[1].Cylinder2 - ((width / 4) + 7) / 2 - 20, 673, (width / 4) + 47, 12);
DrawDisk(disks[1].Cylinder3 - ((width / 4) + 7) / 2 - 20, 673, (width / 4) + 47, 12);
for (i = disk_num; i >= 1; i--)
{
disks[i].x = 1;
disks[i].y = i;
disks[i].Cylinder1 = left + width / 6;
disks[i].Cylinder2 = left + width / 2;
disks[i].Cylinder3 = left + width / 6 * 5;
disks[i].Height = DISKHEIGHT;
disks[i].Tag = i;
disks[i].Lenth = (width / 4 / disk_num * i) + 7;
SetXY(&disks[i], 1, CylinderHeight[0], 0);
CylinderHeight[0]++;
}
getch();
HanoiMove(disk_num, 1, 2, 3);
getch();
close16M();
return 0;
}
void DrawPillar(int number, int bottom)
{
int x, y = 110;
int lenth = 8, height;
int i, j, m, n;
if (number == 1)
x = disks[1].Cylinder1 - 4;
else if (number == 2)
x = disks[1].Cylinder2 - 4;
else
x = disks[1].Cylinder3 - 4;
if (bottom == 0)
height = 575;
else
height = bottom;
m = 0;
n = 0;
for (i = y + 4; i <= y + height - 1 && i < bottom; i++)
{
for (j = x + 4; j <= x + lenth - 5; j++)
{
woodcolor[0][3].r = (unsigned char)woodfillcolor[n][m];
woodcolor[0][3].g = (unsigned char)(((int)woodfillcolor[n][m])>>8);
woodcolor[0][3].b = (unsigned char)(woodfillcolor[n][m]>>16);
putpoint16M(j, i, woodcolor[0][3]);
m++;
if (m > 29)
m = 0;
}
m = 0;
n++;
if (n > 34)
n = 0;
}
woodcolor[0][3] = backcolor;
for (i = 0 ; i < 4; i++)
{
line(x + i, y + i, x + i, y + height - 1, woodcolor[0][i]);
line(x + i + 1, y + i, x + lenth - i - 1, y + i, woodcolor[0][i]);
line(x + lenth - i - 1, y + i, x + lenth - i - 1, y + height - 1, woodcolor[1][i]);
}
}
void DrawDisk(int x, int y, int lenth, int height)
{
int i, j, m, n;
woodcolor[0][3] = backcolor;
for (i = 0 ; i < 4; i++)
{
line(x + i, y + i, x + i, y + height - i - 1, woodcolor[0][i]);
line(x + i + 1, y + i, x + lenth - i - 1, y + i, woodcolor[0][i]);
line(x + i + 1, y + height + i - 4, x + lenth - 1, y + height + i - 4, woodcolor[1][3 - i]);
line(x + lenth - i - 1, y + i, x + lenth - i - 1, y + height - 4, woodcolor[1][i]);
}
m = 0;
n = 0;
for (i = y + 4; i <= y + height - 5; i++)
{
for (j = x + 4; j <= x + lenth - 5; j++)
{
woodcolor[0][3].r = (unsigned char)woodfillcolor[n][m];
woodcolor[0][3].g = (unsigned char)(((int)woodfillcolor[n][m])>>8);
woodcolor[0][3].b = (unsigned char)(woodfillcolor[n][m]>>16);
putpoint16M(j, i, woodcolor[0][3]);
m++;
if (m > 29)
m = 0;
}
m = 0;
n++;
if (n > 34)
n = 0;
}
}
void MoveDisk(int n, int DiskFrom, int DiskTo)
{
if (toquit == 1)
return;
SetXY(&disks[n], DiskTo, CylinderHeight[DiskTo - 1], 1);
CylinderHeight[DiskFrom - 1]--;
CylinderHeight[DiskTo - 1]++;
}
void SetXY(Disks *disk, int nX, int nY, int cleardisk)
{
if (cleardisk == 1)
{
fillbox(disk->Left, disk->Top, disk->Left + disk->Lenth - 1, disk->Top + disk->Height - 1, backcolor1);
DrawPillar(disk->x, DISKHEIGHT - 35 + 599 - disk->Height * CylinderHeight[disk->x - 1] - 1);
}
disk->x = nX;
disk->y = nY;
switch (nX)
{
case 1 :
disk->Left = disk->Cylinder1 - disk->Lenth / 2;
break;
case 2 :
disk->Left = disk->Cylinder2 - disk->Lenth / 2;
break;
case 3 :
disk->Left = disk->Cylinder3 - disk->Lenth / 2;
break;
default :
;
}
disk->Top = 35 - DISKHEIGHT + 638 - disk->Height * disk->y;
DrawDisk(disk->Left, disk->Top, disk->Lenth, disk->Height);
}
void HanoiMove(int n, int X, int Y, int Z)
{
int a;
if (n == 0 || toquit == 1)
return;
HanoiMove(n - 1, X, Z, Y);
MoveDisk(n, X, Z);
if (toquit == 1)
return;
if (automove == 1)
{
a = getch();
if (a == 0)
{
a = getch();
}
else if (a == 27)
{
toquit = 1;
return;
}
else if (a == 32)
{
automove = 1 - automove;
}
}
else
{
if (kbhit())
{
a = getch();
if (a == 27)
{
toquit = 1;
return;
}
else if (a == 32)
{
automove = 1 - automove;
}
}
delay(delayperiod);
}
HanoiMove(n - 1, Y, X, Z);
}
ESC 退出, 空格切换 自动手动
tc 2.0, 3.0 均可运行
#include <dos.h>
#include <fcntl.h>
#include <io.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MINDISK 1
#define MAXDISK 15
#define DISKHEIGHT 30
#define TEXT_BIG_CURSOR 0
#define TEXT_NORMAL_CURSOR 7
typedef struct /*16Mrgb像素类型*/
{
unsigned char b;
unsigned char g;
unsigned char r;
}rgb16M;
typedef struct tagDisks
{
int x;
int y;
int Left;
int Top;
int Lenth;
int Height;
int Tag;
int Cylinder1;
int Cylinder2;
int Cylinder3;
}Disks;
void DrawPillar(int number, int bottom);
void DrawDisk(int x, int y, int lenth, int height);
void HanoiMove(int n, int X, int Y, int Z);
void MoveDisk(int n, int DiskFrom, int DiskTo);
void SetXY(Disks *disk, int nX, int nY, int cleardisk);
int disk_num;
int automove = 1;
int toquit = 0;
int delayperiod;
int CylinderHeight[3];
Disks disks[MAXDISK + 1];
rgb16M backcolor = {140, 197, 227}, backcolor1 = {34, 95, 149};
rgb16M woodcolor[2][4] = {{{180, 223, 240},{138, 205, 230},{98, 188, 222},{58, 171, 214}},{{2, 96,132},{4,110,151},{3, 121, 166},{4, 135, 183}}};
rgb16M bordercolor = {52, 128, 203};
long woodfillcolor[35][30] = {
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1},
{0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x53B2F1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1,0x62B3F1,0x43A3E3,0x43ABF1,0x43ABF1,0x53B2F1,0x62B3F1,0x43A3E3,0x43ABF1}};
unsigned char set_SVGA_mode(int vmode)
{union REGS r;
r.x.ax=0x4f02;
r.x.bx=vmode;
int86(0x10,&r,&r);
return(r.h.ah);
}
void hide_text_cursor(void)
{union REGS r;
r.h.ah=1;
r.h.ch=32;
int86(0x10,&r,&r);
}
void selectpage(register char page)
{union REGS r;
r.x.ax=0x4f05;
r.x.bx=0;
r.x.dx=page;
int86(0x10,&r,&r);
}
void show_text_cursor(char size)
{union REGS r;
r.h.ah=1;
r.h.cl=size;
r.h.ch=7;
int86(0x10,&r,&r);
}
unsigned int get_SVGA_mode()
{union REGS r;
r.x.ax=0x4f03;
int86(0x10,&r,&r);
return(r.x.bx);
}
unsigned int Vmode=0;
void init16M()
{Vmode=get_SVGA_mode();
hide_text_cursor();
if(set_SVGA_mode(0x118))
{printf("\nSorry, Your graphics driver does not supplied.\n");
show_text_cursor(TEXT_NORMAL_CURSOR);
exit(0);
}
}
void close16M()
{
union REGS r;
r.h.ah = 0;
r.h.al = 3;
int86(0x10, &r, &r);
r.h.ah = 0x05;
r.h.al = 0;
int86(0x10, &r, &r);
}
void putpoint16M(int x, int y, rgb16M color)
{
static int nowpage = 0, lastpage = 50;
static long temp;
unsigned char far *p = (unsigned char *)0xa0000000;
temp = y;
x <<= 2;
temp <<= 12;
temp += x;
nowpage = temp >> 16;
temp &= 0xffff;
if (nowpage != lastpage)
{
static union REGS r;
r.x.ax = 0x4f05;
r.x.bx = 0;
r.x.dx = nowpage;
int86(0x10, &r, &r);
lastpage = nowpage;
}
*((rgb16M *)(p + temp)) = color;
}
void line(int startx, int starty, int endx, int endy, rgb16M color)
{
register int t, distance;
int x = 0, y = 0;
int delta_x, delta_y;
int incx, incy;
delta_x = endx-startx;
delta_y = endy-starty;
if (delta_x > 0) incx = 1;
else if(delta_x == 0) incx = 0;
else incx = -1;
if(delta_y > 0) incy = 1;
else if (delta_y == 0) incy = 0;
else incy = -1;
if (delta_x < 0) delta_x = -delta_x;
if (delta_y < 0) delta_y = -delta_y;
if(delta_x > delta_y) distance = delta_x;
else distance = delta_y;
for (t = 0; t <= distance + 1; t++){
putpoint16M(startx, starty, color);
x += delta_x;
y += delta_y;
if (x > distance){
x -= distance;
startx += incx;
}
if(y > distance){
y -= distance;
starty += incy;
}
}
}
void fillbox(int startx, int starty, int endx, int endy, rgb16M color)
{
int i, j;
if (startx > endx)
{
i = startx;
startx = endx;
endx = i;
}
if (starty > endy)
{
i = starty;
starty = endy;
endy = i;
}
if (startx < 0)
startx = 0;
if (endx > 1023)
endx = 1023;
if (starty < 0)
starty = 0;
else if (endy > 767)
endy = 767;
for (j = starty; j <= endy; j++)
for (i = startx; i <= endx; i++)
putpoint16M(i, j, color);
}
int main(void)
{
int i, j;
int left, top;
int width = 975;
int height = 600;
reinnum:;
printf("input the number of disk(%d-%d, 0 to quit): ", MINDISK, MAXDISK);
delayperiod = 500;
scanf("%d", &disk_num);
if (disk_num == 0)
return 0;
if (disk_num < MINDISK || disk_num > MAXDISK)
goto reinnum;
printf("select play style(0:auto, 1:manual): ");
scanf("%d", &automove);
if (automove != 0)
automove = 1;
if (automove == 0)
{
reintime:;
printf("input the delay period(100-5000): ");
scanf("%d", &delayperiod);
if (delayperiod < 100 || delayperiod > 5000)
goto reintime;
}
init16M();
left = (1024 - width) >> 1;
top = (768 - height) >> 1;;
fillbox(left, top, left + width, top + height, backcolor1);
fillbox(left - 10 , top - 12, left - 2, top + height + 13, bordercolor);
fillbox(left + width + 2, top - 12, left + width + 10, top + height + 13, bordercolor);
fillbox(left, top - 12, left + width, top - 2, bordercolor);
fillbox(left, top + height + 2, left + width, top + height + 13, bordercolor);
for (i = 0; i < 3; i++)
CylinderHeight[i] = 0;
disks[1].Cylinder1 = left + width / 6;
disks[1].Cylinder2 = left + width / 2;
disks[1].Cylinder3 = left + width / 6 * 5;
DrawPillar(1, 0);
DrawPillar(2, 0);
DrawPillar(3, 0);
DrawDisk(disks[1].Cylinder1 - ((width / 4) + 7) / 2 - 20, 673, (width / 4) + 47, 12);
DrawDisk(disks[1].Cylinder2 - ((width / 4) + 7) / 2 - 20, 673, (width / 4) + 47, 12);
DrawDisk(disks[1].Cylinder3 - ((width / 4) + 7) / 2 - 20, 673, (width / 4) + 47, 12);
for (i = disk_num; i >= 1; i--)
{
disks[i].x = 1;
disks[i].y = i;
disks[i].Cylinder1 = left + width / 6;
disks[i].Cylinder2 = left + width / 2;
disks[i].Cylinder3 = left + width / 6 * 5;
disks[i].Height = DISKHEIGHT;
disks[i].Tag = i;
disks[i].Lenth = (width / 4 / disk_num * i) + 7;
SetXY(&disks[i], 1, CylinderHeight[0], 0);
CylinderHeight[0]++;
}
getch();
HanoiMove(disk_num, 1, 2, 3);
getch();
close16M();
return 0;
}
void DrawPillar(int number, int bottom)
{
int x, y = 110;
int lenth = 8, height;
int i, j, m, n;
if (number == 1)
x = disks[1].Cylinder1 - 4;
else if (number == 2)
x = disks[1].Cylinder2 - 4;
else
x = disks[1].Cylinder3 - 4;
if (bottom == 0)
height = 575;
else
height = bottom;
m = 0;
n = 0;
for (i = y + 4; i <= y + height - 1 && i < bottom; i++)
{
for (j = x + 4; j <= x + lenth - 5; j++)
{
woodcolor[0][3].r = (unsigned char)woodfillcolor[n][m];
woodcolor[0][3].g = (unsigned char)(((int)woodfillcolor[n][m])>>8);
woodcolor[0][3].b = (unsigned char)(woodfillcolor[n][m]>>16);
putpoint16M(j, i, woodcolor[0][3]);
m++;
if (m > 29)
m = 0;
}
m = 0;
n++;
if (n > 34)
n = 0;
}
woodcolor[0][3] = backcolor;
for (i = 0 ; i < 4; i++)
{
line(x + i, y + i, x + i, y + height - 1, woodcolor[0][i]);
line(x + i + 1, y + i, x + lenth - i - 1, y + i, woodcolor[0][i]);
line(x + lenth - i - 1, y + i, x + lenth - i - 1, y + height - 1, woodcolor[1][i]);
}
}
void DrawDisk(int x, int y, int lenth, int height)
{
int i, j, m, n;
woodcolor[0][3] = backcolor;
for (i = 0 ; i < 4; i++)
{
line(x + i, y + i, x + i, y + height - i - 1, woodcolor[0][i]);
line(x + i + 1, y + i, x + lenth - i - 1, y + i, woodcolor[0][i]);
line(x + i + 1, y + height + i - 4, x + lenth - 1, y + height + i - 4, woodcolor[1][3 - i]);
line(x + lenth - i - 1, y + i, x + lenth - i - 1, y + height - 4, woodcolor[1][i]);
}
m = 0;
n = 0;
for (i = y + 4; i <= y + height - 5; i++)
{
for (j = x + 4; j <= x + lenth - 5; j++)
{
woodcolor[0][3].r = (unsigned char)woodfillcolor[n][m];
woodcolor[0][3].g = (unsigned char)(((int)woodfillcolor[n][m])>>8);
woodcolor[0][3].b = (unsigned char)(woodfillcolor[n][m]>>16);
putpoint16M(j, i, woodcolor[0][3]);
m++;
if (m > 29)
m = 0;
}
m = 0;
n++;
if (n > 34)
n = 0;
}
}
void MoveDisk(int n, int DiskFrom, int DiskTo)
{
if (toquit == 1)
return;
SetXY(&disks[n], DiskTo, CylinderHeight[DiskTo - 1], 1);
CylinderHeight[DiskFrom - 1]--;
CylinderHeight[DiskTo - 1]++;
}
void SetXY(Disks *disk, int nX, int nY, int cleardisk)
{
if (cleardisk == 1)
{
fillbox(disk->Left, disk->Top, disk->Left + disk->Lenth - 1, disk->Top + disk->Height - 1, backcolor1);
DrawPillar(disk->x, DISKHEIGHT - 35 + 599 - disk->Height * CylinderHeight[disk->x - 1] - 1);
}
disk->x = nX;
disk->y = nY;
switch (nX)
{
case 1 :
disk->Left = disk->Cylinder1 - disk->Lenth / 2;
break;
case 2 :
disk->Left = disk->Cylinder2 - disk->Lenth / 2;
break;
case 3 :
disk->Left = disk->Cylinder3 - disk->Lenth / 2;
break;
default :
;
}
disk->Top = 35 - DISKHEIGHT + 638 - disk->Height * disk->y;
DrawDisk(disk->Left, disk->Top, disk->Lenth, disk->Height);
}
void HanoiMove(int n, int X, int Y, int Z)
{
int a;
if (n == 0 || toquit == 1)
return;
HanoiMove(n - 1, X, Z, Y);
MoveDisk(n, X, Z);
if (toquit == 1)
return;
if (automove == 1)
{
a = getch();
if (a == 0)
{
a = getch();
}
else if (a == 27)
{
toquit = 1;
return;
}
else if (a == 32)
{
automove = 1 - automove;
}
}
else
{
if (kbhit())
{
a = getch();
if (a == 27)
{
toquit = 1;
return;
}
else if (a == 32)
{
automove = 1 - automove;
}
}
delay(delayperiod);
}
HanoiMove(n - 1, Y, X, Z);
}
展开全部
#include <GL/glaux.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
float WIDTH = 400;
float HEIGHT = 400;
GLboolean motion = GL_TRUE;
GLboolean back_wall = GL_FALSE;
GLint xangle = 0, yangle = 0;
GLint xlangle = 0, ylangle = 0;
#define other(i,j) (6-(i+j))
#define wallz -(WIDTH/2)
#define DISK_HEIGHT 20
int NUM_DISKS = 11;
#define CONE NUM_DISKS+1
#define WALL CONE + 1
#define HANOI_SOLVE 0
#define HANOI_QUIT 1
#define HANOI_LIGHTING 2
#define HANOI_WALL 3
#define HANOI_FOG 4
GLfloat lightOneDirection[] = {0.0f, 0.0f, -1.0f};
GLfloat lightOnePosition[] = {200.0f, 100.0f, 300.0f, 1.0f};
GLfloat lightOneColor[] = {1.0f, 1.0f, 0.5f, 1.0f};
GLfloat lightTwoDirection[] = {0.0f, 0.0f, -1.0f};
GLfloat lightTwoPosition[] = {600.0f, 100.0f, 300.0f, 1.0f};
GLfloat lightTwoColor[] = {1.0f, 0.0f, 0.3f, 1.0f};
GLfloat lightZeroPosition[] = {400.0f, 200.0f, 300.0f, 1.0f};
GLfloat lightZeroColor[] = {0.6f, 0.6f, 0.0f, 0.0f};
GLfloat diskColor[] = {1.0f, 1.0f, 1.0f, 0.8f},
poleColor[] = {1.0f, 0.2f, 0.2f, 0.8f};
typedef struct stack_node {
int size;
struct stack_node *next;
} stack_node;
typedef struct stack {
struct stack_node *head;
int depth;
} stack;
stack poles[4];
void push(int which, int size)
{
stack_node *new_node = malloc(sizeof(stack_node));
if (!new_node) {
printf("out of memory!\n");
exit(-1);
}
new_node->size = size;
new_node->next = poles[which].head;
poles[which].head = new_node;
poles[which].depth++;
}
int pop(int which)
{
int retval = poles[which].head->size;
stack_node *temp = poles[which].head;
poles[which].head = poles[which].head->next;
poles[which].depth--;
free(temp);
return retval;
}
typedef struct move_node {
int t, f;
struct move_node *next;
struct move_node *prev;
} move_node;
typedef struct move_stack {
int depth;
struct move_node *head, *tail;
} move_stack;
move_stack moves;
void GenLists(void)
{
int i;
for (i = 0; i < 4; i++) {
poles[i].head = NULL;
poles[i].depth = 0;
}
moves.head = NULL;
moves.tail = NULL;
moves.depth = 0;
for (i = 1; i <= NUM_DISKS; i++) {
glNewList(i, GL_COMPILE);
{
auxSolidTorus(DISK_HEIGHT / 2.0f, 4 * i);
}
glEndList();
}
glNewList(CONE, GL_COMPILE);
{
auxSolidCone(8.0f, (NUM_DISKS + 1) * DISK_HEIGHT);
}
glEndList();
}
void Init(void)
{
glViewport(0, 0, (int)WIDTH, (int)HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, 0, HEIGHT, -10000, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(1.0, 1.0, 1.0, 1.0);
glClearDepth(1.0);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT1, GL_POSITION, lightOnePosition);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightOneColor);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 10);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, lightOneDirection);
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT2, GL_POSITION, lightTwoPosition);
glLightfv(GL_LIGHT2, GL_DIFFUSE, lightTwoColor);
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 10);
glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, lightTwoDirection);
glEnable(GL_LIGHT2);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
}
void mpop(void)
{
move_node *temp = moves.head;
moves.head = moves.head->next;
free(temp);
moves.depth--;
}
void move(int t, int f)
{
move_node *new = malloc(sizeof(move_node));
if (!new) {
fprintf(stderr, "Out of memory!\n");
exit(-1);
}
new->t = t;
new->f = f;
new->next = NULL;
new->prev = moves.tail;
if (moves.tail)
moves.tail->next = new;
moves.tail = new;
if (!moves.head)
moves.head = moves.tail;
moves.depth++;
}
void DrawPost(int xcenter)
{
glPushMatrix();
{
glTranslatef((float)xcenter, 0.0f, 0.0f);
glRotatef(90.0f, -1.0f, 0.0f, 0.0f);
glCallList(CONE);
} glPopMatrix();
}
void DrawPosts(void)
{
glColor3fv(poleColor);
glLineWidth(1.0f);
glMaterialfv(GL_FRONT, GL_DIFFUSE, poleColor);
DrawPost((int)WIDTH / 4);
DrawPost(2 * (int)WIDTH / 4);
DrawPost(3 * (int)WIDTH / 4);
}
void DrawDisk(int xcenter, int ycenter, int size)
{
glPushMatrix();
{
glTranslatef((float)xcenter, (float)ycenter, 0.0f);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
glCallList(size);
} glPopMatrix();
}
void DrawDooDads(void)
{
int i;
stack_node *temp;
int xcenter, ycenter;
glColor3fv(diskColor);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diskColor);
for (i = 1; i <= 3; i++) {
xcenter = i * (int)WIDTH / 4;
ycenter = DISK_HEIGHT * poles[i].depth - DISK_HEIGHT / 2;
for (temp = poles[i].head; temp; temp = temp->next) {
ycenter -= DISK_HEIGHT;
DrawDisk(xcenter, ycenter, temp->size);
}
}
}
static void hanoi(int n, int f, int t)
{
int o;
if (n == 1) {
move(t,f);
return;
}
o = other(f, t);
hanoi(n - 1, f, o);
hanoi(1, f, t);
hanoi(n - 1, o, t);
}
GLfloat wallcolor[] = {0.0f, 0.3f, 1.0f, 1.0f};
void CALLBACK draw(void)
{
int t, f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
{
glTranslatef(WIDTH / 2.0f, HEIGHT / 2.0f, 0.0f);
glRotatef((float)xlangle, 0.0f, 1.0f, 0.0f);
glRotatef((float)ylangle, 1.0f, 0.0f, 0.0f);
glTranslatef(-WIDTH / 2.0f, -HEIGHT / 2.0f, 0.0f);
glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);
}
glPopMatrix();
glPushMatrix();
{
glTranslatef(WIDTH / 2.0f, HEIGHT / 2.0f, 0.0f);
glRotatef((float)xangle, 0.0f, 1.0f, 0.0f);
glRotatef((float)yangle, 1.0f, 0.0f, 0.0f);
glTranslatef(-WIDTH / 2.0f, -HEIGHT / 2.0f, 0.0f);
DrawPosts();
DrawDooDads();
}
glPopMatrix();
if (motion && moves.depth) {
t = moves.head->t;
f = moves.head->f;
push(t, pop(f));
mpop();
}
auxSwapBuffers();
}
void CALLBACK update(void)
{
draw();
}
void CALLBACK reshape(int w, int h)
{
glViewport(0,0,w,h);
}
int oldx, oldy;
GLboolean leftb = GL_FALSE, middleb = GL_FALSE;
void main(int argc, char *argv[])
{
int i;
auxInitPosition(0, 0, (int)WIDTH, (int)HEIGHT);
auxInitDisplayMode(AUX_RGBA | AUX_DOUBLE | AUX_DEPTH);
auxInitWindow("Hanoi");
GenLists();
Init();
for (i = 0; i < NUM_DISKS; i++)
push(1, NUM_DISKS - i);
hanoi(NUM_DISKS, 1, 3);
auxReshapeFunc(reshape);
auxIdleFunc(update);
auxMainLoop(draw);
}
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
float WIDTH = 400;
float HEIGHT = 400;
GLboolean motion = GL_TRUE;
GLboolean back_wall = GL_FALSE;
GLint xangle = 0, yangle = 0;
GLint xlangle = 0, ylangle = 0;
#define other(i,j) (6-(i+j))
#define wallz -(WIDTH/2)
#define DISK_HEIGHT 20
int NUM_DISKS = 11;
#define CONE NUM_DISKS+1
#define WALL CONE + 1
#define HANOI_SOLVE 0
#define HANOI_QUIT 1
#define HANOI_LIGHTING 2
#define HANOI_WALL 3
#define HANOI_FOG 4
GLfloat lightOneDirection[] = {0.0f, 0.0f, -1.0f};
GLfloat lightOnePosition[] = {200.0f, 100.0f, 300.0f, 1.0f};
GLfloat lightOneColor[] = {1.0f, 1.0f, 0.5f, 1.0f};
GLfloat lightTwoDirection[] = {0.0f, 0.0f, -1.0f};
GLfloat lightTwoPosition[] = {600.0f, 100.0f, 300.0f, 1.0f};
GLfloat lightTwoColor[] = {1.0f, 0.0f, 0.3f, 1.0f};
GLfloat lightZeroPosition[] = {400.0f, 200.0f, 300.0f, 1.0f};
GLfloat lightZeroColor[] = {0.6f, 0.6f, 0.0f, 0.0f};
GLfloat diskColor[] = {1.0f, 1.0f, 1.0f, 0.8f},
poleColor[] = {1.0f, 0.2f, 0.2f, 0.8f};
typedef struct stack_node {
int size;
struct stack_node *next;
} stack_node;
typedef struct stack {
struct stack_node *head;
int depth;
} stack;
stack poles[4];
void push(int which, int size)
{
stack_node *new_node = malloc(sizeof(stack_node));
if (!new_node) {
printf("out of memory!\n");
exit(-1);
}
new_node->size = size;
new_node->next = poles[which].head;
poles[which].head = new_node;
poles[which].depth++;
}
int pop(int which)
{
int retval = poles[which].head->size;
stack_node *temp = poles[which].head;
poles[which].head = poles[which].head->next;
poles[which].depth--;
free(temp);
return retval;
}
typedef struct move_node {
int t, f;
struct move_node *next;
struct move_node *prev;
} move_node;
typedef struct move_stack {
int depth;
struct move_node *head, *tail;
} move_stack;
move_stack moves;
void GenLists(void)
{
int i;
for (i = 0; i < 4; i++) {
poles[i].head = NULL;
poles[i].depth = 0;
}
moves.head = NULL;
moves.tail = NULL;
moves.depth = 0;
for (i = 1; i <= NUM_DISKS; i++) {
glNewList(i, GL_COMPILE);
{
auxSolidTorus(DISK_HEIGHT / 2.0f, 4 * i);
}
glEndList();
}
glNewList(CONE, GL_COMPILE);
{
auxSolidCone(8.0f, (NUM_DISKS + 1) * DISK_HEIGHT);
}
glEndList();
}
void Init(void)
{
glViewport(0, 0, (int)WIDTH, (int)HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, WIDTH, 0, HEIGHT, -10000, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(1.0, 1.0, 1.0, 1.0);
glClearDepth(1.0);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glLightfv(GL_LIGHT1, GL_POSITION, lightOnePosition);
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightOneColor);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 10);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, lightOneDirection);
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT2, GL_POSITION, lightTwoPosition);
glLightfv(GL_LIGHT2, GL_DIFFUSE, lightTwoColor);
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 10);
glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, lightTwoDirection);
glEnable(GL_LIGHT2);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
}
void mpop(void)
{
move_node *temp = moves.head;
moves.head = moves.head->next;
free(temp);
moves.depth--;
}
void move(int t, int f)
{
move_node *new = malloc(sizeof(move_node));
if (!new) {
fprintf(stderr, "Out of memory!\n");
exit(-1);
}
new->t = t;
new->f = f;
new->next = NULL;
new->prev = moves.tail;
if (moves.tail)
moves.tail->next = new;
moves.tail = new;
if (!moves.head)
moves.head = moves.tail;
moves.depth++;
}
void DrawPost(int xcenter)
{
glPushMatrix();
{
glTranslatef((float)xcenter, 0.0f, 0.0f);
glRotatef(90.0f, -1.0f, 0.0f, 0.0f);
glCallList(CONE);
} glPopMatrix();
}
void DrawPosts(void)
{
glColor3fv(poleColor);
glLineWidth(1.0f);
glMaterialfv(GL_FRONT, GL_DIFFUSE, poleColor);
DrawPost((int)WIDTH / 4);
DrawPost(2 * (int)WIDTH / 4);
DrawPost(3 * (int)WIDTH / 4);
}
void DrawDisk(int xcenter, int ycenter, int size)
{
glPushMatrix();
{
glTranslatef((float)xcenter, (float)ycenter, 0.0f);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
glCallList(size);
} glPopMatrix();
}
void DrawDooDads(void)
{
int i;
stack_node *temp;
int xcenter, ycenter;
glColor3fv(diskColor);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diskColor);
for (i = 1; i <= 3; i++) {
xcenter = i * (int)WIDTH / 4;
ycenter = DISK_HEIGHT * poles[i].depth - DISK_HEIGHT / 2;
for (temp = poles[i].head; temp; temp = temp->next) {
ycenter -= DISK_HEIGHT;
DrawDisk(xcenter, ycenter, temp->size);
}
}
}
static void hanoi(int n, int f, int t)
{
int o;
if (n == 1) {
move(t,f);
return;
}
o = other(f, t);
hanoi(n - 1, f, o);
hanoi(1, f, t);
hanoi(n - 1, o, t);
}
GLfloat wallcolor[] = {0.0f, 0.3f, 1.0f, 1.0f};
void CALLBACK draw(void)
{
int t, f;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
{
glTranslatef(WIDTH / 2.0f, HEIGHT / 2.0f, 0.0f);
glRotatef((float)xlangle, 0.0f, 1.0f, 0.0f);
glRotatef((float)ylangle, 1.0f, 0.0f, 0.0f);
glTranslatef(-WIDTH / 2.0f, -HEIGHT / 2.0f, 0.0f);
glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);
}
glPopMatrix();
glPushMatrix();
{
glTranslatef(WIDTH / 2.0f, HEIGHT / 2.0f, 0.0f);
glRotatef((float)xangle, 0.0f, 1.0f, 0.0f);
glRotatef((float)yangle, 1.0f, 0.0f, 0.0f);
glTranslatef(-WIDTH / 2.0f, -HEIGHT / 2.0f, 0.0f);
DrawPosts();
DrawDooDads();
}
glPopMatrix();
if (motion && moves.depth) {
t = moves.head->t;
f = moves.head->f;
push(t, pop(f));
mpop();
}
auxSwapBuffers();
}
void CALLBACK update(void)
{
draw();
}
void CALLBACK reshape(int w, int h)
{
glViewport(0,0,w,h);
}
int oldx, oldy;
GLboolean leftb = GL_FALSE, middleb = GL_FALSE;
void main(int argc, char *argv[])
{
int i;
auxInitPosition(0, 0, (int)WIDTH, (int)HEIGHT);
auxInitDisplayMode(AUX_RGBA | AUX_DOUBLE | AUX_DEPTH);
auxInitWindow("Hanoi");
GenLists();
Init();
for (i = 0; i < NUM_DISKS; i++)
push(1, NUM_DISKS - i);
hanoi(NUM_DISKS, 1, 3);
auxReshapeFunc(reshape);
auxIdleFunc(update);
auxMainLoop(draw);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
整理分析结果:把第1步中化简问题的条件作为递归 结束条件,将第3步分析得到的算法作为递归算法。
定义函数movedisc( n,fromneedle,toneedle,usingneedle)。将 fromneedle 杆上的 N 个圆盘,借助 usingneedle 杆,移动到 toneedle 杆上。
移动N个圆盘的递归算法描述如下:
movedisc ( n,fromneedle,toneedle,usingneedle )
{ if ( n==1 ) 将 n 号圆盘从 fromneedle 上移到 toneedle;
else {
① movedisc ( n-1,fromneedle,usingneedle,toneedle )
② 将 n 号圆盘从 fromneedle 上移到 toneedle;
③ movedisc ( n-1,usingneedle,toneedle,fromneedle )
}
}
下面是程序
int i=0; /* 移动圆盘数量计数器 */
main( )
{ unsigned n;
scanf ("%d", &n);
movedisc ( n,’a’,’b’,’c’); /* 将A上的N个圆盘借助C将移动到B上 */
printf( "\t Total: %d\n", i );
}
movedisc ( n, fromneedle, toneedle, usingneedle )
unsigned n;
char fromneedle, toneedle, usingneedle;
{ if ( n == 1 )
printf("%2d-(%2d): %c ==> %c\n",++i, n,fromneedle,toneedle);
else {
movedisc ( n-1, fromneedle, usingneedle, toneedle );
printf("%2d-(%2d): %c ==> %c\n",++i, n,fromneedle,toneedle);
movedisc ( n-1, usingneedle, toneedle, fromneedle );
}
}
定义函数movedisc( n,fromneedle,toneedle,usingneedle)。将 fromneedle 杆上的 N 个圆盘,借助 usingneedle 杆,移动到 toneedle 杆上。
移动N个圆盘的递归算法描述如下:
movedisc ( n,fromneedle,toneedle,usingneedle )
{ if ( n==1 ) 将 n 号圆盘从 fromneedle 上移到 toneedle;
else {
① movedisc ( n-1,fromneedle,usingneedle,toneedle )
② 将 n 号圆盘从 fromneedle 上移到 toneedle;
③ movedisc ( n-1,usingneedle,toneedle,fromneedle )
}
}
下面是程序
int i=0; /* 移动圆盘数量计数器 */
main( )
{ unsigned n;
scanf ("%d", &n);
movedisc ( n,’a’,’b’,’c’); /* 将A上的N个圆盘借助C将移动到B上 */
printf( "\t Total: %d\n", i );
}
movedisc ( n, fromneedle, toneedle, usingneedle )
unsigned n;
char fromneedle, toneedle, usingneedle;
{ if ( n == 1 )
printf("%2d-(%2d): %c ==> %c\n",++i, n,fromneedle,toneedle);
else {
movedisc ( n-1, fromneedle, usingneedle, toneedle );
printf("%2d-(%2d): %c ==> %c\n",++i, n,fromneedle,toneedle);
movedisc ( n-1, usingneedle, toneedle, fromneedle );
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不急的话,周末给你写一个.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询