Shuffle game using C++

If you think playing a game as this is a child’s play, try a hand on this and think again. Its all about how you plan on making your next move. All the best, have fun.

Here is a simple game developed in C graphics which is called ‘The Shuffle Game’. We believe u have already played many of such games where in you need to arrange scrambled pieces of a picture, alphabets or numbers. In the present program you have been given a matrix of 16 squares of which 15 are assigned with 15 different numbers and 1 is left out blank (empty space for movement of the remaining boxes) which are all shuffled. All you have to do is arrange these numbers in order as shown in the picture given below. The number of moves and the number that have been arranged by you in the proper position will be continuously displayed on the screen.  The controls  are as given below.

Controls:

You can move the boxes by using the arrow keys on your keyboard or by simply clicking on the box which you want to move into the empty space. While using the arrow keys, if you want to move the box left to the empty space just hit the left arrow button. Similarly you can move the boxes on the top, bottom or right of the empty box by hitting the up, down or right arrows respectively. In case you want to restart the game all over again just click the ‘RESTART’ button or if you want to quit the game, all you have to do is to click the ‘EXIT’ button on the screen.

Shuffle game using C++
Shuffle game using C++

Source code:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<alloc.h>
#include<process.h>
union REGS in,out;
void *buf;
int size,count=0,px,py,rnd[4][4],correct=0;

float octave [7]={130.81,146.83,164.81,174.61,196,220,246.94};

int callmouse()
{    in.x.ax=1;
int86(51,&in,&out);
return 1;

}
void mouseposi(int &xpos,int &ypos,int &click)
{   in.x.ax=3;
int86(51,&in,&out);
click=out.x.bx;
xpos=out.x.cx;
ypos=out.x.dx;
return ;
}
int  mousehide()
{
in.x.ax=2;
int86(51,&in,&out);
return 1;
}
void done(void)
{
int i,j;
mousehide();
j=0;
int n;
for(i=1;i<=15;i++)
{
n=random(7);
sound(octave[n]*4);
delay(300);
setfillstyle(1,2);
bar(42+j,425,68+j,455);
setfillstyle(1,0);
bar(68+j,425,72+j,455);
j=j+29;
}
nosound();
delay(1000);
for(j=0;j<382;j+=2)
{
for(i=0;i<575;i+=2)
{
putpixel(26+i,25+j,4);
putpixel(26+i,460-j,4);
putpixel(25+j*3/4,28+i*3/4,2);
putpixel(595-j*3/4,458-i*3/4,2);
delay(0);
}
}
delay(1000);
settextstyle(1,0,5);
setcolor(14);

outtextxy(320,300,”https://studentprojects.in”);
outtextxy(321,300,”https://studentprojects.in”);
delay(500);
getch();
exit(0);
}
checknum(int mat[4][4])
{
int k=0,i,j;
correct=0;
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
k++;
if(k==mat[i][j])
correct++;
}
}
gotoxy(10,11);
printf(“NUMBERS ARE IN CORRECT POSITION IS ..: %d  “,correct);
if(correct==15)
{
bar(130,40,480,80);
settextstyle(1,0,4);
outtextxy(300,60,”CONGRAGULATIONS !”);
done();
}
else
{
bar(20,415,480,463);
j=0;
for(i=1;i<=correct;i++)
{
setfillstyle(1,1);
bar(42+j,425,68+j,455);
setfillstyle(1,0);
bar(68+j,425,72+j,455);
j=j+29;
}
}
return 0;
}

void  move(int &a,int &b,int &c,int &d,int &w,int &x,int &y,int &z,int &num)
{
if(a>192 && b>190 && c<403 && d<404)
{
sound(600);
count++;
gotoxy(10,9);
printf(“NUMBER OF MOVES..: %d   “,count);
mousehide();
size=imagesize(a,b,c,d);
free(buf);
buf=malloc(size);
getimage(a,b,c,d,buf);
bar(a,b,c,d);
putimage(w,x,buf,COPY_PUT);
w=a;
x=b;
y=c;
z=d;
if(num==1)
{
rnd[px][py]=rnd[px+1][py];
rnd[px+1][py]=0;
px++;
}
else if(num==2)
{
rnd[px][py]=rnd[px-1][py];
rnd[px-1][py]=0;
px–;
}
else if(num==3)
{
rnd[px][py]=rnd[px][py+1];
rnd[px][py+1]=0;
py++;
}
else if(num==4)
{
rnd[px][py]=rnd[px][py-1];
rnd[px][py-1]=0;
py–;
}
checknum(rnd);
callmouse();
delay(40);
nosound();
}
return ;
}

main()
{
int a1,b1,cl,a,b,c,d,w,x,y,z,key,p=0,q=0;
int g=DETECT,m,ext=0,rst=0,rx,ry,num,i,j;
initgraph(&g,&m,”c:\\TC\\bgi”);
randomize();
gotoxy(50,9);
printf(“https://studentprojects.in”);

do
{
correct=0;
count=0;
gotoxy(10,9);
printf(“NUMBER OF MOVES..: %d   “,count);

gotoxy(10,11);
printf(“NUMBERS ARE IN CORRECT POSITION IS ..: %d  “,correct);
setfillstyle(SOLID_FILL,4);
setcolor(15);
for(j=200;j<360;j+=50)
{
for(i=200;i<365;i+=50)
{
rectangle(j-1,i-1,j+46,i+46);
bar(j,i,j+45,i+45);
}
}
rectangle(192,190,403,404);
settextstyle(1,0,3);
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
rnd[i][j]=0;
}
}
for(i=0;i<=15;i++)
{
do
{
rx=random(4);
ry=random(4);
}  while(rnd[rx][ry]);
rnd[rx][ry]=i;
}
char ab[10];
settextjustify(CENTER_TEXT,CENTER_TEXT);
q=p=0;
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
if(rnd[i][j]!=0)
{
sprintf(ab,”%d”,rnd[i][j]);
outtextxy(225+p,217+q,ab);
}
else
{
setfillstyle(1,0);
px=i;py=j;
bar(199+p,199+q,247+p,247+q);
w=199+p;
x=199+q;
y=247+p;
z=247+q;
}
p+=50 ;
}
p=0;
q+=50 ;
}
bar(130,40,480,80);
bar(10,425,480,455);
setfillstyle(SOLID_FILL,4);
rectangle(534,374,591,411);
bar(535,375,590,410);
outtextxy(561,390,”EXIT”);
rectangle(484,419,591,456);
bar(485,420,590,455);
outtextxy(538,433,”RESTART”);
setcolor(4);
rectangle(10,10,610,470);
rectangle(15,15,605,465);
setcolor(14);
rectangle(12,12,607,467);
settextstyle(1,0,4);
outtextxy(300,60,”SHUFFLE GAME”);
line(150,85,450,85);

j=0;
delay(1500);
int n;
for(i=1;i<=15;i++)
{
n=random(7);
sound(octave[n]*4);
delay(300);
setfillstyle(1,2);
bar(42+j,425,68+j,455);
setfillstyle(1,0);
bar(68+j,425,72+j,455);
j=j+29;
}
nosound();
setfillstyle(SOLID_FILL,0);
bar(30,425,480,455);
settextstyle(2,0,5);
outtextxy(250,425,”YOU CAN USE ARROW BUTTONS OR MOUSE TO MOVE BLOCKS.”);
outtextxy(250,450,”      PRESS ANY KEY TO START……..” );

getch();
bar(20,415,480,463);
callmouse();
do
{
gotoxy(10,20);
mouseposi(a1,b1,cl);
if(a1>w && a1<y && b1>x+50 && b1<z+50 && cl==1)
{
num=1;
move(w,x+50,y,z+50,w,x,y,z,num);                   //up
}
if(a1>w && a1<y && b1>x-50 && b1<z-50 && cl==1)
{
num=2;
move(w,x-50,y,z-50,w,x,y,z,num);              //down
}
if(a1>w+50 && a1<y+50 && b1>x && b1<z && cl==1)
{
num=3;
move(w+50,x,y+50,z,w,x,y,z,num);                   //left
}
if(a1>w-50 && a1<y-50 && b1>x && b1<z && cl==1)
{
num=4;
move(w-50,x,y-50,z,w,x,y,z,num);                   //right
}
if(kbhit())
{
key=getch();
switch(key)
{
case 72: num=1;
move(w,x+50,y,z+50,w,x,y,z,num);           //up
break;
case 80: num=2;
move(w,x-50,y,z-50,w,x,y,z,num);           //down
break;
case 75: num=3;
move(w+50,x,y+50,z,w,x,y,z,num);          //left
break;
case 77: num=4;
move(w-50,x,y-50,z,w,x,y,z,num);          //right
break;
}
}

if((a1>535 && a1<590 && b1>375 && b1<410 && cl==1) || key==27)
done();
rst=0;
if(a1>485 && a1<590 && b1>420 && b1<455 && cl==1 )
rst=1;
} while(rst==0);
} while(rst!=0);
closegraph();
}

If you find difficult to compile the code, Download source code, executable file and supporting files here.

You have seen it, you have played it, you have seen the source code. Now its your turn to show us how creative your thinking is!! We hope you have got all the ideas that you need to build such a game with C graphic. If you are successful do send us a copy of the game. We will see through that your name comes on this site.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

7 thoughts on “Shuffle game using C++

  1. hey guys i wana c++ prog for shuffle game….bt here i found it in c….can i get c++ prog for the
    same 🙂 thanks

  2. Hello sir.Iam a am a student .I have selected this programme for my 12th board practical examination.But the programme cannot be executed.There are many errors.(more than 20). Can you please help me;Please sent the correct source code immediate as possible;(before 30-jan -2013).

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in