Windows 7 has provided gadgets in which they have a simple puzzle game called Picture Puzzle... I made the same 1 with numbers at the places of parts of picture...
Share the code for Non-profit purpose..
Here you have to arrange all numbers in ascending order as
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 __
Use w a s d to swap the numbers with "__" sign considered as free space.
you can change the puzzle by just changing the contents of an array c[4][4]
© All Rights Reserved.
/*
Author: Roshan Kedar
*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
void swap(int *a,int *b);
int compare(int a[4][4], int b[4][4]);
int pos(int c[4][4], int d,int no);
void display(int a[4][4]);
void main()
{
int a[4][4],c[4][4],no,b=1,i,j,d=16,d1=16,comp,no3;
char no2;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
a[i][j]=b;
b++;
}
}
c[0][0]=4;
c[0][1]=13;
c[0][2]=3;
c[0][3]=15;
c[1][0]=6;
c[1][1]=14;
c[1][2]=2;
c[1][3]=8;
c[2][0]=7;
c[2][1]=1;
c[2][2]=16;
c[2][3]=11;
c[3][0]=12;
c[3][1]=5;
c[3][2]=9;
c[3][3]=10;
abc:;
d1=16;
d=16;
comp=9;
clrscr();
printf("\nHey game lovers here is the gadget game for Windows");
printf("\n\nmove the nos using w(up),a(left),s(down),d(right) and arrange them");
printf("\n\nFor any instance 2 quit press O");
printf("\n...........................................");
display(c);
printf("\t\n");
no2=getche();
no3=no2;
d1=pos(c,d,16);
if(no3==119)
{
if(d1<12)
{
swap(&c[d1/4][d1%4],&c[(d1/4)+1][d1%4]);
comp=compare(c,a);
if(comp==6)
goto ab;
else
goto abc;
}
else
{
goto abc;
}
}
else if(no3==115)
{
if(d1>3)
{
swap(&c[d1/4][d1%4],&c[d1/4-1][d1%4]);
comp=compare(c,a);
if(comp==6)
goto ab;
else
goto abc;
}
else
{
goto abc;
}
}
else if(no3==100)
{
if(d1%4>0)
{
swap(&c[d1/4][d1%4],&c[d1/4][d1%4-1]);
comp=compare(c,a);
if(comp==6)
goto ab;
else
goto abc;
}
else
{
goto abc;
}
}
else if(no3==97)
{
if(d1%4<3)
{
swap(&c[d1/4][d1%4],&c[d1/4][d1%4+1]);
comp=compare(c,a);
if(comp==6)
goto ab;
else
goto abc;
}
else
{
goto abc;
}
}
else if(no3==111)
exit(1);
else
{
goto abc;
}
ab:;
clrscr();
printf("\n\n\n\n*********congratulation puzzle solved*********");
display(c);
printf("16");
getch();
}
int pos(int c[4][4],int d,int no)
{
int i,j;
if(no<17&&no>0)
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(c[i][j]==no)
{
d=(4*i)+(1*j);
break;
}
}
}
}
return d;
}
void display(int a[4][4])
{
int i,j;
printf("\n");
for(i=0;i<4;i++)
{
printf("\n");
for(j=0;j<4;j++)
{
if(a[i][j]==16)
printf("\t__");
else
printf("\t%d",a[i][j]);
}
}
}
int compare(int a[4][4], int b[4][4])
{
int i,j,comp=9;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(a[i][j]==b[i][j])
comp=i+j;
else
return comp;
}
}
return comp;
}
void swap(int *a,int *b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
C Programming. I miss that language! :D
ReplyDeleteThis comment has been removed by the author.
ReplyDeletepostingan yang bagus tentang"The Puzzle Game - Source Code"
ReplyDeletei had another code which uses arrow key to shift the space
ReplyDeletewhich is as follows
#include
#include
#include
#include
getkey()
{
union REGS i,o;
while(!kbhit())
;
i.h.ah=0;
int86(22,&i,&o);
return(o.h.ah);
}
void display(int arr[][4],int a,int b)
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==a&&j==b)
{
printf(" ");
}
else
{
if(arr[i][j]<10)
{
printf(" %d",arr[i][j]);
}
else
{
printf(" %d",arr[i][j]);
}
}
}
printf("\n");
}
}
void move(int c,int arr[][4],int a,int b)
{
int temp;
switch(c)
{
case 72:
temp=arr[a][b];
arr[a][b]=arr[a-1][b];
arr[a-1][b]=temp;
break;
case 80:
temp=arr[a][b];
arr[a][b]=arr[a+1][b];
arr[a+1][b]=temp;
break;
case 75:
temp=arr[a][b];
arr[a][b]=arr[a][b-1];
arr[a][b-1]=temp;
break;
case 77:
temp=arr[a][b];
arr[a][b]=arr[a][b+1];
arr[a][b+1]=temp;
break;
}
}
void main()
{
int a=3,b=3,c,i,j,k,l,m,d;
int arr[4][4]={
1,4,15,7,
8,10,2,11,
14,3,6,13,
12,9,5,16
};
int ans[4][4]={
1,2,3,4,
5,6,7,8,
9,10,11,12,
13,14,15,16
};
void textbackground (int newcolor);
textbackground(RED);
clrscr();
display(arr,a,b);
printf("Press Esc for exit...\n");
while(c!=1)
{
c=getkey();
if(c==1)
{
return(0);
}
else
{
if(((a==3)&&(c==72))||((a==0)&&(c==80))||((b==3)&&(c==75))||((b==0)&&(c==77)))
{
clrscr();
display(arr,a,b);
printf("Invalid move\n");
printf("Press Esc for exit...\n");
}
else
{
switch(c)
{
case 72:
a=a+1;
break;
case 80:
a=a-1;
break;
case 75:
b=b+1;
break;
case 77:
b=b-1;
break;
}
move(c,arr,a,b);
clrscr();
display(arr,a,b);
d=0;
for(l=0;l<4;l++)
{
for(m=0;m<4;m++)
{
if(ans[l][m]==arr[l][m])
{
d=d+1;
}
}
}
if(d==16)
{
printf("You win");
}
else
{
printf("Press Esc for exit...\n");
}
}
}
}
getch();
}
i was addict to CS when I wrote this code so ultimately my fingers went to W A S D
ReplyDelete