Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CODE THIS USING C++ LANGUAGE PLEASE. Vectors store a linear arrangement of value

ID: 3866866 • Letter: C

Question

CODE THIS USING C++ LANGUAGE PLEASE.

Vectors store a linear arrangement of values, accessed by a single index. Using vectors, write a program to play a modified battleship game - a guessing game where the location and number of ships are concealed to the user and the goal of the game is for the user to hit as many ships as possible by selecting the location of the shots. Define a vector of 10 elements where the location of the ships will be stored and changed if they get hit. Select at most 3 random locations to put each ship (denoted as ) but do not show these locations unless there is a hit in the ship or until the end of the game. Display 10 empty spaces (denoted as) in a row. Then, give the user 3 opportunities to select the location of the shots, and display whether they hit a ship (denoted as ) or do not (denoted as *). At the end of the game, display the location of all the ships and the percentage of ships that were destroyed. Figure 1 shows a sample outcome of the game. Note that the user would input the element number, not the index of the vector. Clear your screen every time you print to the console so that you only have one row of symbols at a time.

Explanation / Answer

#include<iostream>
#include<string>
#include<vector>
#include<stdlib.h>
#include<time.h>

using namespace std;

int main(){

   vector<int> loc;
   vector<string> state;
   int pos;
   int found;
   int count;


   for (int i = 0; i<10; i++){
       state.push_back("---");
   }

   srand(time(NULL));
   for(int i=0; i<3; i++){
      pos = rand() % 10 + 1;
      loc.push_back(pos);
      state[pos-1 ] = " <>";
   }
   cout << endl;
   for (int i = 0; i<10; i++)
      cout << "---" << " ";
   count = 0;  
   for (int i = 0; i<3; i++){
       cout << "Shot " << i+1 << " Location ";
       cin >> pos;
       system("clear");
       if (state[pos-1] == " <>"){
           state[pos-1] = "<*>";
           count++;
       }
       if (state[pos-1] == "---")
           state[pos-1] = " * ";

       if (i==2)
          continue;      
       for (int j = 0; j<10; j++){
           if (state[j] == " <>")
              cout << "---" << " ";
           else
              cout << state[j] << " ";
       }
       
   }
   cout << endl;
   for (int i = 0; i<10; i++)
       cout << state[i] << " ";
   cout <<"Success =" << (count*100)/3 << "%" << endl;
  
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote