Solving Puzzle In C
This program is for solving puzzle using a c program :
In this there are 15 numbered square pieces mounted on a frame. these pieces can be moved horizontally or vertically.a possible arrangement of these pieces is shown below:
here the last block is empty .
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int a[4][4]={1,4,15,7,8,10,2,11,14,3,6,13,12,9,5};
int ch,i,j,n=0,k,l;
i=3;
j=3;
clrscr();
for(k=0;k<4;k++)
{
for(l=0;l<4;l++)
{ printf("\t");
textcolor(4+BLINK); /*here the number in textcolor (4)specifies the red color in c there are seven color and we can use them using numbers from 1,2,.....7.*/
textbackground(2);
cprintf("%d",a[k][l]);
}
printf("\n");
}
textcolor(4+BLINK); /*here BLINK is for blinking the text */
textbackground(2+BLINK);
cprintf("------------------solve the above given puzzle-----------------\n\n");
do
{
printf("\nenter the direction to move the numbers for moving in different directions press\n 1 for up\n 2 for down\n 3 for left\n 4 for right");
scanf("%d",&n);
switch(n)
{
case 1: a[i][j]=a[i-1][j] ;
a[i-1][j]=0;
i--;
for(k=0;k<4;k++)
{
for(l=0;l<4;l++)
{
printf("\t");
textcolor(4+BLINK);
textbackground(1);
cprintf("%d",a[k][l]);
}
printf("\n");
}
break;
case 2: a[i][j]=a[i+1][j];
a[i+1][j]= 0;
i++;
for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
printf("\t");
textcolor(4+BLINK);
textbackground(2);
cprintf("%d",a[k][l]);
}
printf("\n");
}
break;
case 3: a[i][j]=a[i][j-1];
a[i][j-1]= 0;
j--;
for(k=0;k<4;k++)
{
for(l=0;l<4;l++)
{
printf("\t");
textcolor(6+BLINK);
textbackground(2);
cprintf("%d",a[k][l]);
}
printf("\n");
}
break;
case 4: a[i][j]=a[i][j+1];
a[i][j+1]=0;
j++;
for(k=0;k<4;k++)
{
for(l=0;l<4;l++)
{
printf("\t");
textcolor(7+BLINK);
textbackground(4);
cprintf("%d",a[k][l]);
}
printf("\n");
}
break;
}
printf("\nDo you want to play the game press any number except 5");
scanf("%d",&ch);
} while(ch!=5);
}
In this there are 15 numbered square pieces mounted on a frame. these pieces can be moved horizontally or vertically.a possible arrangement of these pieces is shown below:
here the last block is empty .
#include<conio.h>
#include<graphics.h>
void main()
{
int a[4][4]={1,4,15,7,8,10,2,11,14,3,6,13,12,9,5};
int ch,i,j,n=0,k,l;
i=3;
j=3;
clrscr();
for(k=0;k<4;k++)
{
for(l=0;l<4;l++)
{ printf("\t");
textcolor(4+BLINK); /*here the number in textcolor (4)specifies the red color in c there are seven color and we can use them using numbers from 1,2,.....7.*/
textbackground(2);
cprintf("%d",a[k][l]);
}
printf("\n");
}
textcolor(4+BLINK); /*here BLINK is for blinking the text */
textbackground(2+BLINK);
cprintf("------------------solve the above given puzzle-----------------\n\n");
do
{
printf("\nenter the direction to move the numbers for moving in different directions press\n 1 for up\n 2 for down\n 3 for left\n 4 for right");
scanf("%d",&n);
switch(n)
{
case 1: a[i][j]=a[i-1][j] ;
a[i-1][j]=0;
i--;
for(k=0;k<4;k++)
{
for(l=0;l<4;l++)
{
printf("\t");
textcolor(4+BLINK);
textbackground(1);
cprintf("%d",a[k][l]);
}
printf("\n");
}
break;
case 2: a[i][j]=a[i+1][j];
a[i+1][j]= 0;
i++;
for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
printf("\t");
textcolor(4+BLINK);
textbackground(2);
cprintf("%d",a[k][l]);
}
printf("\n");
}
break;
case 3: a[i][j]=a[i][j-1];
a[i][j-1]= 0;
j--;
for(k=0;k<4;k++)
{
for(l=0;l<4;l++)
{
printf("\t");
textcolor(6+BLINK);
textbackground(2);
cprintf("%d",a[k][l]);
}
printf("\n");
}
break;
case 4: a[i][j]=a[i][j+1];
a[i][j+1]=0;
j++;
for(k=0;k<4;k++)
{
for(l=0;l<4;l++)
{
printf("\t");
textcolor(7+BLINK);
textbackground(4);
cprintf("%d",a[k][l]);
}
printf("\n");
}
break;
}
printf("\nDo you want to play the game press any number except 5");
scanf("%d",&ch);
} while(ch!=5);
}
Comments
Post a Comment