Friday, 14 September 2012

C++ Program to play a puzzle

C++ Program to play a puzzle


#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#include<stdio.h>
getkey1()
{union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
void main2()
{int gd=DETECT,gm,i,j,n=0,m,q=0,v=0,k;
char *a[16]={"9","3","2","4","7","6","5","8","1","11" ,"13","12","10","14"," " ,"15"}

,*b[16]={"1","2","3","4","5","6","7","8","9","10" ,"11","12","13","14","15" ," "},*t;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(3);
outtextxy(260,100," PUZZLE");
setcolor(4);
setcolor(WHITE);
delay(2000);
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,a[n]);
n++;
}
do{{ setcolor(3);
outtextxy(100,170,"YOU HAVE TO SOLVE IT WITHIN 400 MOVES");

setcolor(8);
outtextxy(280,130,"LEVEL: 2 ");
setcolor(9);
outtextxy(100,150,"YOU HAVE SPENT ");
//outtextxy(220,150,v);
gotoxy(28,10);
cout<<v;
outtextxy(240,150," MOVES ");
setcolor(2);
outtextxy(100,330,"USE ARROW KEYS FOR DIRECTION");
setcolor(4);
outtextxy(200,350,"PRESS '* ' TO QUIT AT ANY TIME");
setcolor(5);
outtextxy(400,50,"ARRANGE LIKE THIS");
m=0;
for(i=425;i<=500;i+=25)
for(j=60;j<=135;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,b[m]);
m++;
}
}
setcolor(WHITE);
if((*a[0]=='1')&&(*a[1]=='2')&&(*a[2]=='3')&&(*a[3]=='4')&&(*a[4]=='5')&&(*a[5]=='6')&&(*a[6]=='7')&&(*a[7]=='8')
&& (*a[8]=='9')&&(strcmp(a[9],"10")==0)&&(strcmp(a[10],"11")==0)&&(strcmp(a[11],"12")==0)&&(strcmp(a[12],"13")==0)&&(strcmp(a[13],"14")==0)&&(strcmp(a[14],"15")==0))

{setcolor(5);
outtextxy(200,350,"CONGRATULATION! YOU WIN");
getch();
break;
}
k=getkey1();
sound(500);
delay(100);
nosound();
switch(k)
{case 75:{clearviewport();
n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&(n<=11)&&(q==0))
{t=a[n];
a[n]=a[n+4];
a[n+4]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,a[n]);
n++;
}
q=0; break;
}
case 77:{clearviewport();
n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&(n>3)&&(q==0))
{t=a[n];
a[n]=a[n-4];
a[n-4]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,a[n]);
n++;
}
q=0;break;
}
case 80: {clearviewport();
n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&((n!=0)&&(n!=4)&&(n!=8)&&(n!=12))&&(q==0))
{t=a[n];
a[n]=a[n-1];
a[n-1]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,a[n]);
n++;
}
q=0;break;
}
case 72:{clearviewport();
n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&(n!=3)&&(n!=7)&&(n!=11)&&(n!=15)&&(q==0))
{t=a[n];
a[n]=a[n+1];
a[n+1]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=250;i<=325;i+=25)
for(j=200;j<=275;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,a[n]);
n++;
}
q=0; break;
}
}}while((k!=55)&&(v<=400));
if(v>400) {outtextxy(200,350,"SORRY! YOUR MOVE IS OVER");
getch();
}
closegraph();
}

//puzz6.cpp ends here.





#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<string.h>
#include<stdio.h>
getkey()
{union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
void main()
{int gd=DETECT,gm,i,j,n=0,m,q=0,v=0,k;
char *a[9]={"7","2","5","4","3","6","1"," ","8"}

,*b[9]={"1","2","3","4","5","6","7","8"," "},*t;
initgraph(&gd,&gm,"..//bgi");
setcolor(3);
outtextxy(260,100," PUZZLE");
setcolor(4);
setcolor(WHITE);
delay(2000);
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,a[n]);
n++;
}
do{{ setcolor(3);
outtextxy(100,170,"YOU HAVE TO SOLVE IT WITHIN 200 MOVES");
setcolor(1);
outtextxy(210,130,"LEVEL : 1");
setcolor(9);
outtextxy(100,150,"YOU HAVE SPENT ");
//outtextxy(220,150,v);
gotoxy(28,10);
cout<<v;
outtextxy(240,150," MOVES ");

setcolor(2);
outtextxy(100,330,"USE ARROW KEYS FOR DIRECTION");
setcolor(4);
outtextxy(100,350,"PRESS '* ' TO QUIT AT ANY TIME");
setcolor(5);
outtextxy(400,50,"ARRANGE LIKE THIS");
m=0;
for(i=425;i<=475;i+=25)
for(j=60;j<=110;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+8,j+10,b[m]);
m++;
}
}
setcolor(WHITE);
if((*a[0]=='1')&&(*a[1]=='2')&&(*a[2]=='3')&&(*a[3]=='4')&&(*a[4]=='5')&&(*a[5]=='6')&&(*a[6]=='7')&&(*a[7]=='8'))

{setcolor(5);

outtextxy(180,380,"CONGRATULATION! YOU FINISHED LEVEL 1");
getch();
clearviewport();
main2();
break;
}
k=getkey();
sound(300);
delay(100);
nosound();
switch(k)
{case 75:{clearviewport();
n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&(n<=5)&&(q==0))
{t=a[n];
a[n]=a[n+3];
a[n+3]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+10,j+10,a[n]);
n++;

}
q=0; break;
}
case 77:{clearviewport();
n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&(n>=2)&&(q==0))
{t=a[n];
a[n]=a[n-3];
a[n-3]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+10,j+10,a[n]);
n++;
}
q=0;break;
}
case 80: {clearviewport();
n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&((n!=0)&&(n!=3)&&(n!=6))&&(q==0))
{t=a[n];
a[n]=a[n-1];
a[n-1]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+10,j+10,a[n]);
n++;
}
q=0;break;
}
case 72:{clearviewport();
n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
if ((*a[n]==' ')&&((n!=2)&&(n!=5)&&(n!=8))&&(q==0))
{t=a[n];
a[n]=a[n+1];
a[n+1]=t;
q=1;
v++;
}
n++;
} n=0;
for(i=200;i<=250;i+=25)
for(j=200;j<=250;j+=25)
{rectangle(i,j,i+25,j+25);
outtextxy(i+10,j+10,a[n]);
n++;
}
q=0; break;
}
}}while((k!=55)&&(v<=200));
if(v>200) {outtextxy(200,350,"SORRY! YOUR MOVE IS OVER");
delay(2000);
getch();
}
closegraph();
}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More