#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
int start( ); //开始界面
void move(int, int, int); //移动盘子
void hanoi(int, int, int, int); //汉诺塔算法
void hanoi_draw(int); //汉诺塔初始界面
int pan[3];
int m;
int main()
{
char n[2];
int k;
setinitmode(INIT_ANIMATION);
initgraph(640, 480);
randomize();
//if(start() == 1)
// {
inputbox_getline("选择盘子个数","请输入盘子个数", n, 2);
k = atoi(n);
pan[0] = k;
pan[1] = 0;
pan[2] = 0;
cleardevice();
hanoi_draw(k);
Sleep(2000);
m = k;
hanoi(k, 1, 2, 3);
getch();
closegraph();
return 0;
// }
/*else if(start == 0)
{
closegraph();
return 0;
}*/
}
/*int start()
{
int x,y;
setcolor(EGERGB(0x0, 0xFF, 0x0));
setfont(108, 0, "华文彩云");
outtextxy(155, 100, "汉诺塔");
setfillcolor(EGERGB(0x0, 0x80, 0x80));
bar(250, 320, 390, 360);
bar(250, 380, 390, 420);
setcolor(EGERGB(0x0, 0xFF, 0x00));
setfont(32, 0, "宋体");
setbkmode(TRANSPARENT);
outtextxy(255, 322, "开始游戏");
outtextxy(255, 382, "结束游戏");
mouse_msg msg = {0};
for( ; is_run(); delay_fps(60));
{
while (mousemsg())
{
msg = getmouse();
}
//cleardevice();
mousepos(&x, &y);
if((int)msg.is_down() && x >= 250 && x <= 390 && y >= 380 && y <= 420)
{
setfillcolor(EGERGB(255,255,255));
bar(100, 450, 200, 300);
return 0;
}
if((int)msg.is_down() && x >= 250 && x <= 390 && y >= 320 && y <= 360)
{
setfillcolor(EGERGB(255,255,255));
bar(100, 450, 200, 300);
return 1;
}
}
}
*/
void hanoi_draw(int k)
{
int i, j, x1, x2, y1, y2;
for(i = 0; i < k; i ++)
{
x1 = i * 10;
x2 = 180 - i * 10;
y1 = 480 - i * 30;
y2 = 450 - i * 30;
setfillcolor(BLUE);
bar(x1, y1, x2, y2);
}
}
void hanoi(int n,int one,int two,int three)
{
if (n == 1)
{
move(n - 1, one, three);
}
else
{
hanoi(n - 1, one, three, two);
Sleep(2000);
move(n - 1, one, three);
Sleep(2000);
hanoi(n - 1, two, one, three);
}
}
void move(int n, int one, int three)
{
int dx = 5, dy = 5;
int x1, y1, x2, y2;//要移动的盘子的位置
int x3, y3, x4, y4;//移动后盘子落下的位置
int a, b, c, d;
pan[one - 1] -= 1;
pan[three - 1] += 1;
x1 = (one - 1) * 200 + (m - n - 1) * 10;//左下角
y1 = 480 - pan[one - 1] * 30;//左下角
x2 = (one - 1) * 200 + 180 - (m - n - 1) * 10;//右上角
y2 = 450 - pan[one - 1] * 30;
a = x1; b = y1; c = x2; d = y2;
x3 = (three - 1) * 200 + (m - n - 1) * 10;
y3 = 480 - (pan[three - 1] - 1) * 30;
x4 = (three - 1) * 200 + 180 - (m - n - 1) * 10;
y4 = 450 - (pan[three - 1] - 1) * 30;
for ( ; is_run(); delay_fps(60) )
{
//a = x1; b = y1; c = x2; d = y2;
if(d >= 50 && a < x3)
{
b -= dy;
d -= dy;
setfillcolor(BLUE);
bar(a, b, c, d);
Sleep(10);
setfillcolor(EGERGB(0, 0, 0));
bar(a, b + dy, c, d + dy);
}
if(d < 50 && a <= x3)
{
a += dx;
c += dx;
setfillcolor(BLUE);
bar(a, b, c, d);
Sleep(10);
setfillcolor(EGERGB(0, 0, 0));
bar(a - dx, b, c - dx, d);
}
if(d <= y4 && a == x3)
{
b += dy;
d += dy;
setfillcolor(BLUE);
bar(a, b, c, d);
Sleep(10);
setfillcolor(EGERGB(0, 0, 0));
bar(a, b-30-dy+5, c, d-30-dy+5);
}
{// 画帧率文字
char str[20];
sprintf(str, "fps %.02f", getfps());
setcolor(WHITE);
outtextxy(0, 0, str);
}
if(a == x3 && d == y4)
break;
}
}
#include <stdio.h>
#include <stdlib.h>
int start( ); //开始界面
void move(int, int, int); //移动盘子
void hanoi(int, int, int, int); //汉诺塔算法
void hanoi_draw(int); //汉诺塔初始界面
int pan[3];
int m;
int main()
{
char n[2];
int k;
setinitmode(INIT_ANIMATION);
initgraph(640, 480);
randomize();
//if(start() == 1)
// {
inputbox_getline("选择盘子个数","请输入盘子个数", n, 2);
k = atoi(n);
pan[0] = k;
pan[1] = 0;
pan[2] = 0;
cleardevice();
hanoi_draw(k);
Sleep(2000);
m = k;
hanoi(k, 1, 2, 3);
getch();
closegraph();
return 0;
// }
/*else if(start == 0)
{
closegraph();
return 0;
}*/
}
/*int start()
{
int x,y;
setcolor(EGERGB(0x0, 0xFF, 0x0));
setfont(108, 0, "华文彩云");
outtextxy(155, 100, "汉诺塔");
setfillcolor(EGERGB(0x0, 0x80, 0x80));
bar(250, 320, 390, 360);
bar(250, 380, 390, 420);
setcolor(EGERGB(0x0, 0xFF, 0x00));
setfont(32, 0, "宋体");
setbkmode(TRANSPARENT);
outtextxy(255, 322, "开始游戏");
outtextxy(255, 382, "结束游戏");
mouse_msg msg = {0};
for( ; is_run(); delay_fps(60));
{
while (mousemsg())
{
msg = getmouse();
}
//cleardevice();
mousepos(&x, &y);
if((int)msg.is_down() && x >= 250 && x <= 390 && y >= 380 && y <= 420)
{
setfillcolor(EGERGB(255,255,255));
bar(100, 450, 200, 300);
return 0;
}
if((int)msg.is_down() && x >= 250 && x <= 390 && y >= 320 && y <= 360)
{
setfillcolor(EGERGB(255,255,255));
bar(100, 450, 200, 300);
return 1;
}
}
}
*/
void hanoi_draw(int k)
{
int i, j, x1, x2, y1, y2;
for(i = 0; i < k; i ++)
{
x1 = i * 10;
x2 = 180 - i * 10;
y1 = 480 - i * 30;
y2 = 450 - i * 30;
setfillcolor(BLUE);
bar(x1, y1, x2, y2);
}
}
void hanoi(int n,int one,int two,int three)
{
if (n == 1)
{
move(n - 1, one, three);
}
else
{
hanoi(n - 1, one, three, two);
Sleep(2000);
move(n - 1, one, three);
Sleep(2000);
hanoi(n - 1, two, one, three);
}
}
void move(int n, int one, int three)
{
int dx = 5, dy = 5;
int x1, y1, x2, y2;//要移动的盘子的位置
int x3, y3, x4, y4;//移动后盘子落下的位置
int a, b, c, d;
pan[one - 1] -= 1;
pan[three - 1] += 1;
x1 = (one - 1) * 200 + (m - n - 1) * 10;//左下角
y1 = 480 - pan[one - 1] * 30;//左下角
x2 = (one - 1) * 200 + 180 - (m - n - 1) * 10;//右上角
y2 = 450 - pan[one - 1] * 30;
a = x1; b = y1; c = x2; d = y2;
x3 = (three - 1) * 200 + (m - n - 1) * 10;
y3 = 480 - (pan[three - 1] - 1) * 30;
x4 = (three - 1) * 200 + 180 - (m - n - 1) * 10;
y4 = 450 - (pan[three - 1] - 1) * 30;
for ( ; is_run(); delay_fps(60) )
{
//a = x1; b = y1; c = x2; d = y2;
if(d >= 50 && a < x3)
{
b -= dy;
d -= dy;
setfillcolor(BLUE);
bar(a, b, c, d);
Sleep(10);
setfillcolor(EGERGB(0, 0, 0));
bar(a, b + dy, c, d + dy);
}
if(d < 50 && a <= x3)
{
a += dx;
c += dx;
setfillcolor(BLUE);
bar(a, b, c, d);
Sleep(10);
setfillcolor(EGERGB(0, 0, 0));
bar(a - dx, b, c - dx, d);
}
if(d <= y4 && a == x3)
{
b += dy;
d += dy;
setfillcolor(BLUE);
bar(a, b, c, d);
Sleep(10);
setfillcolor(EGERGB(0, 0, 0));
bar(a, b-30-dy+5, c, d-30-dy+5);
}
{// 画帧率文字
char str[20];
sprintf(str, "fps %.02f", getfps());
setcolor(WHITE);
outtextxy(0, 0, str);
}
if(a == x3 && d == y4)
break;
}
}