THE NUMBER YOU CHOSE
(C program)

Finds the number you chose from 1 to 1024 after asking 10 yes-or-no question
/* ##################################### # The number you chose # # tested in Turbo C++,version 3.0 # ###############2/4/2008############## */ #include <dos.h> #include <math.h> #include <conio.h> #include <stdio.h> #include <string.h> int mx, my, mb; void InitMouse() { union REGS r; r.x.ax=0x0, int86(0x33, &r, &r); } void SetMouse(int x, int y) { union REGS r; r.x.ax=0x4, r.x.cx=x*8, r.x.dx=y*8, int86(0x33, &r, &r); } void ShowMouse() { union REGS r; r.x.ax=0x1, int86(0x33, &r, &r); } void HideMouse() { union REGS r; r.x.ax=0x2, int86(0x33, &r, &r); } int ReadMouse() { union REGS r; r.x.ax=0x3, int86(0x33, &r, &r); mx=(int)r.x.cx/8, my=(int)r.x.dx/8, mb=(int)r.x.bx; return mb; } void Put(int x, int y, char *str) { gotoxy(x,y), HideMouse(), cprintf(str), ShowMouse(); } void Color(int fg, int bg) { textcolor(fg), textbackground(bg); } void HideCurser() { gotoxy(1,1), Color(BLUE,BLUE), cprintf(" "), gotoxy(1,1); } void DrawButton(int x, int y, char *str, int active) { int strLen=strlen(str), i; Color(WHITE,GREEN); HideMouse(); if(active == 1) { if(mx == x-1) SetMouse(mx+1, my); Put(x+1, y, str); ShowMouse(); textbackground(RED); Put(x,y," "); gotoxy(x+1, y+1); for(i=0; i<strLen; i++) cprintf(" "); HideCurser(); while(ReadMouse()); } else { Put(x, y, str); Color(BLACK,RED); Put(x+strLen, y, "Ü"); gotoxy(x+1, y+1); for(i=0; i<strLen; i++) cprintf("ß"); ShowMouse(); HideCurser(); } } int YesNoExit() { char ch; int w=16; DrawButton(32, w, " Yes ", 0); DrawButton(42, w, " No " , 0); HideCurser(); while(1) { if(kbhit() && getch()==27) exit(0); if(ReadMouse()==1 && my==w-1) { if(30<mx&&mx<38) { DrawButton(32,w," Yes ",1); if( my==w-1 && 31<mx&&mx<39) return 1; DrawButton(32,w," Yes ",0); } else if(40<mx&&mx<47) { DrawButton(42,w," No ",1); if( my==w-1 && 41<mx&&mx<48 ) return 0; DrawButton(42,w," No ",0); } } } } void DrawCanvas(int x1, int y1, int x2, int y2, char title[]) { int i, j; HideMouse(); textbackground(BLUE); clrscr(); Color(CYAN,BLUE); Put(24,23,"http://programs.on.googlepages.com"); Color(DARKGRAY,BLACK); gotoxy(x1+2, y2+1); for(i=x1; i<=x2; i++) cprintf(" "); for(i=y1+1; i<=y2; i++) gotoxy(x2+1, i), cprintf(" "); Color(WHITE,RED); gotoxy(x1,y1), cprintf("É"); for(i=x1+1; i<x2; i++) cprintf("Í"); cprintf("»"); for(j=y1+1; j<y2; j++) { gotoxy(x1,j), cprintf("º"); for(i=x1+1; i<x2; i++) cprintf(" "); cprintf("º"); } gotoxy(x1,y2), cprintf("È"); for(i=x1+1; i<x2; i++) cprintf("Í"); cprintf("¼"); textcolor(LIGHTGREEN); i = strlen(title); Put((x2+x1-i+1)/2, y1, title); ShowMouse(); } void HGetOk(int bY) { char ch; DrawButton(37, bY, " Ok ", 0); HideCurser(); while(1) { if(kbhit() && getch()==27) exit(0); if(ReadMouse()==1) { if(my==bY-1 && 35<mx&&mx<44) { DrawButton(37, bY, " Ok ", 1); if(my==bY-1 && 36<mx&&mx<45) return; DrawButton(37,bY," Ok ",0); } } } } void Instructions() { DrawCanvas(15, 8, 66, 18, " THE NUMBER YOU CHOSE "); textcolor(YELLOW); Put(20, 11, "You should choose a number from 1 to 1024."); Put(20, 12, "I will ask you ten yes-or-no questions. "); Put(20, 13, "After that, I will tell you the number. "); textcolor(BLUE); Put(35, 14, "Esc to exit."); HGetOk(16); } void Answer(int ans) { DrawCanvas(28, 10, 53, 16, " ANSWER "); textcolor(YELLOW); Put(32, 12, "The number is "); cprintf("%d", ans); HGetOk(14); } void main() { int printNum, yes, i; textmode(C80); InitMouse(); ShowMouse(); while(1) { Instructions(); printNum=1024, yes=0; DrawCanvas(20, 8, 61, 18, " QUESTION: "); textcolor(YELLOW); Put(25, 12, "Your number is greater than"); for(i=9; i>=0; i--) { if(yes == 0) printNum-=pow(2,i); else printNum+=pow(2,i); Color(LIGHTGREEN,RED); gotoxy(45, 8), cprintf("%d", i); textcolor(YELLOW); gotoxy(53,12),cprintf("%-4d",printNum); yes = (YesNoExit() == 0) ? 0 : 1; } if(yes == 1) printNum++; Answer(printNum); } } /******************************************************* * More programs: http://programs.on.googlepages.com * * Please report bugs to: programs.on@gmail.com * ********************************************256*lines**/