C Programming Code Examples C > Beginners Lab Assignments Code Examples Puzzle--finding the number Puzzle--finding the number #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<math.h> int mx,my,ans=0,p=4; screen(); void button(); /* The main logic lies in displaying the numbers in the screen The numbers are placed corresponding to their binary value EX:In case of 14(binary equivalent is 01110).Therefore it will be placed in 2,3 & 4 screen */ int s1[]={16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}; int s2[]={8,9,10,11,12,13,14,15,24,25,26,27,28,29,30,31}; int s3[]={4,5,6,7,12,13,14,15,20,21,22,23,28,29,30,31}; int s4[]={2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31}; int s5[]={1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31}; int a1,a2,a3,a4,a5; void main() { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\tc\bgi"); mouseini(); showmp(); action(); } action() { int ans=0; char st[10]; initialscreen(); while(1) { /* The X & Y coordinate of each function is calculated with respect to mx "maximum of x value" & my "maximum of y value" */ if(click((mx/4)+95,(my/4)+100,(mx/4)+235,(my/4)+140,"Play..")==0) { a1=screen(s1,16); a2=screen(s2,16); a3=screen(s3,16); a4=screen(s4,16); a5=screen(s5,16); ans=a1+a2+a3+a4+a5; result(ans); getch(); exit(); } } } initialscreen() { mx=getmaxx(),my=getmaxy(); setmp(); setfillstyle(1,7); bar(mx/4,my/4,3*mx/4,3*my/4); setcolor(WHITE); line(mx/4,my/4,3*mx/4,my/4); line(mx/4,my/4,mx/4,3*my/4); setcolor(BLACK); line(mx/4,3*my/4,3*mx/4,3*my/4); line(3*mx/4,my/4,3*mx/4,3*my/4); setcolor(RED); outtextxy((mx/4)+40,(my/4)+30,"THINK A NUMBER BETWEEN 1-31"); button((mx/4)+95,(my/4)+100,(mx/4)+235,(my/4)+140,"Play.."); } screen(int *scr,int num) { int a,i,x1,y1; char st[10]; mx=getmaxx(),my=getmaxy(); setmp(); setfillstyle(1,7); bar(mx/4,my/4,3*mx/4,3*my/4); setcolor(WHITE); line(mx/4,my/4,3*mx/4,my/4); line(mx/4,my/4,mx/4,3*my/4); setcolor(BLACK); line(mx/4,3*my/4,3*mx/4,3*my/4); line(3*mx/4,my/4,3*mx/4,3*my/4); setcolor(RED); outtextxy((mx/4)+9,(my/4)+5,"TELL ME WHETHER THE NUMBER IS PRESENT?"); button((3*mx/4)-40 ,(3*my/4)-20,(3*mx/4)-5 ,(3*my/4)-5,"yes"); button((3*mx/4)-80 ,(3*my/4)-20,(3*mx/4)-45 ,(3*my/4)-5,"no"); x1=(mx/4)+80; y1=(my/4)+65; for(i=0;i<num;i++) { a=scr[i]; /*To convert the integer into string and then displaying it on the graphic mode*/ itoa(a,st,10); outtextxy(x1 ,y1,&st); x1+=50; if((i==3)||(i==7)||(i==11)) { y1=y1+30; x1=(mx/4)+80; } } /*We have already noted that the numbers are placed in screen corresponding to their binary value To find the number we can convert these binary numbers to integers*/ while(1) { if (click((3*mx/4)-40 ,(3*my/4)-20,(3*mx/4)-5 ,(3*my/4)-5,"yes")==0) { ans=pow(2,p); p=p-1; return ans; } if (click((3*mx/4)-80 ,(3*my/4)-20,(3*mx/4)-45 ,(3*my/4)-5,"no")==0) { p=p-1; return 0; } } } result(int ans) { char st[10]; mx=getmaxx(),my=getmaxy(); setmp(); setfillstyle(1,7); bar(mx/4,my/4,3*mx/4,3*my/4); setcolor(WHITE); line(mx/4,my/4,3*mx/4,my/4); line(mx/4,my/4,mx/4,3*my/4); setcolor(BLACK); line(mx/4,3*my/4,3*mx/4,3*my/4); line(3*mx/4,my/4,3*mx/4,3*my/4); setcolor(RED); if(ans==0) { outtextxy((mx/4)+110,(my/4)+45,"I DONT BELEIVE"); outtextxy((mx/4)+60,(my/4)+60,"YOU MIGHT BE WRONG SOMEWHERE"); } else { outtextxy((mx/4)+95,(my/4)+30,"THE NUMBER IS "); itoa(ans,st,10); outtextxy((mx/4)+205,(my/4)+30,st); } button((mx/4)+95,(my/4)+100,(mx/4)+235,(my/4)+140,"TRY AGAIN!"); button((mx/4)+95,(my/4)+150,(mx/4)+235,(my/4)+190,"EXIT"); while(1) { if (click((mx/4)+95,(my/4)+150,(mx/4)+235,(my/4)+190,"EXIT")==0) { closegraph(); restorecrtmode(); exit(); } if (click((mx/4)+95,(my/4)+100,(mx/4)+235,(my/4)+140,"TRY AGAIN!")==0) { p=4; action(); } } } void button(int x1,int y1,int x2,int y2,char str[]) { int xc,yc,i=0,l=0; while(i<strlen(str)) { l+=4; i++; } xc=(x2-x1)/2+x1-l; yc=(y2-y1)/2+y1; unpress(x1,y1,x2,y2); settextstyle(0,0,0); setcolor(RED); outtextxy(xc,yc,str); } unpress(int x1,int y1,int x2,int y2) { setlinestyle(0,1,1); setfillstyle(1,7); bar(x1,y1,x2,y2); setcolor(WHITE); line(x1,y1,x2,y1); line(x1,y1,x1,y2); setcolor(0); line(x1,y2,x2,y2); line(x2,y1,x2,y2); return 0; } press(int x1,int y1,int x2,int y2) { setlinestyle(0,1,1); setfillstyle(1,7); bar(x1,y1,x2,y2); setcolor(0); line(x1,y1,x2,y1); line(x1,y1,x1,y2); setcolor(WHITE); line(x1,y2,x2,y2); line(x2,y1,x2,y2); return 0; } mouseini() { union REGS i,o; i.x.ax=0; int86(0x33,&i,&o); return(o.x.ax); } showmp() { union REGS i,o; i.x.ax=1; int86(0x33,&i,&o); return 0; } hidemp() { union REGS i,o; i.x.ax=2; int86(0x33,&i,&o); return 0; } getmp(int *button,int *x,int *y) { union REGS i,o; i.x.ax=3; int86(0x33,&i,&o); *button=o.x.bx; *x=o.x.cx; *y=o.x.dx; return 0; } setmp() { union REGS i,o; i.x.ax=4; i.x.cx=(3*mx/4)+20; i.x.dx=(3*my/4)+20; int86(0x33,&i,&o); } click(int x1,int y1,int x2,int y2,char str[]) { int button,x,y; int xc,yc,i=0,l=0; while(i<strlen(str)) { l+=4; i++; } xc=(x2-x1)/2+x1-l; yc=(y2-y1)/2+y1; getmp(&button,&x,&y); if( (x>x1 && x<x2) && (y>y1 && y<y2) && button==1) { hidemp(); press(x1,y1,x2,y2); setcolor(RED); settextstyle(0,0,0); outtextxy(xc,yc,str); showmp(); while((button==1)) getmp(&button,&x,&y); hidemp(); unpress(x1,y1,x2,y2); showmp(); setcolor(RED); settextstyle(0,0,0); outtextxy(xc,yc,str); for(i=50;i<500;i=i+50) { delay(10); sound(i+200); } showmp(); nosound(); setcolor(RED); settextstyle(0,0,0); outtextxy(xc,yc,str); return 0; } else return 1; }