Você está na página 1de 9

#include<fstream.

h>
#include<iostream.h>
#include<conio.h>
#include<bios.h>
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void inst()
{
outtextxy(210,110,"welcome to battle ship");
outtextxy(210,120,"use arrow keys to move");
outtextxy(210,130,"Player1 use r to deploy ship");
outtextxy(210,140,"Player 2use r to search for ship");
outtextxy(210,150,"player 1 hides ships");
outtextxy(210,160,"player 2 finds them");
outtextxy(210,190,"PLEASE TURN YOUR CAPS LOCK OFF");
outtextxy(210,200,"PRESS ANY KEY TO CONTINUE");
getch();
}
void board()
{
int i;
cleardevice();
line(0,0,0,400);
line(0,0,400,0);
line(0,400,400,400);
line(400,0,400,400);
for(i=40;i<400;i=i+40)
{
line(400,i,0,i);
line(i,400,i,0);
}
}
void re(int grid[10][10])
{
int i=0,j=0;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
grid[i][j]=0;
}
void deploy(int grid[10][10])
{
double long a=0;
int x11=0,y11=0,x22=0,y22=0;
int x1=5,x2=35,y1=35,y2=35;
int flip=0;
int x=0,y=0,up=0,d=0,l=0,r=0,flag=0,col;
for(int s=1;s<4;s++)
{ flag=0;
while(1)
{ /*input*/
col=2;
a=bioskey(a);
board();

/*code for special cases like enter or space or 'r' button*/


if(a==14624) //space//
{
if (flip==1)
{
//for making sure flip statement dosent glitch to show ship out of matr
ix//
if((flip==1)&&(x1>205)||(x2>235))
flip=1;
else
flip=0;
}
else
{
//for making sure flip statement dosent glitch to show ship out of matr
ix//
if((flip==0)&&(y1>235)||(y2>235))
flip=0;
else
flip=1;
}
continue;
}
if(a==29184) //for r button//
{
for(int o=0;o<5;o++)
{
if(flip==1 && grid[y+o][x]==0)
col=1;
if(flip==1 && grid[y+o][x]!=0)
{
col=0;
break;
}
if(flip==0 && grid[y][x+o]==0)
col=1;
if(flip==0 && grid[y][x+o]!=0)
{
col=0;
break;
}
}
if(col==1)
{
for(int val=0;val<5;val++)
{
if(flip==1)
grid[y+val][x]=s;
if(flip==0)
grid[y][x+val]=s;
}

}
}
if(col==1)
{
break;
}
if(col==0)
{
outtextxy(410,10,"there is already a ship deployed here");
}
for(int row=0;row<10;row++)
for(int clmn=0;clmn<10;clmn++)
if (grid[row][clmn]!=0)
bar(clmn*40,row*40,clmn*40+40,row*40+40);
//end of special cases//
if (a==18432)//up//
{
x11=0;
x22=0;
y11=(-40);
y22=(-40);
up++;
if(y<1)
up--;
}
else if (a==20480)//down//
{
x11=0;
x22=0;
y11=40;
y22=40;
d++;
if(flip==1 && y>4)
d--;
if(flip==0 && y>9)
d--;
}
else if (a==19200)//left//
{
x22=(-40);
x11=(-40);
y11=0;
y22=0;
l++;
if(x<1)
l--;
}
else if (a==19712)//right//
{
x22=40;
x11=40;
y11=0;
y22=0;
r++;
if(flip==1 && x>8)
r--;
if(flip==0 && x>4)

r--;
}
/*setting of co ordinates*/
x1+=x11;
x2+=x22;
y1+=y11;
y2+=y22;
y=d-up;
x=r-l;
/*co ordinates are set*/

/*condition for blocks to stay inside board*/


//for horizontal position//
if ((flip==0)&&(x1<5||x1>205||y1<35||y1>400||x2<35||x2>235||y2<35||y2>400))
{
x1-=x11;
x2-=x22;
y1-=y11;
y2-=y22;

}
//condtion for vertical//
else if ((flip==1)&&(x1<5||x1>400||y1<5||y1>245||x2<35||x2>400||y2<35||y2>275
))
{
x1-=x11;
x2-=x22;
y1-=y11;
y2-=y22;
}
x11=x22=y22=y11=0;
//for showing ship//
for(int j=0;j<200;j+=40)
{
if(flip==0)
line (x1+j,y1,x2+j,y2);
else
line (x1,y1+j,x2,y2+j);
}
}
}
}
/*end of deploy fun*/
int search(int grid[10][10])
{
double long a=0;
int x11=0,y11=0,x22=0,y22=0;
int x1=5,x2=35,y1=35,y2=35;
int flip=0;

int x=0,y=0,up=0,d=0,l=0,r=0,flag=0,s1=0,s2=0,s3=0,col=2,count=0;
cleardevice();
outtextxy(210,110,"Player 2 begin search");
getch();
while(1)
{
/*input*/
a=bioskey(a);
/*code for board*/
setcolor(15);
board();
if (a==29184)//r//
{ count++;
if (grid[y][x]==1)
{
outtextxy(410,150,"nice one");
s1++;
grid[y][x]=5;
}
if (grid[y][x]==2)
{
outtextxy(410,150,"nice one");
s2++;
grid[y][x]=6;
}
if (grid[y][x]==3)
{
outtextxy(410,150,"nice one");
s3++;
grid[y][x]=7;
}
if (grid[y][x]==0)
{
outtextxy(410,150,"wrong guess");
}
if ( s1==5 )
{
outtextxy(410,160,"congrats on finding ship1");
}
if ( s2==5 )
{
outtextxy(410,170,"congrats on finding ship2");
}
if ( s3==5 )
{
outtextxy(410,180,"congrats on finding ship3");
}

if ( s1==5 && s2==5 && s3==5)


return count;
}
for(int row=0;row<10;row++)
for(int clmn=0;clmn<10;clmn++)
{
if (grid[row][clmn]==5)
{
setfillstyle(1,1);
bar(clmn*40+5,row*40+5,clmn*40+35,row*40+35);
}
if (grid[row][clmn]==6)
{
setfillstyle(1,2);
bar(clmn*40+5,row*40+5,clmn*40+35,row*40+35);
}
if (grid[row][clmn]==7)
{
setfillstyle(1,5);
bar(clmn*40+5,row*40+5,clmn*40+35,row*40+35);
}
}
if (a==18432)//up//
{
x11=0;
x22=0;
y11=(-40);
y22=(-40);
up++;
if(y<=0)
up--;
}
else if (a==20480)//down//
{
x11=0;
x22=0;
y11=40;
y22=40;
d++;
if(y>=9)
d--;
}
else if (a==19200)//left//
{
x22=(-40);
x11=(-40);
y11=0;
y22=0;
l++;
if(x<=0)
l--;
}
else if (a==19712)//right//
{
x22=40;
x11=40;

y11=0;
y22=0;
r++;
if(x>=9)
r--;
}
/*setting of co ordinates*/
x1+=x11;
x2+=x22;
y1+=y11;
y2+=y22;
y=d-up;
x=r-l;
/*co ordinates are set*/
if(x1<0||x2>400||y1<0||y2>400)
{
x1-=x11;
x2-=x22;
y1-=y11;
y2-=y22;
}
line(x1,y1,x2,y2);
x11=x22=y11=y22=0;
}
}
/*end of search fun*/
class hs
{
int scr;
char name[10];
public:
int getsc()
{
return scr;
}
hs()
{
scr=1000;
}
void set(int count,char n[10])
{
scr=count;
strcpy(name,n);
}
void disp()
{
cout<<" Name: "<<name<<" Score: "<<scr<<endl;
}
}ol[5],nw[5];
void score(int count)
{
char n[10];

getch();
cout<<endl<<"please enter your name:";
cin>>n;
fstream o("hs.dat", ios::in | ios::binary );
fstream i("temp01.dat", ios::out | ios::noreplace | ios::binary);
if(!o)
cout<<"!o";
if(!i)
cout<<"!i";
nw[0].set(count,n);
int val=0;
o.seekg(0);
i.seekp(0);
int addr;
int k=0;
while(val<5)
{
o.read((char*)&ol[val],sizeof(ol[val]));
if( ( nw[0].getsc()<=ol[val].getsc() ) && k==0 )
{ i.write((char*)&nw[0],sizeof(nw[0]));
k=1;
}
else
{
if(k==1)
i.write((char*)&ol[val-1],sizeof(nw[val-1]));
else
i.write((char*)&ol[val],sizeof(nw[val]));
}
val++;
}
o.close();
i.close();
remove("hs.dat");
rename("temp01.dat","hs.dat");
o.open("hs.dat",ios::in) ;
cout<<"Top high score: "<<endl;
o.seekg(0);
for(int l=0;l<5;l++)
{
o.read((char*)&ol[l],sizeof(ol[l]));
if(ol[l].getsc()==1000)
break;
ol[l].disp();
}
getch();
}
void main()
{
int gdriver=DETECT,gmode,errorcode,i,flip=0;
initgraph(&gdriver,&gmode,"c:/TC/BGI ");
int grid[10][10],count=0;

char a;
inst();
re(grid);
deploy(grid);
count=search(grid);
cleardevice();
outtextxy(1,1,"congrats on winning the game");
cout<<endl<<"you won the game in "<<count<<" no of steps." ;
score(count);
cout<<endl<<"PRESS ANY KEY TO CONTINUE";
bioskey(a);

Você também pode gostar