In this lab , you will build the “ The Battle Ship s Game ”. The Game has a n x
ID: 3864508 • Letter: I
Question
In this lab , you will build the “ The Battle Ship s Game ”. The Game has a n x n grid and m ships. Each ship takes up exactly three cells. In the game, it’s you against the computer, but unlike the real battleship game, you don’t place any ships on your own. Instead, your job is to sink the computer’s ship in the fewest number of guess.
Goal:Sink all the computer’s ships(0
Setup:When the game program is launched, user is again asked to enter whatwould be the size of the grid, that is the value ofn. The value ofnmust bebetween 3and10. Then, the computer placesmships on an x ngrid (for an example, see thefigure below).When that’scomplete the game asks for your first guess.
How to play:We haven’t learned to build GUI (Graphical User Interface) yet, so thisversion works at the command-line. The computer will prompt you to enter a guess(a cell), that you willtype at the command-line as “1 3”(where “1 3” means 3rd cell of the 1strow), “45”, etc.. In response to your guess, you will see a result at thecommand line, either “Hit”, “Miss”, “Kill” (or whatever the lucky battleship of the dayis!). When you have sunk all m battleships, the game ends by printing out your rating.
Here is a sample run of the game:
Welcome to the Battle Ships game!
Please enter the size of the grid: 8
Please enter number of ships: 3
Your goal is to sink 3 ships.
Try to sink them all in the fewest number of guesses
Enter a guess [row (1 to 8)] [col (1 to 8)]: 1 3 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 2 1 hit
Enter a guess [row(1 to 8)] [col (1 to 8)]: 2 2 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 3 1 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 1 kill
Enter a guess [row (1 to 8)] [col (1 to 8)]: 3 3 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 1 7 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 3 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 4 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 4 5 kill
Enter a guess [row (1 to 8)] [col (1 to 8)]: 5 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 3 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 6 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 61 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 6 1 miss
Enter a guess [row (1 to 8)] [col (1 to 8)]: 8 4 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 8 5 hit
Enter a guess [row (1 to 8)] [col (1 to 8)]: 8 6 kill
All ships are sank!
It only took you18 guesses.
You got out before your options sank.
When player can finish the game within 20 guesses,print the following message (where xx is the number of guesses):
All ships are sank! It only took you XX guesses.
You got out before your options sank.
If it takes more than 20 guesses, print the following message All ships are sank! Took you long enough.XX guesses.Fish are dancing with your options.
Your program should term
inate after 25 guesses and
reveal the positions of the
ships.
NOTE: While designing the classes for this lab, you must follow OOP design concepts (Abstraction,Modularity,Inheritance and API design)
i need a UML diagram for the program
and a code for the program in java application
and proper comments for the program.
.
Explanation / Answer
#include <iostream>
#include <cstdlib>
using namespace std;
struct Ship{
int power;
int p0[3][2];
};
int srink(Ship*** g,int size );
bool createGrid(Ship*** g,int size);
bool createRandShip(Ship*** g,int size ,int num);
void printGrid(Ship*** g,int size );
int main()
{
int grid_size;
int numOfShips;
cout << "Please Enter Size of Grid " ;
cin >> grid_size;
cout << "Please enter number of ships : " ;
cin >> numOfShips;
cout << "Try to sink them all in the fewest number of guesses"<<endl;
Ship ***grid;
grid = new Ship**[grid_size];
for(int i = 0; i < grid_size; ++i)
grid[i] = new Ship*[grid_size];
for(int i = 0; i < grid_size; ++i)
for(int j = 0; j < grid_size; ++j)
grid[i][j] = NULL;
createRandShip(grid,grid_size ,numOfShips);
//printGrid(grid,grid_size);
int numOfGuess = 0;
while(numOfShips>0){
printGrid(grid,grid_size);
if(srink(grid,grid_size)==3){
numOfShips--;
}
numOfGuess++;
if(numOfGuess==20){
cout << "All ships are not sank!"<<endl;
cout << "Took you long enough "<<numOfGuess<<" guesses.!"<<endl;
cout << "Fish are dancing with your options."<<endl;
}
if(numOfGuess==25){
break;
}
}
if(numOfGuess<=20){
cout << "All ships are sank!"<<endl;
cout << "It only took you "<<numOfGuess<<" guesses.!"<<endl;
cout << "You got out before your options sank."<<endl;
}
if(numOfGuess>20 && numOfShips>0){
cout << "You are not able to sank all ships!"<<endl;
cout << "Here is grid:"<<endl;
printGrid(grid,grid_size);
}
if(numOfGuess>20 && numOfShips==0){
cout << "All ships are sank!"<<endl;
}
return 0;
}
bool createGrid(Ship*** g,int size ){
g = new Ship**[size];
for(int i = 0; i < size; ++i)
g[i] = new Ship*[size];
for(int i = 0; i < size; ++i)
for(int j = 0; j < size; ++j)
g[i][j] = NULL;
}
int srink(Ship*** g,int size ){
int x;
int y;
while(true){
cout <<"Enter a guess [row (1 to "<<size<<")] [col (1 to "<<size<<")]:";
cin >>x >>y;
x--;
y--;
if(x>=0&&x<size && y>=0&&y<size){
break;
}
else{
cout<<"Invalid Value"<<endl;
continue;
}
}
Ship * p = g[x][y];
if(p == NULL){
cout<<" Miss"<<endl;
return 0;
}
else{
p->power--;
g[x][y] = NULL;
if(p->power==0){
cout<<" Kill"<<endl;
return 3;
}
else{
cout<<" Hit"<<endl;
return 2;
}
}
}
void printGrid(Ship*** g,int size ){
for(int i = 0; i < size; ++i){
for(int j = 0; j < size; ++j){
Ship *p = g[i][j];
if(p != NULL){
cout <<" "<<p->power;
}
else
cout <<" 0";
}
cout << endl;
}
}
bool createRandShip(Ship*** g,int size ,int num){
int count = 0;
while(num>0){
int xPos = rand()%size;
int yPos = rand()%size;
if((xPos+1)<size && (xPos-1)>=0 && g[xPos][yPos]==NULL && g[xPos+1][yPos]==NULL && g[xPos-1][yPos]==NULL){
Ship *p = new Ship;
p->power = 3;
g[xPos][yPos] = p;
g[xPos+1][yPos] = p;
g[xPos-1][yPos] = p;
num--;
cout <<"Vert xPos="<<xPos<<" yPos "<<yPos<<endl;
}
else if((yPos+1)<size && (yPos-1)>=0 && g[xPos][yPos]==NULL && g[xPos][yPos+1]==NULL && g[xPos][yPos-1] == NULL){
Ship *p = new Ship;
p->power = 3;
g[xPos][yPos] = p;
g[xPos][yPos+1] = p;
g[xPos][yPos-1] = p;
num--;
cout <<"Horz xPos="<<xPos<<" yPos "<<yPos<<endl;
}
count++;
if(count>200)
break;
}
if(count>200)
return false;
return true;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.