| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6253 人关注过本帖, 1 人收藏
标题:花了一个晚上做的汉诺塔动画,请大家看看!
只看楼主 加入收藏
Lydolphin
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-12-4
收藏(1)
 问题点数:0 回复次数:40 
花了一个晚上做的汉诺塔动画,请大家看看!

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

/* ================================= 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-02-10 05:16
feng1256
Rank: 4
等 级:贵宾
威 望:14
帖 子:2899
专家分:0
注 册:2005-11-24
收藏
得分:0 

厉害了~~


叁蓙大山:工謪、稅務、嗣發 抱歉:不回答女人的问题
2006-02-10 06:05
zhangjuan
Rank: 1
等 级:新手上路
帖 子:992
专家分:0
注 册:2006-1-19
收藏
得分:0 

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


2006-02-10 10:01
Lydolphin
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2005-12-4
收藏
得分:0 
学C一学期了!
C的图形编程是寒假刚学的......

PHP:http://www.
ASP:http://www.
HTML:http://www.
2006-02-10 10:50
aiyuheng
Rank: 1
等 级:新手上路
威 望:1
帖 子:656
专家分:0
注 册:2006-1-12
收藏
得分:0 


when i want to ask anyone,i will ask myself first.
2006-02-10 10:51
zhangjuan
Rank: 1
等 级:新手上路
帖 子:992
专家分:0
注 册:2006-1-19
收藏
得分:0 
厉害

2006-02-10 10:54
Knocker
Rank: 8Rank: 8
等 级:贵宾
威 望:47
帖 子:10454
专家分:603
注 册:2004-6-1
收藏
得分:0 
非常不错

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

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

2006-02-10 13:22
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 

非常不错~!


淘宝杜琨
2006-02-10 14:25
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 

加精了没有?


淘宝杜琨
2006-02-10 14:30
快速回复:花了一个晚上做的汉诺塔动画,请大家看看!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017643 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved