#include
#include
#include
#include
#include
#include
#include
#include
#include
#define towers_col 14
#define towers_place 19
#define tower "ÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄ"
class DISKS
{
public:
DISKS(int dnum);
int xpos,ypos;
int color;
char *fig;
void move(int num,int from,int to);
}**disk;
int towersx[3]={15,42,68};
int towersy[3]= {towers_place-1,towers_place-1,towers_place-1};
int k,all;
char beep,**buf;
char far *Video_Mem;
void write_string(int x, int y, char *p, int attrib)
{
register int i;
char far *v;
v= Video_Mem;
v+= ((y-1) * 160) + (x-1)*2;
for (;*p;i++)
{
*v++= *p++;
*v++= attrib;
}
}
/******************************************************************************/
void cursor_size(int a,int b)
{
union REGS reg;
reg.h.ah = 1;
reg.h.bh = 0;
reg.h.ch = a;
reg.h.cl = b;
int86(0x10,®,®);
}
/******************************************************************************/
void video_mode(void)
{
union REGS r;
r.h.ah= 15;
if ((int86(0x10,&r,&r) & 255) == 7)
Video_Mem= (char far *)0xB0000000;
else
Video_Mem= (char far *)0xB8000000;
}
/******************************************************************************/
DISKS::DISKS(int dnum)
{
fig= (char *)malloc(2*dnum+2);
memset(fig,'ß',2*dnum +1);
*(fig)= 'Û';
*(fig + 2*dnum )='Û';
*(fig + 2*dnum +1)= 0;
color= 16 - dnum;
xpos=towersx[0] -(dnum+1);
ypos=towersy[0];
buf[dnum- 1]= (char *)malloc(100);
gettext(xpos,ypos,xpos+strlen(fig)- 1,ypos,buf[dnum- 1]);
write_string(xpos,ypos,fig,color);
towersy[0]-= 1;
}
/******************************************************************************/
void DISKS::move(int num,int from,int to)
{
char buffer[100];
static n= 1,speed =120;
char ch= 0;
strcpy(buffer,buf[num- 1]);
gotoxy(12,23);
textattr(15);
cprintf("%d Of %d ÄÄÄ> ",n++,all);
cprintf("Moving Disk #%d",num);
cprintf(" From Tower %c",'A'+from);
cprintf(" To Tower %c . ",'A'+to);
do
{
puttext(xpos,ypos,xpos+strlen(fig)- 1,ypos,buffer);
if ((ypos > towers_place -k-3)&&(xpos==towersx[from]-(num+1)))
ypos--;
else
{
if (xpos != towersx[to] -(num+1))
{
if (xpos < towersx[to] -(num+1))
xpos++;
if (xpos > towersx[to] -(num+1))
xpos--;
}
else
if (ypos < towersy[to])
ypos++;
}
gettext(xpos,ypos,xpos+strlen(fig)- 1,ypos,buffer);
write_string(xpos,ypos,fig,color);
delay(speed/6);
if (kbhit())
ch= getch();
switch (ch)
{
case '+':
if (speed > 6)
speed--;
ch= 0;
break;
case '-':
if (speed < 180)
speed++;
ch= 0;
break;
}
*(int *)0x0000041c=*(int *)0x0000041a;
}
while ((xpos!=towersx[to] -(num+1)) || (ypos!=towersy[to]));
if (toupper(ch) == 'S')
beep = !beep;
if (beep)
{ sound((k-num+1)*30+800);
delay(speed/3);
nosound();
}
switch (ch)
{
case 27:
textattr(11);
getch();
clrscr();
cursor_size(3,4);
exit(0);
case 32:
while(((ch=getch()) != 32) && (ch != '+') && (ch != '-') && (ch != 27));
break;
}
ch= 0;
towersy[from]+=1;
towersy[to]-=1;
strcpy(buf[num- 1],buffer);
}
/******************************************************************************/
void costruct(int num)
{
int i;
textattr(8);
clrscr();
for (i=0;i<=97;i++)
cputs(" Towers Of Hanoi ");
textattr(9);
cputs("\r \n\rÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ");
textattr(3);
cputs(" Spacebar:Pause +,-:Speed S:Sound Esc:Exit");
disk= (DISKS **)malloc(num*sizeof(*disk));
for (i= 0;i < num;i++)
disk[i]= (DISKS *)malloc(sizeof(**disk));
buf= (char **)malloc(num*sizeof(*buf));
write_string(towersx[0]-10,towers_place,tower,towers_col);
write_string(towersx[1]-10,towers_place,tower,towers_col);
write_string(towersx[2]-10,towers_place,tower,towers_col);
for (i= 1;i< num+2;i++)
{
write_string(towersx[0]-1,towers_place-i,"³",towers_col);
write_string(towersx[1]-1,towers_place-i,"³",towers_col);
write_string(towersx[2]-1,towers_place-i,"³",towers_col);
}
write_string(towersx[0]-1,towers_place+1,"A",towers_col);
write_string(towersx[1]-1,towers_place+1,"B",towers_col);
write_string(towersx[2]-1,towers_place+1,"C",towers_col);
for (i= num;i> 0;i--)
disk[i]= new DISKS(i);
}
/******************************************************************************/
void swap(int &a,int &b){
int c;
c=a;
a=b;
b=c;
}
// ************************************************************************
void Hanoi(int n,int from,int temp,int to)
{
if (n>1){
Hanoi(n-1,from,to,temp);
}
if(n>0)
disk[n]->move(n,from,to);
if(n>0) {
Hanoi(n-1,temp,from,to);
}
}
/******************************************************************************/
void intro(void)
{
char s[3];
char ch;
cursor_size(1,0);
textattr(11);
clrscr();
gotoxy(34,1);
cputs("IN THE OF GOD");
gotoxy(29,2);
cputs(" WELL COM TO TOWERS OF\n\n\n\n\r");
textattr(9);
cputs(" Û Û ÛÛ ÛÛ Û ÛÛÛÛÛÛ ÛÛÛÛÛ \n\r");
cputs(" Û Û Û Û Û Û Û Û Û Û \n\r");
cputs(" Û Û Û Û Û Û Û Û Û Û \n\r");
cputs(" Û Û Û Û Û Û Û Û Û Û \n\r");
cputs(" Û Û Û Û Û Û Û Û Û Û \n\r");
cputs(" ÛÜÜÜÜÜÜÜÜÛ ÛÜÜÜÜÜÜÜÜÛ Û Û Û Û Û Û \n\r");
cputs(" Û Û Û Û Û Û Û Û Û Û \n\r");
cputs(" Û Û Û Û Û Û Û Û Û Û \n\r");
cputs(" Û Û Û Û Û Û Û Û Û Û \n\r");
cputs(" Û Û Û Û Û Û Û Û Û Û \n\r");
cputs(" Û Û Û Û Û ÛÛ ÛÛÛÛÛÛ ÛÛÛÛÛ \n\n\n\r");
textattr(15);
cputs(" Wrietn By HOSAIN YOUSEFPOUR .\n\n\r");
textattr(14);
cputs(" Enter The Number Of Disks (1..13): < >");
s[0]= 3;
while (!((k < 14)&&(k > 0)))
{
gotoxy(46,21);
cputs("..\b\b");
textattr(12);
cgets(s);
textattr(14);
k= atoi(s+2);
}
cputs("\n\n\r With Sound ?(Y/N)");
all= pow(2,k)-1;
while (((ch= toupper(getch())) != 'N') && (ch != 'Y') && (ch != 27));
if (ch == 27)
{
textattr(11);
clrscr();
cursor_size(3,4);
exit(0);
}
beep= ch- 'N';
}
/******************************************************************************/
void main (void)
{
intro();
video_mode();
costruct(k);
Hanoi(k,0,2,1);
while(getch() != 27);
textattr(11);
clrscr();
cursor_size(3,4);
exit(0);
}