| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 3042 人关注过本帖
标题:花了一个晚上做的汉诺塔动画,请大家看看!
收藏  订阅  推荐  打印 
Lydolphin
Rank: 2
等级:注册会员
帖子:47
积分:620
注册:2005-12-4
花了一个晚上做的汉诺塔动画,请大家看看!

做得不好不要骂我...
毕竟刚接触不久......

/* ================================= Program Description ================================= */
/* Program Name : Hanoi.cpp */
/* Program Purpose : Simulate Hanoi */
/* Environment : TC3.0 */
/* Operating System : Windows XP */
/* Written By Lydolphin. */
/* ======================================================================================= */

/* --------------------------------------------------------------------------------------- */

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <dos.h>

/* --------------------------------------------------------------------------------------- */

#define PATH "D:\\Software\\Programs\\TC3.0\\BGI"
#define N 4 //修改盘数目
#define Rate 4 //修改速度

/* --------------------------------------------------------------------------------------- */

int x[N][2], y[N][2] ;
int countA, countB, countC ;

/* --------------------------------------------------------------------------------------- */

void init(int numT) ;
void Hanoi(char from, char to, char aux, int n) ;
void movTower(int n, char from, char to) ;
void movup(int arrayE, int x1, int x2) ;
void movdown(int arrayE, int x1, int x2, int countN) ;
void movright(int arrayE, int x, int n) ;
void movleft(int arrayE, int x, int n) ;

/* --------------------------------------------------------------------------------------- */

void main()
{
int gdriver=DETECT, gmode ;

registerbgidriver(EGAVGA_driver) ;
initgraph(&gdriver, &gmode, PATH) ;

countA=N ;
countB=0 ;
countC=0 ;

init(N) ;
getch() ;
Hanoi('A', 'C', 'B', N) ;

getch() ;
closegraph() ;
}

/* --------------------------------------------------------------------------------------- */

void init(int numT)
{
int i ;
int count=0 ;

bar(10, 70, 629, 409) ;// the border
setfillstyle(1, 0) ;
bar(30, 359, 609, 369) ;//the bottom
bar(121, 150, 131, 358) ;//the lef
bar(314, 150, 324, 358) ;//the mid
bar(507, 150, 517, 358) ;//the rig
setcolor(8) ;
for(i=numT-1 ; i>=0 ; i--)
{
x[count][0]=121-(numT-i)*4 ;
x[count][1]=131+(numT-i)*4 ;
y[count][0]=348-i*11 ;
y[count][1]=358-i*11 ;
rectangle(x[count][0], y[count][0], x[count][1], y[count][1] ) ;
count++ ;
}
}

/* --------------------------------------------------------------------------------------- */

void Hanoi(char from, char to, char aux, int n)
{
if(n==1)
movTower(1, from, to) ;
else
{
Hanoi(from, aux, to, n-1) ;
movTower(n, from, to) ;
Hanoi(aux, to, from, n-1) ;
}
}

/* --------------------------------------------------------------------------------------- */

void movTower(int n, char from, char to)
{
int arrayE ;//定义二维数组元素的第一维位置

arrayE=n-1 ;

if(from=='A' && to=='C')
{
countA-- ;

movup(arrayE, 121, 131) ;

movright(arrayE, 507, n) ;

movdown(arrayE, 507, 517, countC) ;

countC++ ;
}
if(from=='A' && to=='B')
{
countA-- ;

movup(arrayE, 121, 131) ;

movright(arrayE, 314, n) ;

movdown(arrayE, 314, 324, countB) ;

countB++ ;
}
if(from=='B' && to=='A')
{
countB-- ;

movup(arrayE, 314, 324) ;

movleft(arrayE, 121, n) ;

movdown(arrayE, 121, 131, countA) ;

countA++ ;
}
if(from=='B' && to=='C')
{
countB-- ;

movup(arrayE, 314, 324) ;

movright(arrayE, 507, n) ;

movdown(arrayE, 507, 517, countC) ;

countC++ ;
}
if(from=='C' && to=='A')
{
countC-- ;

movup(arrayE, 507, 517) ;

movleft(arrayE, 121, n) ;

movdown(arrayE, 121, 131, countA) ;

countA++ ;
}
if(from=='C' && to=='B')
{
countC-- ;

movup(arrayE, 507, 517) ;

movleft(arrayE, 314, n) ;

movdown(arrayE, 314, 324, countB) ;

countB++ ;
}
}

/* --------------------------------------------------------------------------------------- */

void movup(int arrayE, int x1, int x2)
{
while(y[arrayE][1] > 140)
{
delay(Rate) ;
setcolor(15) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
setcolor(0) ;
if(y[arrayE][0] > 150)
line(x1, y[arrayE][0], x2, y[arrayE][0] ) ;
if(y[arrayE][1] > 150)
line(x1, y[arrayE][1], x2, y[arrayE][1] ) ;
y[arrayE][0] -- ;
y[arrayE][1] -- ;
setcolor(8) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
}
}

/* --------------------------------------------------------------------------------------- */

void movdown(int arrayE, int x1, int x2, int countN)
{
int botCPos ;

botCPos=358-countN*11 ;
while(y[arrayE][1] < botCPos)
{
delay(Rate) ;
setcolor(15) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
setcolor(0) ;
if(y[arrayE][0] > 150)
line(x1, y[arrayE][0], x2, y[arrayE][0] ) ;
if(y[arrayE][1] > 150)
line(x1, y[arrayE][1], x2, y[arrayE][1] ) ;
y[arrayE][0] ++ ;
y[arrayE][1] ++ ;
setcolor(8) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
}
}

/* --------------------------------------------------------------------------------------- */

void movright(int arrayE, int x1, int n)
{
int movCPos ;

movCPos=x1-n*4 ;
while(x[arrayE][0] < movCPos)
{
delay(Rate) ;
setcolor(15) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
x[arrayE][0] ++ ;
x[arrayE][1] ++ ;
setcolor(8) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
}
}

/* --------------------------------------------------------------------------------------- */

void movleft(int arrayE, int x1, int n)
{
int movCPos ;

movCPos=x1-n*4 ;
while(x[arrayE][0] > movCPos)
{
delay(Rate) ;
setcolor(15) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
x[arrayE][0] -- ;
x[arrayE][1] -- ;
setcolor(8) ;
rectangle(x[arrayE][0], y[arrayE][0], x[arrayE][1], y[arrayE][1] ) ;
}
}

/* --------------------------------------------------------------------------------------- */

搜索更多相关主题的帖子: 汉诺塔  动画  
2006-2-10 05:16
feng1256
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:14
帖子:2891
积分:29010
注册:2005-11-24

厉害了~~


叁蓙大山:工謪、稅務、嗣發 抱歉:不回答女人的问题
2006-2-10 06:05
zhangjuan
Rank: 6Rank: 6
等级:金牌会员
帖子:992
积分:10120
注册:2006-1-19

不知道楼主学了多久了呢?


2006-2-10 10:01
Lydolphin
Rank: 2
等级:注册会员
帖子:47
积分:620
注册:2005-12-4

学C一学期了!
C的图形编程是寒假刚学的......

2006-2-10 10:50
aiyuheng
Rank: 4
等级:高级会员
威望:1
帖子:656
积分:6710
注册:2006-1-12


when i want to ask anyone,i will ask myself first.
2006-2-10 10:51
zhangjuan
Rank: 6Rank: 6
等级:金牌会员
帖子:992
积分:10120
注册:2006-1-19

厉害

2006-2-10 10:54
Knocker
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:36
帖子:9963
积分:324662
注册:2004-6-1

非常不错

九洲方除百尺冰,映秀又遭蛮牛耕。汽笛嘶鸣国旗半,哀伤尽处是重生。     -老K
治国就是治吏。礼义廉耻,国之四维。四维不张,国之不国。   -毛泽东
2006-2-10 12:06
cuimartin
Rank: 1
等级:新手上路
帖子:11
积分:210
注册:2005-12-30

难懂~~~~~~~~呵呵

2006-2-10 13:22
神vLinux飘飘
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:87
帖子:6085
积分:61226
注册:2004-7-17

非常不错~!


泛出微微的蓝色的光,像有生命般涌动着,闪烁着,平滑而优美,达到了机械和美学结合的最高境界,向一件艺术品一样,默默的展示着,寂寞而孤傲,只有宇宙才能证实它的存在,只有永恒可以于它并存
2006-2-10 14:25
神vLinux飘飘
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:87
帖子:6085
积分:61226
注册:2004-7-17

加精了没有?


泛出微微的蓝色的光,像有生命般涌动着,闪烁着,平滑而优美,达到了机械和美学结合的最高境界,向一件艺术品一样,默默的展示着,寂寞而孤傲,只有宇宙才能证实它的存在,只有永恒可以于它并存
2006-2-10 14:30
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.066646 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved