Lưu ý: Code này chỉ chạy được trên Borland C !
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#define move(x,y,z) makeblocks(x,y,z); delay(t); delblocks(x, y, z);
int t;
int a[] = { 0, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 };
int color[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
int bg = 0;
void makeblocks(int x, int y, int z)
{
int h = a[z] / 2;
window(x - h, y, x + h, y);
textbackground(color[z]);
textcolor(color[z]);
clrscr();
}
void delblocks(int x, int y, int z)
{
int h = a[z] / 2;
window(x - h, y, x + h, y);
textbackground(bg);
clrscr();
}
void tower(int x, int y, int m)
{
for (int i = m; i >= 1; i--)
makeblocks(x, y - m + i, i);
}
void moveblocks(int x1, int y1, int x2, int y2, int z)
{
static int count = 0;
int x, y;
count++;
window(1, 1, 24, 1);
textbackground(BLUE);
clrscr();
textcolor(WHITE);
cprintf("\nLan dich chuyen thu: ");
textcolor(YELLOW + BLINK);
cprintf("%4d", count);
for (y = y1; y >= 4; y--)
{
move(x1, y, z);
}
if (x1<x2)
for (x = x1; x <= x2; x++)
{
move(x, 4, z);
}
else
for (x = x1; x >= x2; x--)
{
move(x, 4, z );
}
for (y = 4; y <= y2; y++)
{
move(x2, y, z);
}
makeblocks(x2, y2,z);
}
void movetower(int x1, int y1, int x2, int y2, int x3, int y3, int m)
{
if (m < 1) return;
else
if (m == 1)
moveblocks(x1, y1, x2, y2, 1);
else
{
movetower(x1, y1 - 1, x3, y3, x2, y2, m - 1);
moveblocks(x1, y1, x2, y2, m);
movetower(x3, y3, x2, y2 - 1, x1, y1, m - 1);
}
}
void main(void)
{
int m;
int x1, y1, x2, y2, x3, y3;
x1 = 13;
x2 = 40;
x3 = 67;
y1 = y2 = y3 = 24;
textmode(C80);
clrscr();
printf("\nSo tang: ");
scanf("%d", &m);
printf("\nTime delay: ");
scanf("%d", &t);
textbackground(bg);
clrscr();
tower(x1, y1, m);
getch();
movetower(x1, y1, x2, y2, x3, y3, m);
getch();
window(1, 1, 80, 25);
textbackground(bg);
textcolor(WHITE);
clrscr();
}
Không có nhận xét nào:
Đăng nhận xét