You are on page 1of 4

/******************************************************************************/

/*
Program Name: Battle Ship Game
Design by: Nathan Sartnurak
Date: 2/18/2017
This program simulates the Battle Ship game. Player one enters position of 3 ships.
After player one finished enter the ship positions, player two will start enter the x and
y coordinates of the ship. Player two must hit all 3 ships in order to win. Good Luck.
Please follow the instructions as you play.

History:
Version Modified by Date Comments
1 Nathan Sartnurak 2/18/2017 Created.

*/
/******************************************************************************/

#include <cstdlib>
#include <iostream>
using namespace std;
int main () {

/******************************************************************************/
// declarations
int P1array [10][10];
char P2array [10][10];
int x,y,S, done1, done2, done3;
int Ship1MaxCnt, Ship2MaxCnt, Ship3MaxCnt, PosY, PosX;
int Ship1HitCnt, Ship2HitCnt, Ship3HitCnt;
char DoneEnter;
//int P1array [5][5] = { }{ };
//int P2array [5][5] = { }{ };
/******************************************************************************/
//----------------initialize player 1 and 2 arrays
for (y=0; y<=9; y++)
for (x=0; x<=9; x++)
{
P1array [y][x]=0;
P2array [y][x]='E';
}
/******************************************************************************/
//----------------initialize all the variables
Ship1MaxCnt=0; //init max cnt
Ship2MaxCnt=0;
Ship3MaxCnt=0;
Ship1HitCnt=0;
Ship2HitCnt=0;
Ship3HitCnt=0;
done1=0;
done2=0;
done3=0;
/******************************************************************************/
//----------------player 1 enter x and y coordinate of the ship position for all 3 ships
cout<<"Player 1 please enter your ship position"<<endl;
for(S=1; S<=3; S++)
{
do {
cout<<"Please enter Ship "<<S<<" coordinates between [0,9]"<<endl;
cout<<"please enter number greater than 9 for exit."<<endl;
cout<<"please enter x position"<<endl;
cin>>PosX;
if (PosX >9) //If position is greater than 9, done
{
break;
}
cout<<"please enter y position"<<endl;
cin>>PosY;
if (PosY >9) // If position is greater than 9, done
{
break;
}
if(S==1)
{
Ship1MaxCnt++; // Add 1 to ship#1 max count
}
if(S==2)
{
Ship2MaxCnt++; // Add 1 to ship#2 max count
}
if(S==3)
{
Ship3MaxCnt++; // Add 1 to ship#3 max count
}
cout<<"Ship 1 count = "<<Ship1MaxCnt<<endl; //Display current ship#1 max count
cout<<"Ship 2 count = "<<Ship2MaxCnt<<endl; //Display current ship#2 max count
cout<<"Ship 3 count = "<<Ship3MaxCnt<<endl; //Display current ship#3 max count
P1array[PosY][PosX]=S;
//----------------display player 1 ships as you enter (so you wont enter the same position twice
cout<<"y | x "<<endl;
cout<<" |0 1 2 3 4 5 6 7 8 9"<<endl;
cout<<" |------------------------------------------------------------------------"<<endl;
for (y=0; y<=9; y++)
{
cout<<y<<" |" ;
for (x=0; x<=9; x++)
{
cout<<P1array[y][x]<<"\t";
}
cout<<endl;
}
} while (PosX <= 9 or PosY <=9);
} //end for S loop

/******************************************************************************/
//----------------Enter player 2 ships position
cout<<"Player 2 please enter your ship position"<<endl;
do
{
cout<<"please enter x position"<<endl;
cin>>PosX;
cout<<"please enter y position"<<endl;
cin>>PosY;
if(P1array[PosY][PosX]== 0) // If the enter position contains 0, it a miss
{
P2array[PosY][PosX]='M'; //Put M into player 2 array to display when miss
}
else if( P1array[PosY][PosX]==1) // if the enter position contains 1, it is a hit for ship 1
{
P2array[PosY][PosX]='H'; //Put H into player 2 array to display when hit
Ship1HitCnt++; //Increase ship 1 hit count
}
else if( P1array[PosY][PosX]==2) // if the enter position contains 2, it is a hit for ship 2
{
P2array[PosY][PosX]='H'; //Put H into player 2 array to display when hit
Ship2HitCnt++; //Increase ship 2 hit count
}
else if( P1array[PosY][PosX]==3) // if the enter position contains 3, it is a hit for ship 3
{
P2array[PosY][PosX]='H'; //Put H into player 2 array to display when hit
Ship3HitCnt++; //Increase ship 3 hit count
}
//----------------display player 2 ships
cout<<"y | x "<<endl;
cout<<" |0 1 2 3 4 5 6 7 8 9"<<endl;
cout<<" |------------------------------------------------------------------------"<<endl;
for (y=0; y<=9; y++)
{
cout<<y<<" |" ;
for (x=0; x<=9; x++)
{
cout<<P2array[y][x]<<"\t";
}
cout<<endl;
}
// for debug, uncomment the following two lines
//cout<<"Hit"<<Ship1HitCnt<<endl;
//cout<<"Max"<<Ship1MaxCnt<<endl;
//----------------display hit message
if(Ship1HitCnt==Ship1MaxCnt and done1!=1) //If hit count and max count are the same, done
{
cout<<"Congrat, you sunk Paul Ryan's Yacht"<<endl;
done1=1;
}
if(Ship2HitCnt==Ship2MaxCnt and done2!=1) //If hit count and max count are the same, done
{
cout<<"Congrat, you sunk Mike Pence's' Yacht"<<endl;
done2=1;
}
if(Ship3HitCnt==Ship3MaxCnt and done3!=1) //If hit count and max count are the same, done
{
cout<<"Congrat, you sunk Donald Trump's' Yacht"<<endl;
done3=1;
}
}while (Ship1HitCnt!=Ship1MaxCnt or Ship2HitCnt!=Ship2MaxCnt or Ship3HitCnt!=Ship3MaxCnt);
cout<<"Congratulations, you just put Hillary Clinton as president"<<endl;

system("PAUSE");
return 0;
}
/******************************************************************************/

You might also like