Vectors store a linear arrangement of values, accessed by a single index. Using
ID: 3866869 • Letter: V
Question
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. MAKE SURE TO HAVE AS MUCH FUNCTION AS POSSILE AND LESS CODING IN MAIN FUNCTION
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.v
Shot 1 Location: 3 -Shot 2 Location: 4 .Explanation / Answer
#include #include using namespace std; const int BOARD_WIDTH = 15; const int BOARD_HEIGHT = 10; const int SHIP_TYPES = 5; const char isWATER = 247; //ASCII Character Code const char isHIT = 'X'; const char isSHIP = 'S'; const char isMISS = '0'; struct POINT { //A location on the grid defined //by X(horizontal) Y(vertical) coordinates int X; int Y; }; struct SHIP { //Ship name string name; //Total points on the grid int length; //Coordinates of those points POINT onGrid[5]; //0-4 max length of biggest ship //Whether or not those points are a "hit" bool hitFlag[5]; }ship[SHIP_TYPES]; struct PLAYER { char grid[BOARD_WIDTH][BOARD_HEIGHT]; }player[3]; //Ignore player 0, just using player's 1 & 2 enum DIRECTION {HORIZONTAL,VERTICAL}; struct PLACESHIPS { DIRECTION direction; SHIP shipType; }; bool gameRunning = false; //Functions void LoadShips(); void ResetBoard(); void DrawBoard(int); PLACESHIPS UserInputShipPlacement(); bool UserInputAttack(int&,int&,int); bool GameOverCheck(int); int main() { LoadShips(); ResetBoard(); //"PLACE SHIPS" phase of game //Loop through each player... for (int aplyr=1; aplyrRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.