做得不好不要骂我...
毕竟刚接触不久......
/* ================================= 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] ) ;
}
}
/* --------------------------------------------------------------------------------------- */