c++

Puzzle Game C++

Puzzle Game

C++


#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <dos.h>
#include <omid_bahrami.h>

int main()
{

int w=0;
char z='s';
char x7;
char d;
game *p;
easy e;
normal n;
hard h;
timer s;

_setcursortype(_NOCURSOR);
clrscr();
cout<<"e => easy"<<endl<<"n => normal"<<endl<<"h => hard"<<endl;
cout<<"Select Difficalty => ";
begin:
cin>>d;

switch(d)
{

case'e':
p=&e;
break;

case'n':
p=&n;
break;

case'h':
p=&h;
break;

default:
cout<<"Your Entry Is Invalid!"<<endl<<"=> ";
goto begin;
}

p->play();

s.meno();
s.show();
getch();

p->start();
p->print();

while(w==0 && z!='q')
{

while(!kbhit())
{
s.show();
delay(1);
s.plus();
}
z=getch();

p->move(z);
w=p->check();
p->print();

}

p->exit();

return 0;
}

Here Is omid_bahrami.h


class timer
{

public:
timer();
void zero();
void meno();
void show();
void plus();

private:
int ms;
int s;
int m;
int h;
};

void timer::zero()
{
ms=s=m=h=0;
}

timer::timer()
{
zero();
}

void timer::show()
{
int a,b;
b=25;
a=19;

gotoxy(a+5,b);
cout<<h<<" ";

gotoxy(a+12,b);
cout<<m<<" ";

gotoxy(a+19,b);
cout<<s<<" ";

gotoxy(a+32,b);
cout<<ms<<" ";

}

void timer::meno()
{
int a,b;
b=25;
a=19;

gotoxy(a,b);
cout<<"Hour:";

gotoxy(a+8,b);
cout<<"Min:";

gotoxy(a+15,b);
cout<<"Sec:";

gotoxy(a+22,b);
cout<<"Milli Sec:";
}

void timer::plus()
{
ms++;

if(ms==1000)
{
ms=0;
s++;
}

if(s==60)
{
s=0;
m++;
}

if(m==60)
{
m=0;
h++;
}

}

class game
{
public:
game();
void play();
void set();
void first();
int check();
void print();
void chart();
void move(char z1);
void exit();
virtual void start()=0;

protected:
int a[4][4];
char fn[30];
char ln[30];
int v;
int c;
int w;
};

void game::play()
{

clrscr();
set();
clrscr();
chart();
print();

}

game::game()
{
first();
w=c=v=0;
}

void game::set()
{

cout<<"enter first name:";
cin>>fn;

cout<<"enter last name:";
cin>>ln;

}

class easy:public game
{
public:
void start();
};

class normal:public game
{
public:
void start();
};

class hard:public game
{
public:
void start();
};

void easy::start()
{
int n[15];
int m,t,s,x,i,j;
randomize();

for(m=0;m<=14;m++)
n[m]=20;

for(m=0;m<=3;m++)
{
b1:
t=random(4);
t++;
for(s=0;t!=n[s] && s<=14;s++);
if(s>14)
n[m]=t;
else
goto b1;
}

for(m=4;m<=7;m++)
{
b2:
t=random(4);
t=t+5;
for(s=0;t!=n[s] && s<=14;s++);
if(s>14)
n[m]=t;
else
goto b2;
}

for(m=8;m<=11;m++)
{
b3:
t=random(4);
t=t+9;
for(s=0;t!=n[s] && s<=14;s++);
if(s>14)
n[m]=t;
else
goto b3;
}

for(m=12;m<=14;m++)
{
b4:
t=random(3);
t=t+13;
for(s=0;t!=n[s] && s<=14;s++);
if(s>14)
n[m]=t;
else
goto b4;
}

x=0;
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
a[i][j]=n[x];
x++;
}
}
a[3][3]=0;
}

void normal::start()
{
int n[15];
int m,t,s,x,i,j;
randomize();

for(m=0;m<=14;m++)
n[m]=20;

for(m=0;m<=7;m++)
{
b1:
t=random(8);
t++;
for(s=0;t!=n[s] && s<=14;s++);
if(s>14)
n[m]=t;
else
goto b1;
}

for(m=8;m<=14;m++)
{
b2:
t=random(7);
t=t+9;
for(s=0;t!=n[s] && s<=14;s++);
if(s>14)
n[m]=t;
else
goto b2;
}

x=0;
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
a[i][j]=n[x];
x++;
}
}
a[3][3]=0;
}

void hard::start()
{
int n[15];
int m,t,s,x,i,j;
randomize();

for(m=0;m<=14;m++)
n[m]=15;

for(m=0;m<=14;m++)
{
b1:
t=random(15);
for(s=0;t!=n[s] && s<=14;s++);
if(s>14)
n[m]=t;
else
goto b1;
}

for(m=0;m<=14;m++)
n[m]=n[m]+1;

x=0;
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
a[i][j]=n[x];
x++;
}
}
a[3][3]=0;
}

void game::first()
{
int x,i,j,f[16];

for(i=0;i<=14;i++)
f[i]=i+1;
f[15]=0;

x=0;
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
{
a[i][j]=f[x];
x++;
}

}

int game::check()
{
int x,i,j,f[16];

for(i=0;i<=14;i++)
f[i]=i+1;
f[15]=0;

x=0;
for(i=0;i<=3;i++)
for(j=0;j<=3;j++)
{
if( a[i][j]!=f[x])
goto down;
else
x++;
}

down:
if(i>3 && j>3)
w=1;
else
w=0;

v=x;

return w;
}

void game::print()
{
int i,j;

for(i=0;i<=3;i++)
{

for(j=0;j<=3;j++)
{

if(a[i][j]==0)
{
gotoxy(((j+1)*5)+25,((i+1)*5));
cout<<" ";
}

else
{

if(a[i][j]<=4 && a[i][j]>=1)
{
gotoxy(((j+1)*5)+25,((i+1)*5));
textcolor(15);
cprintf("%d ",a[i][j]);
}

else if(a[i][j]<=8 && a[i][j]>=5)
{
gotoxy(((j+1)*5)+25,((i+1)*5));
textcolor(13);
cprintf("%d ",a[i][j]);
}

else if(a[i][j]<=12 && a[i][j]>=9)
{
gotoxy(((j+1)*5)+25,((i+1)*5));
textcolor(10);
cprintf("%d ",a[i][j]);
}

else if(a[i][j]<=15 && a[i][j]>=13)
{
gotoxy(((j+1)*5)+25,((i+1)*5));
textcolor(12);
cprintf("%d ",a[i][j]);
}

}
}
}

gotoxy(10,1);
cout<<(c);

gotoxy(17,3);
cout<<v<<" ";
}

void game::chart()
{
int t,i;
i=100;
textcolor(11);

for(t=4;t<=21;t++)
{
gotoxy(28,t);
cprintf("|");

gotoxy(33,t);
cprintf("|");

gotoxy(38,t);
cprintf("|");

gotoxy(43,t);
cprintf("|");

gotoxy(48,t);
cprintf("|");

delay(i);
}

for(t=0;t<=18;t++)
{
gotoxy(29+t,4);
cprintf("-");

gotoxy(29+t,8);
cprintf("-");

gotoxy(29+t,12);
cprintf("-");

gotoxy(29+t,16);
cprintf("-");

gotoxy(29+t,21);
cprintf("-");

delay(i);
}

gotoxy(26,1);
textcolor(15);
cprintf("Programmer => Omid Bahrami");

gotoxy(1,1);
cout<<"Count => ";

gotoxy(1,3);
cout<<"First Rights => ";
}

void game::move(char z)
{
int x,y;

for(x=0;x<=3;x++)
for(y=0;y<=3;y++)
if(a[x][y]==0)
goto p;
p:

switch(z)
{

case 77:

if(y==0)
break;

else
{
a[x][y]=a[x][y-1];
a[x][y-1]=0;
c++;
break;
}

case 75:

if(y==3)
break;

else
{
a[x][y]=a[x][y+1];
a[x][y+1]=0;
c++;
break;
}

case 72:

if(x==3)
break;

else
{
a[x][y]=a[x+1][y];
a[x+1][y]=0;
c++;
break;
}

case 80:

if(x==0)
break;

else
{
a[x][y]=a[x-1][y];
a[x-1][y]=0;
c++;
break;
}

}

}

void game::exit()
{

gotoxy(21,1);
textcolor(15);
cout<<"First name => "<<fn<<"\tLast name => "<<ln;
gotoxy(31,2);

if(w==1)
cprintf("You Are Winner!");

else
cprintf("You Are Loser!");

getch();
}