techpreparation-homepage

Home  Interview Questions  Certifications  Aptitude Questions  Tutorials  Placement Papers  Search  Resume  Soft Skills  Video  Forum  Blog


Technical Interview Questions

Visual Basic Interview Question
ASP .NET Interview Questions
C++ Interview Questions
C Interview Questions
                              .........More

Programming Source Codes
C/C++ Source Codes
C# Source Codes
                              .........More

Aptitude Interview Questions
C/C++ Aptitude Questions
C Aptitude Questions
                              .........More

Tutorials
C Tutorial
C++ Tutorial
                              .........More

Soft Skills
Communication Skills
Leadership Skills
                              .........More

 

 

  

C/C++ Source Codes

A puzzle

#include<graphics.h>
#include<dos.h>
#include<string.h>
#include<stdlib.h>
void drawgrid();
void shownumbers();
void initarr();
void startplay();
void movenumber();
void win_or_not();
int getkey();
int search();


char *a[] = {
" 1"," 4","15"," 7",
" 8","10"," 2","11",
"14"," 3"," 6","13",
"12"," 9"," 5"," "
};


char *arr[] = {
" 1"," 4","15"," 7",
" 8","10"," 2","11",
"14"," 3"," 6","13",
"12"," 9"," 5"," "
};


char *arr1[] = {
" 1"," 2"," 3"," 4",
" 5"," 6"," 7"," 8",
" 9","10","11","12",
"13","14","15"," "
};
void main()


{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
drawgrid();
shownumbers();
startplay();
getch();
closegraph();
}
/* To draw the grid containing numbers */
void drawgrid()


{
int i,j,k;
setcolor(YELLOW);
rectangle(10,10,629,469);
setcolor(BLUE);
rectangle(219,139,419,339);
line(269,139,269,339);
line(319,139,319,339);
line(369,139,369,339);
line(219,189,419,189);
line(219,239,419,239);
line(219,289,419,289);
setcolor(CYAN);
settextstyle(7,0,1);
rectangle(20,20,170,80);
outtextxy(30,25,"ESC >> Exit");
outtextxy(30,55,"F1 >> Restart");
setcolor(RED);
settextstyle(1,0,3);
rectangle(230,50,400,80);
setcolor(YELLOW);
outtextxy(275,50,"PUZZLE");
}
/* To display numbers in the Grid */
void shownumbers()


{
int col,row,i,j,k;
i = 229;
j = 149;
k = 0;
setcolor(YELLOW);
settextstyle(2,0,7);
for(row=0;row<4;row++)


{
i = 229;
for(col=0;col<4;col++)


{
outtextxy(i,j,arr[k]);
i += 50;
k++;
}
j += 50;
}
}
/* To start playing the Game */
void startplay()


{
int key;
int solved;
while(1)


{
key = getkey();
movenumber(key);
win_or_not();
}/* end while */
}
/* Returns scancode of the key that has been hit */
int getkey()


{
union REGS i,o;
while(!kbhit())
;
i.h.ah = 0;
int86(22,&i,&o);
return(o.h.ah);
}
/* Retuens the index of blank string in the array arr[] */
int search()


{
int i,a;
for(i=0;i<16;i++)


{
a = strcmp(arr[i], " ");
if(a == 0)
break;
}
return(i);
}
/* Move a number to the blank position as per the arrow key being hit */
void movenumber(int key)


{
int blank;
int col,row,i,j,k;
char *temp;
switch(key)


{
/* for left key */
case 75: blank = search();
if(blank%4==3)


{
sound(1000);
delay(100);
nosound();
break;
}
else


{
temp = arr[blank];
arr[blank] = arr[blank+1];
arr[blank+1] = temp;
cleardevice();
drawgrid();
shownumbers();
break;
}
/* for right key*/
case 77: blank = search();
if(blank%4==0)


{
sound(1000);
delay(100);
nosound();
break;
}
else


{
temp = arr[blank];
arr[blank] = arr[blank-1];
arr[blank-1] = temp;
cleardevice();
drawgrid();
shownumbers();
break;
}
/* for up key */
case 72: blank = search();
if(blank>12)


{
sound(1000);
delay(100);
nosound();
break;
}
else


{
temp = arr[blank];
arr[blank] = arr[blank+4];
arr[blank+4] = temp;
cleardevice();
drawgrid();
shownumbers();
break;
}
/* for down key */
case 80: blank = search();
if(blank<=3)


{
sound(1000);
delay(100);
nosound();
break;
}
else


{
temp = arr[blank];
arr[blank] = arr[blank-4];
arr[blank-4] = temp;
cleardevice();
drawgrid();
shownumbers();
break;
}
case 1: exit(0);
case 59: cleardevice();
drawgrid();
initarr();
shownumbers();
break;
//startplay();
default: sound(1000);
delay(100);
nosound();
} /* end switch */
} /* function movenumber() ends here */
/* Check whether puzzle is solved or not */
void win_or_not()


{
int i,count=0;
int flag,k;
for(i=0;i<16;i++)


{
flag = strcmp(arr[i],arr1[i]);
if(flag==0)
count++;
} /* end for */
if(count==16)


{
setcolor(RED);
settextstyle(1,0,1);
outtextxy(55,380,"* * * * * Congratulations !! You solved the Puzzle * * * * *");
} /* end if */
} /* function win_or_not() ends here */
/* To initialize the array arr[] */
void initarr()


{
int k;
int count;
int count1;
randomize();
count = random(16);
count1 = random(16);
for(k=0;k<16;k++)


{
strcpy(arr[count],a[count1]);
count++;
count1++;
if(count>15)
count=0;
if(count1>15)
count1=0;
}
}


<<<----- Return to C/C++ Source Code Questions Page.


Have a Question ? post your questions here. It will be answered as soon as possible.

Check Object Oriented Interview Questions for more Object Oriented Interview Questions with answers

Check Data Structure Interview Questions for more data structure interview questions with answers