Tháp HN

Màu nền
Font chữ
Font size
Chiều cao dòng

#include <stdio.h>

#include <conio.h>

const int time=30;

int stc[4]; // So tang cua 3 cot

int st,docao,huong,i,mau,xd,yd,rong;

void xay_thap(int n, int c)

  {

    mau=0;

    yd=24;

    xd=c*20-10;

    rong=20;

    for (i=1; i<=n; i++)

      {

mau=mau+1;

mau=mau % 8;

if (mau==0)

 mau++;

textbackground(mau);

window(xd,yd,xd+rong,yd+1);

clrscr();

yd=yd-2;

xd=xd+1;

rong=rong-2;

delay(time*7);

      }

    stc[c]=n;

  }

void bay_len(int c1)

  {

   do

    {

     textbackground(BLACK);

     window(xd,yd,xd+rong,yd+1);

     clrscr();

     yd=yd-1;

     textbackground(mau);

     window(xd,yd,xd+rong,yd+1);

     clrscr();

     delay(time);

     if (kbhit())

exit(0);

    }

   while (yd!=docao);

   stc[c1]=stc[c1]-1;

  }

void bay_ngang(int t,int c1, int c2)

  {

   if (c2-c1>0)

     huong=1;

   else

     huong=-1;

   do

    {

     textbackground(BLACK);

     window(xd,yd,xd+rong,yd+1);

     clrscr();

     xd=xd+huong;

     textbackground(mau);

     window(xd,yd,xd+rong,yd+1);

     clrscr();

     delay(time);

     if (kbhit())

exit(0);

    }

   while (xd!=c2*20-10+(t-1));

  }

void bay_xuong(int c2)

  {

   do

    {

     textbackground(BLACK);

     window(xd,yd,xd+rong,yd+1);

     clrscr();

     yd=yd+1;

     textbackground(mau);

     window(xd,yd,xd+rong,yd+1);

     clrscr();

     delay(time);

     if (kbhit())

exit(0);

    }

   while (yd!=26-stc[c2]*2-2);

   stc[c2]=stc[c2]+1;

  }

void bay(int t, int c1, int c2)

  {

   yd=26-stc[c1]*2;

   xd=c1*20-10+(t-1);

   rong=20-(t-1)*2;

   mau=t % 8;

   if (mau==0)

     mau=1;

   if (stc[c1]>stc[c2])

     docao=stc[c1];

   else

     docao=stc[c2];

   if ((abs(c1-c2)==2) && (stc[2]>docao))

     docao=stc[2];

   docao=26-docao*2-3;

   bay_len(c1);

   bay_ngang(t,c1,c2);

   bay_xuong(c2);

  }

void chuyen_thap(int n, int c1, int c3, int c2)

  {

   if (n==1)

     bay(st-n+1,c1,c3);

   else

     {

      chuyen_thap(n-1,c1,c2,c3);

      bay(st-n+1,c1,c3);

      chuyen_thap(n-1,c2,c3,c1);

     }

  }

void main()

 {

  textmode(C80);

  textbackground(BLACK);

  clrscr();

  stc[1]=0;

  stc[2]=0;

  stc[3]=0;

  printf("Nhap so tang <12: "); scanf("%d",&st);

  if (st>11)

    exit(0);

  xay_thap(st,1);

  chuyen_thap(st,1,3,2);

  getch();

 }

Bạn đang đọc truyện trên: Truyen2U.Pro