Using C++ NOT C, Create a Battleship Game with the criteria below. This program
ID: 3713617 • Letter: U
Question
Using C++ NOT C, Create a Battleship Game with the criteria below.
This program will include:
- Function
- Array
- Random number generator
- Loop
- Branching
1. The user will determine which level they would like to play
easy(e), medium(m) or hard(h). These will determine the size of
the board.
2. Using a random number generator, a board will be selected that
determines where the ships are located. These will be pre-made
boards that are indicate the ships as 1’s and blank spaces as
0’s. This board is not displayed to the user.
3. The user will make a choice on the grid (say like B7) they will
need to compare that index to the pre made grid (again 1s and 0s
and then proceed to AND (&) the location indicated by the user to
the static grid... i.e. if B7 is a hit then 1 and 1 still makes a
1 otherwise it is a miss...
4. The board that the player sees will be updated after each play
indicating where ships have been hit.
If the guess is correct, then the board should display the hit. If it is a miss, then it should also indicate this. For the battleship game, also the board needs to be displayed. Thank you in advance!
Explanation / Answer
Code:
#include<iostream>
#include<ctime>
using namespace std;
int main(){
const int h = 10; //number of rows and columns in the board for hard game
const int m=5; //number of rows and columns in the board for medium game
const int e=3; //number of rows and columns in the board for easy game
int board[10][10];
srand(time(NULL));
char choice;
cout<<"Enter choice :"<<endl;
cout<<"Hard Game(h)"<<endl;
cout<<"Medium Game(m)"<<endl;
cout<<"Easy Game(e)"<<endl;
cin>>choice;
int n; //number of rows and columns in the board;
if(choice == 'h' || choice=='H')
n=h;
if(choice=='m' || choice=='M')
n=m;
if(choice=='e' || choice=='E')
n=e;
cout<<"There is a "<<n<<" x "<<n<<" board"<<endl;
for(int i =0;i<n;i++){
for(int j=0;j<n;j++){
board[i][j]=rand()%2;
}
}
cout<<"--------The game starts--------"<<endl;
int score=0;
while(1==1){
cout<<"Enter choice (for e.g. B7): "<<endl;
cout<<"Or press e to exit. "<<endl;
char a,b;
cin>>a;
if(a=='e' || a=='E'){//r is equal to 'e' and so we exit
break;
}
cin>>b;
int r=a-'A';
int c=b-'0';
if(r<n && c<n){
if(board[r][c]==1){ //if there is a ship make that posiition 0 and increase score
board[r][c]=0;
score++;
cout<<"Hit!!"<<endl;
}
else
cout<<"Miss!!"<<endl;
}
else{
cout<<"Grid index is wrong.Please try again."<<endl;
}
}
cout<<"The board is as follows: ";//display the board
for(int i =0;i<n;i++){
for(int j=0;j<n;j++){
cout<<board[i][j]<<" ";
}
cout<<endl;
}
cout<<"Score: "<<score<<endl;
}
Sample Output:
radas-macOS:Desktop radas$ ./a.out
Enter choice :
Hard Game(h)
Medium Game(m)
Easy Game(e)
m
There is a 5 x 5 board
--------The game starts--------
Enter choice (for e.g. B7):
Or press e to exit.
B1
Hit!!
Enter choice (for e.g. B7):
Or press e to exit.
A2
Hit!!
Enter choice (for e.g. B7):
Or press e to exit.
C3
Hit!!
Enter choice (for e.g. B7):
Or press e to exit.
B1
Miss!!
Enter choice (for e.g. B7):
Or press e to exit.
B2
Hit!!
Enter choice (for e.g. B7):
Or press e to exit.
B0
Miss!!
Enter choice (for e.g. B7):
Or press e to exit.
e
The board is as follows: 1 0 0 0 0
0 0 0 0 1
0 0 0 0 0
1 0 1 0 0
1 1 0 1 1
Score: 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.