Sunteți pe pagina 1din 5

#include<iostream.

h>
#include<conio.h>
#include<math.h>
#include<string.h>
void matpos(int k);
void target(int v); //most variables declared globally bescause changes will b
e easily reflected in the program
void target1(int h);
int i,j,hit1=0,hit2=0;
int p1pos[8][8]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51
,52,53,54,55,56,57,58,59,60,61,62,63,64};
int p2pos[8][8]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51
,52,53,54,55,56,57,58,59,60,61,62,63,64};
void main()
{clrscr();
int x,y,z;
cout<<"\t\t\tBATTLESHIP\n"<<"\t\tby ARJUN ";
cout<<"player1 :\n"<<"these are the possible positions that your ships can occup
y\n";
for(int a=0;a<8;a++) //displaying 2D matrix
{for(int b=0;b<8;b++)
{cout<<p1pos[a][b]<<" ";}
cout<<"\n";
}
cout<<"enter the posn of battleship 1: \n";
//taking positions
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of battleship 2: \n";
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of battleship 3: \n";
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of patrol boat 1: \n";
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of patrol boat 2: \n";
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of submarine 1: \n";
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of submarine 2: \n";
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of submarine 3: \n";
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of destroyer 1: \n";
cin>>x;

matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of destroyer 2: \n";
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of destroyer 3: \n";
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of destroyer 4: \n";
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of aircraft carrier 1:
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of aircraft carrier 2:
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of aircraft carrier 3:
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of aircraft carrier 4:
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"enter the posn of aircraft carrier 5:
cin>>x;
matpos(x);
p1pos[i][j]=101;
cout<<"player 1:\n"<<"this is your board(101
s destroyed vessels)\n";
int a4=0;
int b4=0;
for(;a4<8;a4++)
{for(int b4=0;b4<8;b4++)
{cout<<p1pos[a4][b4]<<" ";}
cout<<"\n";}

\n";

\n";

\n";

\n";

\n";

denotes active vessels and 0 denote

//now coding for player 2


cout<<"player2 :\n"<<"these are the possible positions that your ships can occup
y\n";
for(int a1=0;a1<8;a1++)
{for(int b1=0;b1<8;b1++)
{cout<<p2pos[a1][b1]<<" ";}
cout<<"\n";
}
cout<<"enter the posn of battleship 1: \n";
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the posn of battleship 2: \n";
cin>>x;
matpos(x);

p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);
p2pos[i][j]=202;
cout<<"enter the
cin>>x;
matpos(x);

posn of battleship 3: \n";

posn of patrol boat 1: \n";

posn of patrol boat 2: \n";

posn of submarine 1: \n";

posn of submarine 2: \n";

posn of submarine 3: \n";

posn of destroyer 1: \n";

posn of destroyer 2: \n";

posn of destroyer 3: \n";

posn of destroyer 4: \n";

posn of aircraft carrier 1: \n";

posn of aircraft carrier 2: \n";

posn of aircraft carrier 3: \n";

posn of aircraft carrier 4: \n";

posn of aircraft carrier 5: \n";

p2pos[i][j]=202;
cout<<"player 2:\n"<<"this is your board(202 denotes active vessels and 0 denote
s destroyed vessels)\n";
for(int a8=0;a8<8;a8++)
{for(int b8=0;b8<8;b8++)
{cout<<p2pos[a8][b8]<<" ";}
cout<<"\n";}
int c1=0;
//starting to target!!! :D
for(;(hit1<17)&&(hit2<17);)
{cout<<"player 1:\n"<<"this is your board(101 denotes active vessels and 0 denot
es destroyed vessels)\n";
int a4=0;
int b4=0;
for(;a4<8;a4++)
{for(int b4=0;b4<8;b4++)
{cout<<p1pos[a4][b4]<<" ";}
cout<<"\n";}
cout<<"enter which position of player two's you want to target:\n";
cin>>y;
if(hit2==17)
{break;}
target(y);//player 2 targeting next
cout<<"player 2:\n"<<"this is your board(202 denotes active vessels and 0 denote
s destroyed vessels)\n";
a4=0;
b4=0;
for(;a4<8;a4++)
{for(int b4=0;b4<8;b4++)
{cout<<p2pos[a4][b4]<<" ";}
cout<<"\n";}
cout<<"enter which position of player one's you want to target:\n";
cin>>y;
target1(y);
if(hit1==17)
{break;}
}
if(hit1==17)
{cout<<"player 2 has won\n";}
else if(hit2==17)
{cout<<"player 1 has won\n";}
getch();
}
//whew!!! now defining the functions matpos() and target()
void matpos(int k)
{
if((k>=1)&&(k<=8))
{j=k-1;}
else if(k%8!=0)
{j=(k%8)-1;}
else if(k%8==0)
{j=7;}
if((k>=1)&&(k<=8))
{i=0;}
if((k>=9)&&(k<=16))
{i=1;}
if((k>=17)&&(k<=24))
{i=2;}

if((k>=25)&&(k<=32))
{i=3;}
if((k>=33)&&(k<=40))
{i=4;}
if((k>=41)&&(k<=48))
{i=5;}
if((k>=49)&&(k<=56))
{i=6;}
if((k>=57)&&(k<=64))
{i=7;}
}//determines the positions for 1-64 in the 2 dimensional array
void target(int v) //determines whether enemy ship has been destroyed or not
{
matpos(v);
if(p2pos[i][j]==202)
{cout<<"HIT!!!!!!\n";
hit2++;
p2pos[i][j]=0;}
else
{cout<<"miss\n";}
}
void target1(int h)
{
matpos(h);
if(p1pos[i][j]==101)
{cout<<"HIT!!!!!!\n";
hit1++;
p1pos[i][j]=0;}
else
{cout<<"miss\n";}
} //enjoy!!!!!!!!!
i am awesome :D :D :D :D :D

S-ar putea să vă placă și